|
这是写入串口的函数:
- BOOL WriteComm(HWND hWnd) //写串口
- {
- extern char *pszReadWriteComm;
- extern HANDLE m_hCom;
- extern FileSize,WriteFlag;
- char pszWriteBuffer[1024];
- DWORD WriteBytes;
- OVERLAPPED hWriteOver;///重叠IO
- int FlagState=1,dwErr;
- // OVERLAPPED结构实始化为0
- memset(&hWriteOver,0x00,sizeof(OVERLAPPED));
- hWriteOver.Internal=0;
- hWriteOver.InternalHigh=0;
- hWriteOver.Offset=0;
- hWriteOver.OffsetHigh=0;
- hWriteOver.hEvent=CreateEvent(0,TRUE,FALSE,0);
- if(hWriteOver.hEvent!=NULL)
- {
- if(WriteFlag==1)
- {
- if(!WriteFile(m_hCom,pszReadWriteComm,FileSize,&WriteBytes,&hWriteOver))
- {
- if(GetLastError()!=ERROR_IO_PENDING)
- MessageBox(hWnd,"写串口失败",0,MB_OK);
- else
- {
-
- dwErr=WaitForSingleObject(hWriteOver.hEvent,INFINITE);
- switch(dwErr)
- {
-
- case WAIT_OBJECT_0:
- if(!GetOverlappedResult(m_hCom,&hWriteOver,&WriteBytes,FALSE))
- MessageBox(hWnd,"重叠IO失败",0,MB_OK);
-
- break;
- default:
- MessageBox(hWnd,"重叠IO失败",0,MB_OK);
- break;
- }
- }
- }
- }
- else
- {
-
- byte buffsg[8]={0x00,0x03,0x00,0x50,0x00,0x0A,0x00,0x00};
- if(!WriteFile(m_hCom,buffsg,8,&WriteBytes,&hWriteOver))
- {
- //ShowError();
- if(GetLastError()!=ERROR_IO_PENDING)
- MessageBox(hWnd,"写串口失败",0,MB_OK);
- else
- {
-
- dwErr=WaitForSingleObject(hWriteOver.hEvent,INFINITE);
- switch(dwErr)
- {
- case WAIT_OBJECT_0:
- if(!GetOverlappedResult(m_hCom,&hWriteOver,&WriteBytes,FALSE))
- MessageBox(hWnd,"重叠IO失败",0,MB_OK);
- break;
- default:
- MessageBox(hWnd,"重叠IO失败",0,MB_OK);
- break;
- }
- }
- }
- }
- }
- PurgeComm(m_hCom,PURGE_TXABORT | PURGE_TXCLEAR);
-
- return TRUE;
- }
复制代码 |
|