int CSeries::SerOpen(int PortNo, char * param) { DCB dcb; BOOL fRetVal ; BYTE i,bSet; DWORD dwError; COMMTIMEOUTS to; char baudr[128] = {0}; TCHAR DevName[10]; wsprintf(DevName,"\\\\.\\COM%01d",(int)PortNo); SerClose(); if ((m_CommPort=CreateFile(DevName , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , NULL , NULL )) == INVALID_HANDLE_VALUE) { return -1; } // set up DCB memset(&dcb, 0, sizeof(dcb)); dcb.DCBlength = sizeof(dcb) ; strcpy(baudr, (char *)param); if(!BuildCommDCBA(baudr, &dcb)) { SerClose(); return -1; } if(!SetCommState(m_CommPort, &dcb)) { SerClose(); return(-1); } to.ReadIntervalTimeout = MAXDWORD; to.ReadTotalTimeoutMultiplier = 0; to.ReadTotalTimeoutConstant = 0; to.WriteTotalTimeoutMultiplier = 0; to.WriteTotalTimeoutConstant = 0; if(!SetCommTimeouts(m_CommPort, &to) ) { SerClose(); return(-1); } return 0; }
static int COMM_CLOSE(SIO_INFO *s) { if (s!=NULL) { if( s->fd!=BAD_FD ) { SerReceiveFlush( s->fd, 0 ); SerSetReceiveBuffer( s->fd, NULL, 0 ); SerClose( s->fd ); } s->fd=BAD_FD; return(1); } else { return(-1); } }
static int COMM_OPEN(SIO_INFO *s, char *dev) { Err e; UInt fd; if( strcmp( dev, "1" ) ) return( BAD_FD ); e = SysLibFind("Serial Library", &fd ); if( e ) return( BAD_FD ); e = SerOpen( fd, 0, 9600 ); if( e==serErrAlreadyOpen ) { SerClose( fd ); return( BAD_FD ); } else if( e==serErrBadParam ) { return( BAD_FD ); } return(fd); }
int CSeries::SerOpen(int PortNo, long BaudRate, int Parity, int ByteSize) { CString MsgStr; DCB dcb; BOOL fRetVal ; BYTE i,bSet; DWORD dwError; COMMTIMEOUTS to; TCHAR DevName[10]; wsprintf(DevName,"\\\\.\\COM%01d",(int)PortNo); SerClose(); if ((m_CommPort=CreateFile(DevName , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , NULL , NULL )) == INVALID_HANDLE_VALUE) { return -1; } for(i=0; i<5; i++) { /*设置缓冲区大小*/ fRetVal = SetupComm(m_CommPort, MAX_BUFFER_IN ,MAX_BUFFER_OUT); if (fRetVal) { break; } Sleep(100); } // purge any information in the buffer PurgeComm(m_CommPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); // set up for overlapped I/O GetCommTimeouts(m_CommPort, &to); to.ReadIntervalTimeout = 0xFFFFFFFF; to.ReadTotalTimeoutMultiplier =0; to.ReadTotalTimeoutConstant =0; to.WriteTotalTimeoutMultiplier =10;//10ms to.WriteTotalTimeoutConstant =5000;//5000ms SetCommTimeouts(m_CommPort, &to) ; // set up DCB dcb.DCBlength = sizeof(DCB) ; for(i=0; i<5; i++) { fRetVal =GetCommState(m_CommPort, &dcb); if (fRetVal) { break; } Sleep(100); } if (fRetVal == 0) { dwError = GetLastError(); CloseHandle(m_CommPort); m_CommPort = INVALID_HANDLE_VALUE; return -2; } dcb.BaudRate=BaudRate; dcb.ByteSize=ByteSize; dcb.Parity = Parity; dcb.StopBits =ONESTOPBIT;//2009-10-29 // setup hardware flow control #if 2 bSet = false; dcb.fOutxDsrFlow = bSet ; if (bSet) dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; else dcb.fDtrControl = DTR_CONTROL_ENABLE; dcb.fOutxCtsFlow = bSet ; if (bSet) dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; else dcb.fRtsControl = RTS_CONTROL_ENABLE; // setup software flow control bSet = false; dcb.fInX = dcb.fOutX = bSet ; dcb.XonLim = 100 ; dcb.XoffLim = 100 ; dcb.XonChar = XON; dcb.XoffChar = XOFF; dcb.fBinary = TRUE; if (dcb.Parity == 0) dcb.fParity = FALSE; else dcb.fParity = TRUE; #else /**/ dcb.fOutxCtsFlow = FALSE; // No CTS output flow control dcb.fOutxDsrFlow = FALSE; // No DSR output flow control dcb.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type dcb.fDsrSensitivity = FALSE; // DSR sensitivity dcb.fTXContinueOnXoff = TRUE; // XOFF continues Tx dcb.fOutX = FALSE; // No XON/XOFF out flow control dcb.fInX = FALSE; // No XON/XOFF in flow control dcb.fErrorChar = FALSE; // Disable error replacement dcb.fNull = FALSE; // Disable null stripping dcb.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control dcb.fAbortOnError = FALSE; // 当串口发生错误,并不终止串口读写 #endif // set up DCB dcb.DCBlength = sizeof( DCB ); for(i=0; i<5; i++) { //Sleep(80); fRetVal = SetCommState(m_CommPort, &dcb); if (fRetVal) break; Sleep(100); } if (fRetVal == 0) { dwError = GetLastError(); // an error occurred, try to recover MsgStr.Format("<CE-%u>,Please Check ComPort", dwError); MessageBox(NULL, MsgStr, "SetCommState", MB_OK); CloseHandle(m_CommPort); m_CommPort = INVALID_HANDLE_VALUE; return -3; } GetCommState(m_CommPort, &dcb ) ; // EscapeCommFunction(m_CommPort, SETDTR ) ; PurgeComm(m_CommPort , PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; return 0; }