class Serial_IO : public CFrameBuffer { public: CString GetConnectionStr(); void ClearBuffer(); int ReadData( void *pBuffer, int nLimit ); int SendData( const unsigned char *pBuffer, int nSize );
Serial_IO::Serial_IO( HWND hWnd, int nBufferSize ) : CFrameBuffer( nBufferSize ) { // store hWnd for posting messages to main application m_hWnd = hWnd;
// read call blocks until data, write returns after a timeout of 5 secs or when data was written to port COMMTIMEOUTS CommTimeOuts; CommTimeOuts.ReadIntervalTimeout = MAXDWORD; CommTimeOuts.ReadTotalTimeoutMultiplier = 0; CommTimeOuts.ReadTotalTimeoutConstant = 0; CommTimeOuts.WriteTotalTimeoutMultiplier = 0; CommTimeOuts.WriteTotalTimeoutConstant = 5000; SetCommTimeouts( m_hIDComDev, &CommTimeOuts );
void Serial_IO::SetHWnd(HWND hWnd) { // set main application's hWnd m_hWnd = hWnd; }
int Serial_IO::SendData( const unsigned char *pBuffer, int nSize ) { BOOL bWriteStat; DWORD dwBytesWritten = 0;
// if port open and device valid if( (m_bPortOpen) && (m_hIDComDev != NULL) ) { // write data to port bWriteStat = WriteFile( m_hIDComDev, pBuffer, nSize, &dwBytesWritten, NULL );
// if successful if( !bWriteStat ) { // tell failure AfxMessageBox( "Failed to send data to serial port" ); ASSERT( NULL ); } }
// return bytes written to port return (int)dwBytesWritten; }
int Serial_IO::ReadData( void *pBuffer, int nLimit ) { BOOL bReadStatus; DWORD dwBytesRead = 0, dwErrorFlags; COMSTAT ComStat;
// if port open and device valid if( (m_bPortOpen) && (m_hIDComDev != NULL) ) { // clear any comm errors and get bytes in IN cue ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
// bytes in IN cue dwBytesRead = (DWORD) ComStat.cbInQue;
// if more bytes in que than space in buffer supplied, limit reading to max buffer space if( nLimit < (int)dwBytesRead ) dwBytesRead = (DWORD)nLimit;
// if port open and device valid if( (m_bPortOpen) && (m_hIDComDev != NULL) ) { // while data in system port buffer while( ComStat.cbInQue != 0 ) { // clear any port errors ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
// if any data if( ComStat.cbInQue != 0 ) { // read max 512 bytes ReadFile( m_hIDComDev, szBuffer, 512, &dwBytesRead, NULL ); } } }
// Open the serial port. m_hPort = CreateFile(lpszPortName, // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read/write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING, // How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute to copy
if(m_hPort == INVALID_HANDLE_VALUE) { // Could not open the port. return CANNOT_OPEN_PORT; }
PortDCB.DCBlength = sizeof(DCB);
// Get the default port setting information. GetCommState (m_hPort, &PortDCB);
// Change the DCB structure settings. PortDCB.BaudRate = CBR_57600; // Current baud PortDCB.fBinary = TRUE; // Binary mode; no EOF check PortDCB.fParity = TRUE; // Enable parity checking. PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx PortDCB.fOutX = FALSE; // No XON/XOFF out flow control PortDCB.fInX = FALSE; // No XON/XOFF in flow control PortDCB.fErrorChar = FALSE; // Disable error replacement. PortDCB.fNull = FALSE; // Disable null stripping. PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error. PortDCB.ByteSize = 8; // Number of bits/bytes, 4-8 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB structure. if(!SetCommState(m_hPort, &PortDCB)) { // Could not create the read thread. return CANNOT_CONFIGURE_PORT; }
// Retrieve the time-out parameters for all read and write operations on the port. GetCommTimeouts(m_hPort, &CommTimeouts);
// Set the time-out parameters for all read and write operations on the port. if(!SetCommTimeouts(m_hPort, &CommTimeouts)) { // Could not create the read thread. return CANNOT_SET_PORT_TIMEOUT; }
// Direct the port to perform extended functions SETDTR and SETRTS. // SETDTR: Sends the DTR (data-terminal-ready) signal. // SETRTS: Sends the RTS (request-to-send) signal. EscapeCommFunction(m_hPort, SETDTR); EscapeCommFunction(m_hPort, SETRTS);
// Create a read thread for reading data from the communication port. if(m_hReadThread = CreateThread(NULL, 0, PortReadThread, this, 0, &dwThreadID)) { CloseHandle(m_hReadThread); } else { // Could not create the read thread. return FALSE; }
***********************************************************************/ int Port::PortWrite(BYTE Byte) { DWORD dwNumBytesWritten;
if(!WriteFile(m_hPort, // Port handle &Byte, // Pointer to the data to write 1, // Number of bytes to write &dwNumBytesWritten, // Pointer to the number of bytes // written NULL)) // Must be NULL for Windows CE { // WriteFile failed. Report error. return CANNOT_WRITE_TO_PORT; }
***********************************************************************/ int Port::PortClose() { if(m_hPort != INVALID_HANDLE_VALUE) { // Close the communication port. if(!CloseHandle(m_hPort)) { return CANNOT_CLOSE_PORT; } else { m_hPort = INVALID_HANDLE_VALUE; return SUCCES; } }
return INVALID_PORT_HANDLE; }
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.