A long time ago, I was prepared to learn how to implement serial communication in the VC environment. Today, we finally took the first step, and successfully sent the information through the serial port. This is a small personal mileage card.
CodeAs follows:
// ================================================ ========================================================== ========
# Include <windows. h>
# Include <windowsx. h>
# Include "Main. H"
# Include "dialogs. H"
# Include "resource. H"
# Include "stdio. H"
Handle hcom;
DWORD dwerror;
DCB lpdcbcom;
// Compile Error Using lpdcb
Char scombuf [256];
Void openscom ()
{
Hcom = createfile ("COM7 ",\
Generic_read | generic_write ,\
0 ,\
Null ,\
Open_existing,
File_attribute_normal | file_flag_overlapped ,\
Null
);
If (hcom = invalid_handle_value)
{
If (5 = getlasterror ())
{
Sprintf (scombuf, "% s", "Serial Port occupied ");
MessageBox (null, scombuf, null, mb_ OK );
}
// Sprintf (scombuf, "% u", dwerror );
}
Getcommstate (hcom, & lpdcbcom );
Buildcommdcb ("baud = 9600 parity = n data = 8 stop = 1", & lpdcbcom );
Setcommstate (hcom, & lpdcbcom );
}
Void initscom ()
{
}
Bool winapi main_proc (hwnd, uint umsg, wparam, lparam)
{
Switch (umsg)
{
// Begin message Crack
Handle_msg (hwnd, wm_initdialog, main_oninitdialog );
Handle_msg (hwnd, wm_command, main_oncommand );
Handle_msg (hwnd, wm_close, main_onclose );
// End message Crack
}
Return false;
}
//////////////////////////////////////// ////////////////////////////////////////
// Main_oninitdialog
Bool main_oninitdialog (hwnd, hwnd hwndfocus, lparam)
{
// Set app icons
Hicon = loadicon (hinstance) getwindowlong (hwnd, gwl_hinstance), makeintresource (idi_iconapp ));
Sendmessage (hwnd, wm_seticon, true, (lparam) hicon );
Sendmessage (hwnd, wm_seticon, false, (lparam) hicon );
//
// Add initializing code here
//
Return true;
}
//////////////////////////////////////// ////////////////////////////////////////
// Main_oncommand
Void main_oncommand (hwnd, int ID, hwnd hwndctl, uint codenostrap)
{
Switch (ID)
{
Case idc_send:
// MessageBox (hwnd, "test", "test", mb_ OK );
Transmitcommchar (hcom, 'J ');
Break;
Case idc_ OK:
// MessageBox (hwnd, text ("You clicked OK! "), Text (" scom "), mb_ OK );
Openscom ();
// Enddialog (hwnd, ID );
Break;
Case idc_cancel:
// MessageBox (hwnd, text ("You clicked cancel! "), Text (" scom "), mb_ OK );
Closehandle (hcom );
// Enddialog (hwnd, ID );
Break;
Default: break;
}
}
//////////////////////////////////////// ////////////////////////////////////////
// Main_onclose
Void main_onclose (hwnd)
{
Enddialog (hwnd, 0 );
}
// End of File
// ================================================ ========================================================== ========
Some of these problems are not clear for the moment. The first line in the original code uses this section "lpdcb lpdcbcom;" and sets "getcommstate (hcom, & lpdcbcom ); "" & lpdcbcom "in this and later sections is changed to" lpdcbcom ",ProgramAfter running, an error is reported when the serial port is opened.
Later, follow the tutorial and change the section to the current version "lpdcb lpdcbcom.
Why?