Code:
#include <string>
#include <cstring>
#include <stdlib.h>
#include <afx.h>
#include "SerialWnd.h"
#include "Resource.h"
using namespace std;
HDC hdc ;
DCB dcb;
HANDLE hCom;
char buff[2];
char buffout[2];
HDC MemDCExercising;
PAINTSTRUCT Ps;
HBITMAP bmpExercising;
HINSTANCE hInst;
BOOL fSuccess;
unsigned long dwBytesRead = 0;
BYTE abBuffer[100];
char *pcCommPort = "COM1";
DWORD dwCommEvent;
DWORD dwMask;
DWORD inqueue;
DWORD outqueue;
int TractionData=0;
int WLF=0;
int WLR=0;
int WRF=0;
int WRR=0;
char fleft[26] = "Front Left Wheel Speed: ";
char fright[27] = "Front Right Wheel Speed: ";
char rleft[25] = "Rear Left Wheel Speed: ";
char rright[26] = "Rear Right Wheel Speed: ";
char tstatus[19] = "Traction Status: ";
CSerialWnd serial;
LRESULT CALLBACK Handler(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
if (iMsg == WM_PAINT)
{
hdc = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hdc);
// Select the new bitmap
SelectObject(MemDCExercising, bmpExercising);
// Copy the bits from the memory DC into the current dc
BitBlt(hdc, 0, 0, 782, 522, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
DeleteDC(MemDCExercising);
DeleteObject(bmpExercising);
EndPaint(hWnd, &Ps);
hdc = GetDC(hWnd);
SetTextColor(hdc, RGB(0,0,0));
//SetBkColor(hdc, RGB(0,0,0));
TextOut(hdc,385,435,fleft,26);
TextOut(hdc,5,175,fright,27);
TextOut(hdc,600,350,rleft,25);
TextOut(hdc,190,100,rright,26);
TextOut(hdc,330,30,tstatus,19);
}
if (iMsg == CSerialWnd::mg_nDefaultComMsg)
{
// A serial message occurred
const CSerialWnd::EEvent eEvent = CSerialWnd::EEvent(LOWORD(wParam));
const CSerialWnd::EError eError = CSerialWnd::EError(HIWORD(wParam));
switch (eEvent)
{
case CSerialWnd::EEventRecv:
hdc = GetDC(hWnd);
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(0,0,0));
serial.Read(abBuffer,1,&dwBytesRead,0,INFINITE);
sprintf(buffout,"%X",(LPCTSTR)abBuffer[0]);
SetTextColor(hdc, RGB(255,255,255));
//TextOut(hdc,0,80,buffout,2);
// Sets flags based on qualifiers
if ((buffout[0] == '1') && (buffout[1] == '0')) {
TractionData = 1;
break;
}
if ((buffout[0] == '2') && (buffout[1] == '0')) {
WLF = 1;
break;
}
if ((buffout[0] == '3') && (buffout[1] == '0')) {
WLR = 1;
break;
}
if ((buffout[0] == '4') && (buffout[1] == '0')) {
WRF = 1;
break;
}
if ((buffout[0] == '5') && (buffout[1] == '0')) {
WRR = 1;
break;
}
// Displays data based on flags
if (TractionData == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,434,30,buffout,1);
TractionData = 0;
break;
}
if (WLF == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,547,435,buffout,2);
WLF = 0;
break;
}
if (WLR == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,756,350,buffout,2);
WLR = 0;
break;
}
if (WRF == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,177,175,buffout,2);
WRF = 0;
break;
}
if (WRR == 1) {
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
TextOut(hdc,360,100,buffout,2);
WRR = 0;
break;
}
break;
}
return 0;
}
if (iMsg == WM_DESTROY) {
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow){
//Declare variables
const char* szAppName = "MyWin";
WNDCLASSEX wndclass;
HWND hWnd;
MSG msg;
hInst = hInstance;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = Handler;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hWnd = CreateWindow(szAppName, "Wireless Telemetry Monitor", WS_OVERLAPPEDWINDOW,0,0, 782, 522, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
serial.Open(pcCommPort, hWnd, WM_NULL, 0, inqueue, outqueue);
serial.Setup(CSerial::EBaud2400,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
serial.SetupHandshaking(CSerial::EHandshakeOff);
serial.SetMask(CSerial::EEventRecv);
serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);
while (GetMessage(&msg, NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}