VC cainiao-VC serial programming, I am shy of you

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.