Hello!
I want to plot serial data using the Win32 api. However, the SetPixel function is ploting only part of the data. It doesnt plot anything below a certain imaginary line of the window, over that line it works perfectly. Does anybody know what the problem is?
I believe that the problem is in the second thread. There is a certain point of the window that the SetPixel function doesn't plot data anymore. I also believe that it has to do with the way windows reapaints the window.
Thanks!
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define BUTTON1 1
#define BUTTON2 2
#define IDM_FILE_NEW 3
#define IDM_FILE_OPEN 4
#define IDM_FILE_QUIT 5
#define STOP 6
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND textfield;
HWND TextBox;
FILE* fp;
HANDLE hCom;
HANDLE Handlethread;
DCB dcb;
BOOL fSuccess;
DWORD WINAPI THREAD1(LPVOID lpParam);
HWND hwnd;
DWORD ExitCode = 0;
int *lpArgPtr;
void AddMenus(HWND);
char textSaved[20];
char textSaved2[]={"Connecting."};
HDC hdc;
TCHAR szClassName[ ] = _T("EXAME DE SINCOPE VASOVAGAL");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = "Menu";
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("EXAME DE SÍNCOPE VASOVAGAL"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
544,
700,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int gwtstat = 0;
char * t = &textSaved[0];
char pcCommPort[50] = "\\\\.\\COM4";
char SuceText[] = {"Connected"};
char stopedcon[]= {"Connection Stoped"};
char ErrorText[] = {"Error Connecting"};
char ConnectionError[] = {"Port Not Connected"};
int j;
PAINTSTRUCT ps;
TEXTMETRIC tm; TCHAR teste[]={"Exame:"};
switch (message)
{
case WM_CREATE:
AddMenus(hwnd);
TextBox = CreateWindow("EDIT",
"",
WS_BORDER | WS_CHILD | WS_VISIBLE,
10, 540,400,20,
hwnd,NULL,NULL,NULL);
CreateWindow("BUTTON",
"SAVE",
WS_VISIBLE | WS_CHILD | WS_BORDER,
420, 540, 70, 20,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow("BUTTON",
"START",
WS_VISIBLE | WS_CHILD | WS_BORDER,
420, 580, 70, 20,
hwnd, (HMENU) 2, NULL, NULL);
CreateWindow("BUTTON",
"STOP",
WS_VISIBLE | WS_CHILD | WS_BORDER,
300, 580, 70, 20,
hwnd, (HMENU) 6, NULL, NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
FillRect(hdc,&ps.rcPaint,(HBRUSH)(COLOR_WINDOW+1)); SelectObject(hdc, GetStockObject( NULL_BRUSH ) );
for (j=10;j<500;j++){
SetPixel(hdc,j,0,RGB(0,0,0));
}
for (j=10;j<500;j++){
SetPixel(hdc,j,255,RGB(0,0,0));
}
for (j=10;j<500;j++){
SetPixel(hdc,j,510,RGB(0,0,0));
}
TextOut(hdc,1,515,teste,strlen(teste)); EndPaint(hwnd,&ps);
break;
case WM_COMMAND:
switch (LOWORD(wParam)){
case BUTTON1:
gwtstat = GetWindowText(TextBox,t, 20);
::MessageBox(hwnd,textSaved,textSaved,MB_OK);
fopen("info.txt","a+");
fwrite(teste,1,sizeof(teste),fp);
break;
case BUTTON2:
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL );
if (hCom == INVALID_HANDLE_VALUE)
{
::MessageBox(hwnd,ConnectionError,ConnectionError,MB_OK);
return (1);
}
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{ return (2);
}
dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = TWOSTOPBITS;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
::MessageBox(hwnd,ConnectionError,ConnectionError,MB_OK);
return (3);
}else{
Handlethread = CreateThread(NULL,0,THREAD1,lpArgPtr,0,NULL);
}
::MessageBox(hwnd,SuceText,SuceText,MB_OK);
break;
case IDM_FILE_NEW:
Beep(100, 100);
break;
case IDM_FILE_OPEN:
Beep(50, 100);
break;
case IDM_FILE_QUIT:
SendMessage(hwnd,WM_CLOSE,0,0);
break;
case STOP:
if (CloseHandle(hCom)==0){
::MessageBox(hwnd,ErrorText,NULL,MB_OK);
}
else{
::MessageBox(hwnd,stopedcon,stopedcon,MB_OK);
}
break;
}
case WM_DESTROY:
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
void AddMenus(HWND hwnd)
{
HMENU hMenubar;
HMENU hMenu;
hMenubar = CreateMenu();
hMenu = CreateMenu();
AppendMenuW(hMenu,MF_STRING, IDM_FILE_NEW, L"&New");
AppendMenuW(hMenu,MF_STRING, IDM_FILE_OPEN, L"&Open");
AppendMenuW(hMenu,MF_SEPARATOR, 0,NULL);
AppendMenuW(hMenu,MF_STRING, IDM_FILE_QUIT, L"&Quit");
AppendMenuW(hMenubar, MF_POPUP, (UINT_PTR) hMenu, L"&File");
SetMenu(hwnd, hMenubar);
}
DWORD WINAPI THREAD1(LPVOID lpParam){
printf("I am inside the thread\n");
int i = 0;
int y = 0;
bool tx;
char PlotData[500] = {0};
DWORD num;
byte in[100]={0};
while(1){
tx = ReadFile(hCom,&in,2,&num,NULL);
if (tx==0||num!=2){
printf("ERROR");
if (CloseHandle(hCom)==0){
ExitThread(ExitCode);
}
}else{
printf("%x\t",(in[0]));
}
if(i>=500){
for(y=1;y<500;y++){
hdc = GetDC(hwnd);
SetPixel(hdc,y,PlotData[y],RGB(255,255,255));
ReleaseDC(hwnd,hdc);
PlotData[y-1] = PlotData[y];
PlotData[499] = (255 - in[0]);
}
for(y=0;y<500;y++){
hdc = GetDC(hwnd);
SetPixel(hdc,y,PlotData[y],RGB(255,0,0));
ReleaseDC(hwnd,hdc);
}
}else if (i<500){
PlotData[i] = (255 - in[0]);
i++;
}
}
return NULL;
}