// Listener.cpp - Sample application for CSerial // // Copyright (C) 1999-2003 Ramon de Klein (Ramon.de.Klein@ict.nl) // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #define STRICT #include #include #include #include #include #include #include "Serial.h" #define LEVELXBIAS 17 #define LEVELYBIAS 17 int ShowError (LONG lError, LPCTSTR lptszMessage) { // Generate a message text TCHAR tszMessage[256]; wsprintf(tszMessage,_T("%s\n(error code %d)"), lptszMessage, lError); // Display message-box and return with an error-code ::MessageBox(0,tszMessage,_T("Listener"), MB_ICONSTOP|MB_OK); return 1; } struct STilt { float x, y; } tilt; STilt ReadTilt(CSerial &serial) { static STilt tilt = { 0, 0 }; static int stage = 0; // 0 is ping, 1 wait for ping // 2 is channel 0, 3 is wait for channel 0 // 4 is request, 5 is wait for request // 6 is channel 1, 7 is wait for channel 1 // 8 is request, 9 is wait for request // back to 2 static time_t ltime = time(NULL); // we also watch the time if it dies after a second we will know int oldstage=stage; LONG lLastError; if ((stage%2)==0) // send { if (stage==0) serial.Write("P"); if (stage==2) serial.Write("0"); if (stage==4 || stage == 8) serial.Write("7"); if (stage ==6) serial.Write("1"); stage++; } else { // Read data, until there is nothing left DWORD dwBytesRead = 0; BYTE szBuffer; // Read data from the COM-port lLastError = serial.Read(&szBuffer,sizeof(szBuffer),&dwBytesRead); if (lLastError != ERROR_SUCCESS) { exit(::ShowError(serial.GetLastError(), _T("Unable to read from COM-port."))); } if (dwBytesRead == 1) { int blue = szBuffer; // we go from 127 to -128 to 0 if (blue < 0) blue = 128 + (128 - abs(blue)); if (stage==5) tilt.x = (float)(blue - 128 + LEVELXBIAS)/30.0f; if (stage==9) { tilt.y = (float)(blue-128+LEVELYBIAS)/30.0f; stage = 2;} else stage++; } else if (stage==3 || stage==7) stage++; } if (oldstage != stage) { ltime = time(NULL); } else { time_t diff = time(NULL)-ltime; if (diff>1000L) exit(::ShowError(serial.GetLastError(), _T("My error timeout"))); } return tilt; } int __cdecl _tmain (int /*argc*/, char** /*argv*/) { CSerial serial; LONG lLastError = ERROR_SUCCESS; // Attempt to open the serial port (COM1) lLastError = serial.Open(_T("COM2"),0,0,false); if (lLastError != ERROR_SUCCESS) return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port")); // Setup the serial port (9600,8N1, which is the default setting) lLastError = serial.Setup(CSerial::EBaud38400,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); if (lLastError != ERROR_SUCCESS) return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting")); // Register only for the receive event lLastError = serial.SetMask(CSerial::EEventRecv); if (lLastError != ERROR_SUCCESS) return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port event mask")); // Use 'non-blocking' reads, because we don't know how many bytes // will be received. This is normally the most convenient mode // (and also the default mode for reading data). lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); if (lLastError != ERROR_SUCCESS) return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port read timeout.")); DWORD dwBytesRead = 0; char szBuffer[101]; while (!kbhit()) { tilt = ReadTilt(serial); printf("%+02.3f %+02.3f\n", tilt.x, tilt.y); Sleep(10); } // Close the port again serial.Close(); return 0; }