#include "stdafx.h"
#include <windows.h>
#define IDC_MAIN_BUTTON 101
#define IDC_MAIN_EDIT 102
#define IDC_MAIN_EDIT2 103
#define IDC_MAIN_EDIT3 104
#define IDC_MAIN_EDIT4 105
HWND hEdit, hEdit2, hEdit3, hEdit4;
DCB dcb;
HANDLE hNumPort;
char lpBuffor_read[ 33 ], lpBuffor_write[ 33 ];
DWORD RS_ile;
char tabw[ 4 ], tabr;
LPSTR buf;
LRESULT CALLBACK WinProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
int dlugos;
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd )
{
WNDCLASSEX wClass;
ZeroMemory( & wClass, sizeof( WNDCLASSEX ) );
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof( WNDCLASSEX );
wClass.cbWndExtra = NULL;
wClass.hbrBackground =( HBRUSH ) COLOR_WINDOW;
wClass.hCursor = LoadCursor( NULL, IDC_ARROW );
wClass.hIcon = NULL;
wClass.hIconSm = NULL;
wClass.hInstance = hInst;
wClass.lpfnWndProc =( WNDPROC ) WinProc;
wClass.lpszClassName = "Window Class";
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW | CS_VREDRAW;
if( !RegisterClassEx( & wClass ) )
{
int nResult = GetLastError();
MessageBox( NULL,
"Window class creation failed\r\n",
"Window Class Failed",
MB_ICONERROR );
}
HWND hWnd = CreateWindowEx( NULL,
"Window Class",
"NOWE RS232 TEST (bajt 1,bajt 2,bajt 3,bajt 4)",
WS_OVERLAPPEDWINDOW,
200,
200,
640,
480,
NULL,
NULL,
hInst,
NULL );
if( !hWnd )
{
int nResult = GetLastError();
MessageBox( NULL,
"Window creation failed\r\n",
"Window Creation Failed",
MB_ICONERROR );
}
ShowWindow( hWnd, nShowCmd );
MSG msg;
ZeroMemory( & msg, sizeof( MSG ) );
while( GetMessage( & msg, NULL, 0, 0 ) )
{
TranslateMessage( & msg );
DispatchMessage( & msg );
}
return 0;
}
LRESULT CALLBACK WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CREATE:
{
hEdit = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 50, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT, GetModuleHandle( NULL ), NULL );
hEdit2 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 150, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT2, GetModuleHandle( NULL ), NULL );
hEdit3 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 250, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT3, GetModuleHandle( NULL ), NULL );
hEdit4 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | ES_NUMBER, 350, 50, 25, 20, hWnd,( HMENU ) IDC_MAIN_EDIT4, GetModuleHandle( NULL ), NULL );
HGDIOBJ hfDefault = GetStockObject( DEFAULT_GUI_FONT );
SendMessage( hEdit, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
SendMessage( hEdit2, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
SendMessage( hEdit3, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
SendMessage( hEdit4, WM_SETFONT,( WPARAM ) hfDefault, MAKELPARAM( FALSE, 0 ) );
HWND hWndButton = CreateWindowEx( NULL,
"BUTTON",
"Wyślij",
WS_TABSTOP | WS_VISIBLE |
WS_CHILD | BS_DEFPUSHBUTTON,
50,
220,
100,
24,
hWnd,
( HMENU ) IDC_MAIN_BUTTON,
GetModuleHandle( NULL ),
NULL );
SendMessage( hWndButton,
WM_SETFONT,
( WPARAM ) hfDefault,
MAKELPARAM( FALSE, 0 ) );
hNumPort = CreateFile( "COM1", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
dcb.DCBlength = sizeof( dcb );
dcb.BaudRate = CBR_9600;
dcb.fParity = FALSE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = DTR_CONTROL_ENABLE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDsrSensitivity = FALSE;
dcb.fAbortOnError = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
if( SetCommState( hNumPort, & dcb ) ) MessageBox( NULL, "Udało się połączyć z portem COM1", "Inf", MB_ICONINFORMATION );
else MessageBox( NULL, "Nie udało się połączyć z portem COM1", "Błąd", MB_ICONERROR );
}
break;
case WM_COMMAND:
switch( LOWORD( wParam ) )
{
case IDC_MAIN_BUTTON:
{
dlugos = GetWindowTextLength( hEdit ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit, buf, dlugos + 1 ); tabw[ 0 ] = atoi( buf ) % 256;
dlugos = GetWindowTextLength( hEdit2 ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit2, buf, dlugos + 1 ); tabw[ 1 ] = atoi( buf ) % 256;
dlugos = GetWindowTextLength( hEdit3 ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit3, buf, dlugos + 1 ); tabw[ 2 ] = atoi( buf ) % 256;
dlugos = GetWindowTextLength( hEdit4 ); buf =( LPSTR ) GlobalAlloc( GPTR, dlugos + 1 ); GetWindowText( hEdit4, buf, dlugos + 1 ); tabw[ 3 ] = atoi( buf ) % 256;
WriteFile( hNumPort, tabw, 4, & RS_ile, 0 );
MessageBox( NULL,
"Danej zostały wysłane",
"Information",
MB_ICONINFORMATION );
}
break;
}
break;
case WM_DESTROY:
{ CloseHandle( hNumPort );
PostQuitMessage( 0 );
return 0;
}
break;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
I już nie mam problemu z wysyłaniem danych - program działa.
Teraz chciałbym dodać do tego odbierania danej.
Program który napisałem dla mikrokontrolera wysyła bajt o wartości 'A' kiedy wymuszę stan niski na określnym jego wejściu.
W terminalu znak A faktycznie się pokazuje.
Mój program PC ma wyświetlić messagebox'a z informacją gdy przyjdzie do niego kod znaku A.
PYTANIA: 1)Jak dokłądnie działa ReadFile - chodzi mi o to czy jeśli nic nie odbiera to zatrzymuje wątek aż się dana pojawi?
2)Jak najoptymalniej dodać powyższą funkcjonalność do mojego programu? Byłbym wdzięczny gdyby ktoś dodał :)