|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2015-10-25 10:49:19
|
显示全部楼层
本帖最后由 yxylxj 于 2015-10-25 10:50 编辑
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <windows.h>
- #include <winbase.h>
- #include <winerror.h>
- #include <conio.h>
- #define RX_SIZE 1024*8
- #define true 1 //数据缓冲区大小
- #define false 0
- #define bool char
- DCB dcb;
- COMMTIMEOUTS ct;
- HANDLE hCommDevice;
- bool m_portopen,OpenCloseFlag;
- OVERLAPPED m_ovread,m_ovwrite,m_ovwait;
- int ProcessErrorMessage(char* ErrorText)
- {
- char szBuf[128];
- LPVOID lpMsgBuf;
- DWORD dw = GetLastError();
- FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- dw,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL );
- sprintf(szBuf,"%s 出错信息 (出错码=%ld): %s",ErrorText, dw,(char *) lpMsgBuf);
- LocalFree(lpMsgBuf);
- //输出提示。
- printf(szBuf);
- return 0;
- }
- void CCommPort(void)
- {
- hCommDevice = NULL;
- m_portopen = false;
- memset(&m_ovread,0,sizeof(OVERLAPPED));
- memset(&m_ovwrite,0,sizeof(OVERLAPPED));
- memset(&m_ovwait,0,sizeof(OVERLAPPED));
- m_ovwait.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
- m_ovwrite.hEvent= CreateEvent(NULL,TRUE,FALSE,NULL);
- m_ovread.hEvent= CreateEvent(NULL,TRUE,FALSE,NULL);
- }
- //---------------------------------------------------------------------------
- void CCommPort2(void)
- {
- if (hCommDevice)
- CloseHandle(hCommDevice);
- CloseHandle(m_ovwait.hEvent);
- CloseHandle(m_ovwrite.hEvent);
- CloseHandle(m_ovread.hEvent);
- }
- //---------------------------------------------------------------------------
- bool InitPort(LPCSTR comm)
- {
- if (m_portopen)
- return m_portopen;
- bool result = false;
- hCommDevice = CreateFile(comm,
- GENERIC_WRITE|GENERIC_READ,
- 0, /* comm devices must be opened w/exclusive-access */
- NULL, /* no security attrs */
- OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
- FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, /* not overlapped I/O */
- NULL /* hTemplate must be NULL for comm devices */
- ); /* handle error */
- if(hCommDevice == INVALID_HANDLE_VALUE)
- {
- ProcessErrorMessage("INVALID_HANDLE_VALUE\r\n");
- printf(" open failed\r\n");
- result = false;
- }
- else
- {
- printf(" open success\r\n");
- OpenCloseFlag = 1;
- //Get the config of the modem connection
- GetCommState(hCommDevice, &dcb);
- //Set to 8 bits, no parity, 1 stopbit
- dcb.BaudRate = 115200;
- dcb.ByteSize = 8;
- dcb.Parity = 0;//EVENPARITYNOPARITY;
- dcb.StopBits = ONESTOPBIT;
- dcb.fDtrControl = DTR_CONTROL_ENABLE;//DTR_CONTROL_DISABLE;//1;
- dcb.fRtsControl = RTS_CONTROL_ENABLE;//RTS_CONTROL_DISABLE;//1;
- dcb.fBinary = 1;
- dcb.fParity = FALSE;
- dcb.fOutxCtsFlow = FALSE;
- //Actually set the config
- SetCommState(hCommDevice, &dcb);
- if (!SetupComm(hCommDevice, 256, 256))
- {
- CloseHandle(hCommDevice);
- result = false;
- }
- else
- {
- //Get timeout information for the communications channel
- GetCommTimeouts(hCommDevice, &ct);
- ct.ReadIntervalTimeout=100;
- ct.ReadTotalTimeoutMultiplier=0;
- ct.ReadTotalTimeoutConstant=100;
- ct.WriteTotalTimeoutMultiplier=50;
- ct.WriteTotalTimeoutConstant=100;
- SetCommTimeouts(hCommDevice, &ct);
- COMSTAT com;
- DWORD Errors;
- ClearCommError(hCommDevice,&Errors,&com);
- if (com.cbInQue>0)
- PurgeComm(hCommDevice,PURGE_RXCLEAR|PURGE_TXCLEAR);
- SetCommMask(hCommDevice,EV_RXCHAR);
- result = true;
- m_portopen = true;
- }
- }
- return result;
- }
- //---------------------------------------------------------------------------
- bool WritePort(PCVOID lpszSendBuffer,DWORD dwLength)
- {
- BOOL fWriteStat = 0;
- DWORD dwBytesWritten = 0;
- DWORD dwErrorFlags = 0;
- DWORD dwBytesSent=0;
- COMSTAT ComStat;
- // HANDLE m_hWriteEvent;
- // ResetEvent(m_hWriteEvent);
- ClearCommError( hCommDevice, &dwErrorFlags, &ComStat ) ;
- PurgeComm(hCommDevice,PURGE_RXCLEAR|PURGE_TXCLEAR);
- m_ovwrite.Offset=0;
- fWriteStat = WriteFile(hCommDevice, lpszSendBuffer,dwLength,&dwBytesWritten, &m_ovwrite) ;
- if (!fWriteStat)
- {
- if(GetLastError() == ERROR_IO_PENDING)
- {
- if (!GetOverlappedResult(hCommDevice,&m_ovwrite, &dwBytesWritten, true))
- {
- ClearCommError( hCommDevice, &dwErrorFlags, &ComStat ) ;
- return FALSE;
- }
- dwBytesSent += dwBytesWritten;
- if( dwBytesSent != dwLength)
- return FALSE;
- else
- return TRUE;
- }//Io pending Error
- else
- {
- ClearCommError( hCommDevice, &dwErrorFlags, &ComStat ) ;
- PurgeComm(hCommDevice,PURGE_RXCLEAR|PURGE_TXCLEAR);
- return FALSE;
- }
- }
- return TRUE ;
- }
- char Rxbuff[RX_SIZE];
- DWORD WINAPI Thread_Tx(LPVOID nParam)
- {
- unsigned char buff[1024];//= "hello world\r\n";
- char input[2],tmp;
- int i=0;
- printf("%s","/***********************************************\r\n");
- printf("%s","Please press '1' to Test CMD 0x01----Set Fed Prama\r\n");
- while(hCommDevice != INVALID_HANDLE_VALUE)
- {
- memset(buff,0x00,sizeof(buff));
- memset(buff,0x00,sizeof(input));
- i = 0;
- printf("\r\n ");
- while(1){
- scanf("%c",&tmp);
- if(tmp!='\n'&&tmp!=' '){
- input[i]=tmp;
- if(i<1) i++;
- }
- else
- break;
- }
- switch(input[0])
- {
- case '1':
- buff[0]=0x01;
- buff[1]=0x02;
- buff[2]=0x03;
- buff[3]=0x04;
- buff[4]=0X05;
- buff[5]=0X06;
- buff[6]=0X07;
- buff[7]=0X08;
- buff[8]=0X09;
- buff[9]=0X0a;
- buff[10]=0X0b;
- buff[11]=0X0c;
- buff[12]=0X0d;
- buff[13]=0X0e;
- WritePort(buff,14);
- break;
- default : printf("0x0c cmd :Error cmd \r\n ");break;
- }
- printf("%02X cmd :\r\n ",buff[2]);
- for(i=0;i<buff[3]+4;i++)
- printf("0x%02X ",buff[i]);
- printf("\r\n ");
- Sleep(100);
- }
- return 0;
- }
- void Deal_buff(unsigned char *buff,DWORD cnt)
- {
- unsigned char *p =buff;
- while(cnt)
- {
- cnt--;
- printf("0x%02X",*p++);// printf("%c",*p);
- printf(" ");
- }
- printf("\r\n");
- }
- DWORD WINAPI Thread_Rx(LPVOID nParam)
- {
- BOOL bRead =TRUE;
- BOOL bResult =TRUE;
- DWORD dwError =0;
- DWORD BytesRead ;
- COMSTAT ComStat ;
- unsigned char Rxbuff[RX_SIZE];
- for(;;)
- {
- ClearCommError( hCommDevice, &dwError, &ComStat);
- if(ComStat.cbInQue==0){
- continue;
- }
- if (bRead){
- bResult = ReadFile(hCommDevice, // Handle to COMM port
- &Rxbuff,// RX Buffer Pointer
- RX_SIZE,//RX_SIZE// Read one byte
- &BytesRead,//
- &m_ovread);
- // printf("0x%02X",Rxbuff[0]);
- // printf(" ");
- if(!bResult){//读不成功
- switch (dwError = GetLastError())
- {
- case ERROR_IO_PENDING:
- bRead = FALSE;
- break;
- default: break;
- }
- } if(bResult){//读成功
- bRead = TRUE;
- }
- }if (!bRead) {
- bRead = TRUE;
- bResult = GetOverlappedResult(hCommDevice, // Handle to COMM port
- &m_ovread, // Overlapped structure
- &BytesRead, // Stores number of bytes read
- TRUE); // Wait flag
- if(BytesRead)//bResult
- {
- printf("Recived = %ld Bytes ",BytesRead);
- Deal_buff(Rxbuff,BytesRead);
- memset(Rxbuff,0x00,sizeof(Rxbuff));
- }
- }
- }
- }
- int main ()
- {
- InitPort("com5");
- CreateThread(NULL ,0,Thread_Tx,0,0,NULL);
- CreateThread(NULL ,0,Thread_Rx,0,0,NULL);//Thread_Rx
- while(1);
- return 0;
- }
复制代码 |
|