Program Implementation of RS-232-C serial port monitoring software

Source: Internet
Author: User
Program Implementation of RS-232-C serial port monitoring software

(1) interface style

Because it is a real-time monitoring software, it is necessary to monitor the real-time data from the peripherals, but also through the serial port to send some specific instructions to the peripherals to control the peripherals to complete the preset action. To facilitate sending commands to the serial port, you can add a dialog bar similar to the "Internet Explorer" style on the toolbar. You can specify the "Internet Explorer rebars" style when creating a project, you can also add the "dialog bar" component that comes with Microsoft Visual C ++ 6.0. To display the data read from the outside to the management personnel in a timely manner, and keep a considerable record for reference, you can select the List View.

(2) Set and enable serial port Parameters

Parameter configuration for serial port of RS-232-C is necessary for serial port communication. In addition, different serial ports are configured for different occasions, purposes, and functions. In order to make the program more flexible and more adaptable, all possible parameters are pre-set in several combo boxes. You can change the settings at any time after the program runs. Customize a Data Structure for setting serial port parameters:

Typedef struct tagcom_config
{
Int nport; file: // port number, from COM1 to com4
Int nbaud; file: // baud rate, from 1200bps to 57600bps (the corresponding macro is cbr_1200 to cbr_57600)
Int ndata; file: // number of data bits, 7 or 8 bits
Int nstop; file: // Number of Stop bits, which can be 1, 1.5, or 2 bits.
Int nparity; // check method, whether there is a check (noparity ),
File: // oddparity and evenparity.
} Com_config;

After selecting the appropriate parameters, you can open the communication port based on the configured port configuration. Unlike the serial communication programs in DOS, Windows does not recommend that applications directly control hardware (including ports) or interrupt the use of hardware (unless it reaches the ring0 system level ), instead, data is transmitted through the device driver provided by the Windows operating system. In Windows, the serial port is processed as a file like other communication ports, rather than directly operating on the port, win 32 provides the corresponding file I/O functions and communication functions. By understanding the use of these functions, You can compile a communication program that meets different needs. The communication device-related structures include commconfig, commp drop, commtimeouts, COMSTAT, DCB, modemdevcaps, and modemsettings. There are 26 communication-related Windows API functions, for more information, see the msdn help file. The following are some key code for enabling the serial port:

// Open the file as a file and save the returned port handle in the idcomdev directory.
Idcomdev = createfile (g_szcom_port [g_com_config.nport],
Generic_read | generic_write,
0,
Null,
Open_existing,
File_attribute_normal,
Null );
......
File: // CFG is an instance object in the commconfig structure to obtain the status of the current communication port.
Cfg. DCB. dcblength = sizeof (DCB );
Getcommstate (idcomdev, & (CFG. DCB ));
File: // set the size of the sending and receiving cache.
Setupcomm (idcomdev, 4096,409 6 );
// Purgecomm () is a clearing function that can be used to stop any pending background reads or writes, and can throw I/O
File: // buffer. Specifically: purge_txabort is used to stop the background write operation; pruge_rxabort is used to stop the background
File: // read operation; pruge_txclear is used to clear the sending buffer; pruge_rxclear is used to clear the receiving buffer
Purgecomm (idcomdev, purge_txabort | purge_rxabort | purge_txclear | purge_rxclear );
File: // fill in the data member variables of the DCB structure object according to the set parameters
DCB. dcblength = sizeof (DCB );
Getcommstate (idcomdev, & DCB );
File: // set port communication parameters
DCB. baudrate = g_com_baud [g_com_config.nbaud];
DCB. bytesize = g_com_bytesize [g_com_config.ndata];
DCB. Parity = g_com_parity [g_com_config.nparity];
DCB. stopbits = g_com_stopbits [g_com_config.nstop];
File: // hardware Traffic Control
DCB. fdtrcontrol = dtr_control_disable;
DCB. foutxctsflow = false;
DCB. frtscontrol = rts_control_disable;
File: // software flow control
DCB. finx = DCB. foutx = false;
DCB. xonchar = (char) 0xff;
DCB. xoffchar = (char) 0xff;
DCB. xonlim = 100;
DCB. xofflim = 100;
DCB. evtchar = 0x0d;
DCB. fbinary = true;
DCB. fparity = true;

File: // timeout control settings. There are two kinds of Timeout: interval Timeout: (only useful for reading data from the port) It specifies the time to read between two characters; Total Timeout: timeout is triggered when the total time required to read or write a specific number of bytes exceeds a certain threshold. Calculation timeout can be calculated according to the formula:
File: // readtotaltimeout = (readtotaltimeoutmultiplier * bytes_to_read) +
// Readtotaltimeoutconstant
File: // writetotaltimeout = (writetotaltimeoutmuliplier * bytes_to_write) +
// Writetototaltimeoutconstant
File: // If the parameter is set to 0 when the time-out period is set, the value is infinite waiting, that is, no time-out.
Commtimeouts. readintervaltimeout = maxdword;
Commtimeouts. readtotaltimeoutmultiplier = 0;
Commtimeouts. readtotaltimeoutconstant = 0;
Commtimeouts. writetotaltimeoutmultiplier = 2*9600/DCB. baudrate;
Commtimeouts. writetotaltimeoutconstant = 25;
Setcommtimeouts (idcomdev, & commtimeouts );
File: // set the status of the communication port according to the configured DCB structure, enable the listener port, and monitor the number of incoming packets from the peripherals.
File: // The data thread comreadthreadproc.
If (setcommstate (idcomdev, & DCB ))
{
M_bcomportopen = true;
G_hcom = idcomdev;
Afxbeginthread (comreadthreadproc, null, thread_priority_normal );
Return;
}

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.