// Greg Stitt, Ann Gordon-Ross // // Tony Givargis // //----------------------------------------------------------------------------- #include "stdafx.h" #include #include "port.h" #include "special.h" //----------------------------------------------------------------------------- Port::Port(unsigned port, unsigned baud) : DefaultTimeout(1000) { char buf[32]; DCB commState; sprintf(buf, "COM%i", port); if( (handle = CreateFile(buf, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, /*FILE_FLAG_OVERLAPPED, */ // 95 FILE_ATTRIBUTE_NORMAL, // NT 0)) == INVALID_HANDLE_VALUE ) { handle = 0; } else { GetCommState(handle, &commState); commState.BaudRate = baud; commState.Parity = NOPARITY; commState.StopBits = ONESTOPBIT; commState.fDtrControl = DTR_CONTROL_DISABLE; commState.fRtsControl = RTS_CONTROL_DISABLE; commState.ByteSize = 8; SetCommState(handle, &commState); EscapeCommFunction(handle, SETDTR); EscapeCommFunction(handle, SETRTS); SetTimeout(DefaultTimeout); } } //----------------------------------------------------------------------------- Port::~Port() { if( handle ) { CloseHandle(handle); } } //----------------------------------------------------------------------------- void Port::SetTimeout(unsigned ms) { COMMTIMEOUTS timeout; if( handle ) { GetCommTimeouts(handle, &timeout); timeout.ReadIntervalTimeout = 0; timeout.ReadTotalTimeoutMultiplier = 0; timeout.ReadTotalTimeoutConstant = ms; SetCommTimeouts(handle, &timeout); } } //----------------------------------------------------------------------------- unsigned Port::Write(void* buf, unsigned szBuf) { DWORD check = 0; if( handle ) { WriteFile(handle, buf, szBuf, &check, 0); } return check; } //----------------------------------------------------------------------------- unsigned Port::Read(void* buf, unsigned szBuf) { DWORD check = 0; if( handle ) { ReadFile(handle, buf, szBuf, &check, 0); } return check; } /**************************************************/ // Greg and Ann's code // sends a character void Port::SendUart(unsigned char d) { // send the character Write(&d, 1); Sleep(25); } // calculates a checksum and sends a character void Port::SendChar(unsigned char d) { static int counter = 0; static unsigned char CheckSum = 0; if (counter == 0) SendUart(BEGIN_PACKET); if (d == '\0') { // fills packet with \0 if eof is reached in the // middle of a packet for(; counter < PACKET_SIZE; counter ++) { CheckSum ^= '\0'; SendUart('\0'); } SendUart(CheckSum); Sleep(75); counter = 0; CheckSum = 0; } else { // sends character CheckSum ^= d; SendUart(d); counter ++; if (counter == PACKET_SIZE) { SendUart(CheckSum); Sleep(75); counter = 0; CheckSum = 0; } } } // sends an entire string void Port::SendStringUart(const char* s) { while( *s ) { SendChar(*s++); } SendChar('\0'); }