UART communication in windows desktop application

Hello

I want to make a windows desktop application https://docs.microsoft.com/en-us/cpp/windows/walkthrough-creating-windows-desktop-applications-cpp

that receives data by UART and displays data in window. Data is received by USB to UART bridge https://www.silabs.com/documents/public/data-sheets/CP2102-9.pdf
That is working fine. I can see the data coming in in some terminal program. Data should be received once a second. It contains 15 two digit numbers.

I have it working really nice in console application. It displays numbers every time when they are received.

But I have some problems with win32 API program. It misses about 6 or 7 data "packs". I call data pack one series of those 15 numbers. So that means my win32 program receives data only once every 7, 8 seconds.

I must say that I am beginner in win32 API programming and I don't even know how to put stuff in main loop when there is main message loop going on.

Since it's not really coding issue I will put a summary of my code here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include "stdafx.h"
#include <Windows.h>
#include "WindowsProject1.h"
#include <stdio.h>

//variable declaration

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
//UART initialization
//500ms timer initialization

for (; ; ) {
// Main message loop:
		
		while (GetMessage(&msg, nullptr, 0, 0))
		{
			if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
				
			}
		}
	}

}

//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.

ATOM MyRegisterClass(HINSTANCE hInstance)



//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)



//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message){

case WM_TIMER:
if (WaitCommEvent(hComm, &dwEventMask, NULL))
//read UART data and store it


 case WM_PAINT:
 PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
TCHAR V1[20];
...
...
...
_stprintf_s(V1, TEXT("%.1f m"), Altitude);
				TextOut(hdc, 150, 20, V1, _tcslen(V1));
...
...
...

EndPaint(hWnd, &ps);
}


}


If I try and put "if (WaitCommEvent(hComm, &dwEventMask, NULL))" in main message loop than it's even slower. Oh and also, while data is received the mouse cursor switches to round icon that means something is loading. And I can't move the window for those few seconds. I don't think it should be like that.
What am I doing wrong?

If you need more code I will be happy to post it, but for now I would not put too much of it for the sake of simplicity.
Last edited on
...so _stprintf_s works like printf but for that white box that opens? I'm still trying to figure out the windows mode too.
Topic archived. No new replies allowed.