#include #include #include #include "timer.h" // commands: // 'A' is a multi-use test (65) // 'B' is take a snapshot (66) // 'C' is retrieve the snapshot (67) DCB oldDcb; HANDLE hComPort; BOOL InitializeComPort(char *szPort) { COMMTIMEOUTS to; DCB dcb; hComPort = CreateFile(szPort, GENERIC_READ|GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0, NULL); if(!hComPort || (hComPort == INVALID_HANDLE_VALUE)) return FALSE; SetupComm(hComPort, 4096, 4096); GetCommState(hComPort, &dcb); memcpy(&oldDcb, &dcb, sizeof(DCB)); BuildCommDCB("baud=9600 parity=N data=8 stop=1", &dcb); dcb.fBinary = TRUE; dcb.fNull = FALSE; SetCommState(hComPort, &dcb); memset(&to, 0, sizeof(to)); to.ReadIntervalTimeout = MAXDWORD; to.ReadTotalTimeoutMultiplier = MAXDWORD; to.ReadTotalTimeoutConstant = 2000; to.WriteTotalTimeoutMultiplier = 150; SetCommTimeouts(hComPort, &to); PurgeComm(hComPort, PURGE_TXCLEAR); PurgeComm(hComPort, PURGE_RXCLEAR); return TRUE; } void CleanupComPort() { SetCommState(hComPort, &oldDcb); CloseHandle(hComPort); } void SendChar(char c) { DWORD dwWrote; WriteFile(hComPort, &c, 1, &dwWrote, NULL); } BOOL ReceiveChar(char *pc) { DWORD dwRead = 0; ReadFile(hComPort, pc, 1, &dwRead, NULL); if(dwRead != 1) return FALSE; return TRUE; } unsigned char cBmpHeader[1078] = { 0x42, 0x4d, 0x76, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x04, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x00, 0x08, 0x08, 0x08, 0x00, 0x0c, 0x0c, 0x0c, 0x00, 0x10, 0x10, 0x10, 0x00, 0x14, 0x14, 0x14, 0x00, 0x18, 0x18, 0x18, 0x00, 0x1c, 0x1c, 0x1c, 0x00, 0x20, 0x20, 0x20, 0x00, 0x24, 0x24, 0x24, 0x00, 0x28, 0x28, 0x28, 0x00, 0x2c, 0x2c, 0x2c, 0x00, 0x30, 0x30, 0x30, 0x00, 0x34, 0x34, 0x34, 0x00, 0x38, 0x38, 0x38, 0x00, 0x3c, 0x3c, 0x3c, 0x00, 0x40, 0x40, 0x40, 0x00, 0x44, 0x44, 0x44, 0x00, 0x48, 0x48, 0x48, 0x00, 0x4c, 0x4c, 0x4c, 0x00, 0x50, 0x50, 0x50, 0x00, 0x54, 0x54, 0x54, 0x00, 0x58, 0x58, 0x58, 0x00, 0x5c, 0x5c, 0x5c, 0x00, 0x60, 0x60, 0x60, 0x00, 0x64, 0x64, 0x64, 0x00, 0x68, 0x68, 0x68, 0x00, 0x6c, 0x6c, 0x6c, 0x00, 0x70, 0x70, 0x70, 0x00, 0x74, 0x74, 0x74, 0x00, 0x78, 0x78, 0x78, 0x00, 0x7c, 0x7c, 0x7c, 0x00, 0x80, 0x80, 0x80, 0x00, 0x84, 0x84, 0x84, 0x00, 0x88, 0x88, 0x88, 0x00, 0x8c, 0x8c, 0x8c, 0x00, 0x90, 0x90, 0x90, 0x00, 0x94, 0x94, 0x94, 0x00, 0x98, 0x98, 0x98, 0x00, 0x9c, 0x9c, 0x9c, 0x00, 0xa0, 0xa0, 0xa0, 0x00, 0xa4, 0xa4, 0xa4, 0x00, 0xa8, 0xa8, 0xa8, 0x00, 0xac, 0xac, 0xac, 0x00, 0xb0, 0xb0, 0xb0, 0x00, 0xb4, 0xb4, 0xb4, 0x00, 0xb8, 0xb8, 0xb8, 0x00, 0xbc, 0xbc, 0xbc, 0x00, 0xc0, 0xc0, 0xc0, 0x00, 0xc4, 0xc4, 0xc4, 0x00, 0xc8, 0xc8, 0xc8, 0x00, 0xcc, 0xcc, 0xcc, 0x00, 0xd0, 0xd0, 0xd0, 0x00, 0xd4, 0xd4, 0xd4, 0x00, 0xd8, 0xd8, 0xd8, 0x00, 0xdc, 0xdc, 0xdc, 0x00, 0xe0, 0xe0, 0xe0, 0x00, 0xe4, 0xe4, 0xe4, 0x00, 0xe8, 0xe8, 0xe8, 0x00, 0xec, 0xec, 0xec, 0x00, 0xf0, 0xf0, 0xf0, 0x00, 0xf4, 0xf4, 0xf4, 0x00, 0xf8, 0xf8, 0xf8, 0x00, 0xfc, 0xfc, 0xfc, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, }; int main(int argc, char *argv[]) { char szInBuf[100]; char cRet; DWORD dwMicrosecs; char *szPort = "COM1"; unsigned char cImage[1600]; int n, m; BOOL bSuccess; FILE *fp; if(!InitializeComPort(szPort)) { printf("Cannot initialize %s\n", szPort); return 0; } while(1) { ResetTimer(); printf("Command: A=test B=snapshot C=retrieve q=quit > "); fflush(stdout); gets(szInBuf); if(szInBuf[0] == 'q') break; else if(szInBuf[0] == 'A') { // just a test StartTimer(); SendChar(szInBuf[0]); if(!ReceiveChar(&cRet)) { dwMicrosecs = GetTimer(); printf("failed to receive response. Time %d microsecs\n", dwMicrosecs); continue; } dwMicrosecs = GetTimer(); printf("Sent %d Received %d time %d microseconds\n", (int) szInBuf[0], (int) cRet, dwMicrosecs); } else if(szInBuf[0] == 'B') { // tell the board to take a snapshot StartTimer(); SendChar(szInBuf[0]); if(!ReceiveChar(&cRet)) { printf("failed to receive response\n"); continue; } dwMicrosecs = GetTimer(); printf("Snapshot taken. Sent %d Received %d time %d microseconds\n", (int) szInBuf[0], (int) cRet, dwMicrosecs); } else if(szInBuf[0] == 'C') { // fetch and save the image bSuccess = TRUE; StartTimer(); SendChar(szInBuf[0]); for(n = 0; n < 1600; n++) cImage[n] = 0xFF; // start with solid background for(n = 0; n < 32; n++) { for(m = 0; m < 32; m++) { if(!ReceiveChar((char *) (&cImage[(39-n)*40 + m]))) { printf("FAILURE: %d bytes received\n", n); bSuccess = FALSE; break; } } } if(bSuccess) { dwMicrosecs = GetTimer(); printf("Image transferred. Time: %d microseconds\n", dwMicrosecs); fp = fopen("bing.bmp", "wb"); if(!fp) { printf("Unable to write to bing.bmp\n"); } else { fwrite(cBmpHeader, sizeof(cBmpHeader), 1, fp); fwrite(cImage, 1600, 1, fp); fclose(fp); printf("Image saved to bing.bmp\n"); } } } } CleanupComPort(); return 0; }