#include "serial.h" // port - quel port utiliser (1-4) Serial::Serial(int port) { COM[0] = 'C'; COM[1] = 'O'; COM[2] = 'M'; COM[3] = '0'+port; COM[4] = 0; intbuf = new char[1025]; memset(intbuf, 0, 1025); } Serial::~Serial() { CloseHandle(hCOM); delete intbuf; } // Serial::Setup - opens and initializes the COM port to specified settings // returns false if something went wrong; use GetLastError() to find out specifics // baud - the baud rate to use; default is 9600 // bits - number of bits/byte; default is 8 // parity - whether or not to use parity; default is NOPARITY (look up "DCB" in the help file for constants) // stopbits - number of stopbits; default is ONESTOPBIT (look up "DCB" in the help file for constants) // flowcontrol - one of HARDWARE (using RTS/CTS), XONXOFF, or NOFLOW; default is HARDWARE // inbuf/outbuf - input and output buffer sizes; default is 128 bool Serial::Setup(int baud, int bits, int parity, int stopbits, int flowcontrol, int inbuf, int outbuf) { DCB dcb; if( (hCOM = CreateFile( COM, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL))==INVALID_HANDLE_VALUE) return false; if( !SetupComm(hCOM, inbuf, outbuf) ) return false; if( !GetCommState(hCOM, &dcb) ) return false; dcb.BaudRate = baud; dcb.ByteSize = bits; dcb.fParity = parity; dcb.StopBits = stopbits; switch( flowcontrol ) { case HARDWARE: dcb.fRtsControl = RTS_CONTROL_ENABLE; dcb.fInX = false; dcb.fOutX = false; break; case XONXOFF: dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fInX = true; dcb.fOutX = true; break; case NOFLOW: dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fInX = false; dcb.fOutX = false; break; } if( !SetCommState(hCOM, &dcb) ) return false; return true; } bool Serial::WriteBuffer(char *buf, int size) { DWORD res; if( !WriteFile(hCOM, buf, size, &res, NULL) ) return false; if( res != size ) return false; return true; } // reads a buffer from COM port until a specified character is found // guarantees that last character is the character specified in cr (or buffer is filled), and stores anything read after that // basically, therefore, this function returns one line of text read from the serial port // input size is max size of buffer, output of size is total bytes read // maximum buffer size is 1024. sorry. bool Serial::WaitForChar(char *buf, int &size, char cr) { bool err = false; bool done = false; char *end; DWORD res; int ofs = 0, len, bytes; memset(buf, 0, size); if( size > 1024 ) size = 1024; if( end=strchr(intbuf, cr) ) { len = (unsigned long)end - (unsigned long)intbuf + 1; bytes = len >= size ? size : len; memmove(buf, intbuf, bytes); memmove(intbuf, &intbuf[bytes], 1025 - bytes); // memset(&intbuf[bytes], 0, 1025-bytes); size = bytes; return true; } else { strncpy(buf, intbuf, size); ofs = strlen(buf); } do { if( !ReadFile(hCOM, &buf[ofs], size-ofs, &res, NULL) ) { done = true; err = true; } else { if( end=strchr(&buf[ofs], cr) ) { done = true; strncpy(intbuf, end+1, 1024); end[1] = 0; } else ofs += res; } } while( !done || ofs==size ); if( err ) return false; else return true; }