Hello Everybody!
I am new to Serial programming, And I have to make a programm that receives data from a microcontroler. The problem is that the number that it receives are a little diferent from the ones I sent. For example, i send 1 every 0.1, it receives and prints 1.. 10... 1... 10... How do I solve this?
Any hint would be helpful!
I used a timer to sincronize receiving events;
CODE:
[...]
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
/
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int gwtstat = 0;
int I;
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 x[300]={0},y[300]={0},z[300]={0},v[300]={0};
int vetor1[500],vetor2[500];
PAINTSTRUCT ps;
HDC hdc;
TEXTMETRIC tm; TCHAR teste[]={"Exame:"};
int tx;
DWORD num;
byte in[2]={0};
switch (message)
{
case WM_CREATE:
AddMenus(hwnd);
TextBox = CreateWindow("EDIT",
"",
WS_BORDER | WS_CHILD | WS_VISIBLE,
10, 40,400,20,
hwnd,NULL,NULL,NULL);
CreateWindow("BUTTON",
"SAVE",
WS_VISIBLE | WS_CHILD | WS_BORDER,
420, 40, 70, 20,
hwnd, (HMENU) 1, NULL, NULL);
CreateWindow("BUTTON",
"START",
WS_VISIBLE | WS_CHILD | WS_BORDER,
420, 80, 70, 20,
hwnd, (HMENU) 2, NULL, NULL);
CreateWindow("BUTTON",
"STOP",
WS_VISIBLE | WS_CHILD | WS_BORDER,
300, 80, 70, 20,
hwnd, (HMENU) 6, NULL, NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
FillRect(hdc,&ps.rcPaint,(HBRUSH)(COLOR_WINDOW+1));
Rectangle(hdc,10,110,500,300);
for (I=0;I<50;I++){
SetPixel(hdc,I,I,RGB(255,0,0));
}
TextOut(hdc,1,1,teste,strlen(teste));
EndPaint(hwnd,&ps);
break;
case WM_TIMER:
tx = ReadFile(hCom,&in,1,&num,NULL);
printf("%x\t",(in[0]));
hdc = GetDC(hwnd);
SetPixel(hdc,i,in[0],RGB(255,0,0));
ReleaseDC(hwnd,hdc);
i++;
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 = ODDPARITY; dcb.StopBits = ONESTOPBIT;
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
::MessageBox(hwnd,ConnectionError,ConnectionError,MB_OK);
return (3);
}
::MessageBox(hwnd,SuceText,SuceText,MB_OK);
SetTimer(hwnd,1,0x1,NULL);
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:
KillTimer(hwnd,1);
if (CloseHandle(hCom)==0){
::MessageBox(hwnd,ErrorText,NULL,MB_OK);
}
else{
::MessageBox(hwnd,stopedcon,stopedcon,MB_OK);
}
break;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}