Vc++6.0 in serial communication development method - Database & Sql Blog Articles

TG5500CA VC-TCXO
Spot Remarks 2017+

1 Introduction In today's industrial control field, serial communication is a commonly used method for data communication between computers and other devices. It has several advantages such as simple implementation, flexible use, and reliable data transmission, especially in real-time monitoring systems. The serial port used on the computer we use is generally rs232. The rs232 interface can only communicate one-to-one. However, in the industrial control field, an industrial computer and multiple intelligent devices are often used for communication, and the transmission distance is required. Because of these needs, rs485 is generally used in the industrial control field.
In win32, you can use two programming methods to achieve serial communication. One is to use the mscomm control. This method is simple but not flexible. The second is to call the api function of windows. This method can clearly grasp the mechanism of serial communication and is free and flexible. The method of using the control is also essentially using the api for serial communication. The control is only a package processing for the api. This article only introduces the method of using the api for serial communication programming.

2 General steps for serial communication
2.1
Open the serial port in 32-bit windows. The serial port and other communication devices (such as disks) are processed as files. They must be opened before use. To ensure the reliability of serial communication data transmission, the serial port is generally in non-shared mode. Open, that is, after being opened by the serial port, other programs cannot open the device.
2.2
Configure the serial port to be correctly configured before using the serial port for data communication. The main parameters that need to be configured on the serial port are baud rate, data bit, stop bit, parity, and data buffer size. In addition, the serial port should be timed out to prevent the data transmission from being suddenly interrupted during serial communication, causing the read/write operation to enter an indefinite waiting state. The timeout is set. If the operation is not completed within the specified time, Then this operation is automatically abandoned.
2.3
After the serial port is opened and set up, the serial port can be used to read and write data. The read and write data can be synchronized, asynchronous, and event driven.
2.4
Close the serial port and close it after using the serial port. If it is not closed, the serial port will be open, and other applications will not be able to open the serial port.

3 using the api function to achieve serial communication
3.1 Open the serial port
The win32 system extends the concept of files. Whether it is a file, a communication device, a named pipe, a mail slot, a disk, or a console, it is opened or created with the api function createfile. The prototype of this function is:
Handle createfile( lpctstr lpfilename,
Dword dwdesiredaccess,
Dword dwsharemode,
Lpsecurity_attributes lpsecurityattributes,
Dword dwcreationdistribution,
Dword dwflagsandattributes,
Handle htemplatefile);
The various parameters are described as follows:
Lpfilename: the logical name of the serial port to be opened, such as "com1";
Dwdesiredaccess: specifies the type of serial port access, which can be read, written, or both.
Dwsharemode: Specifies the shared attribute. Since the serial port cannot be shared, this parameter must be set to 0.
Lpsecurityattributes: reference security attribute structure, the default value is null;
Dwcreationdistribution: Creates a flag. For serial port operation, this parameter must be set to open_existing;
Dwflagsandattributes: Attribute description, used to specify whether the serial port performs asynchronous operations. The value is file_flag_overlapped, which means that asynchronous i/o is used; the value is 0, indicating synchronous i/o operation;
Htemplatefile: This parameter must be set to null for the serial port;
3.2 Configuring the Serial Port After opening the communication device handle, you need to perform some initial configuration on the serial port. This needs to be done through a dcb structure. The dcb structure contains information such as baud rate, number of data bits, parity, and number of stop bits. When querying or configuring the properties of a serial port, use the dcb structure as a buffer. After opening the serial port, you can call the getcommstate function to get the default configuration of the serial port. This function gets a dcb structure, as long as the dcb structure should be modified first in the structure, and then the setcommstate function is called to set the serial port with the modified dcb structure. Dcb mainly has the following important members: byte bytesize; // communication byte number byte parity; // specifies the parity method. This member can have the following values: ://evenparity even parity noparity no parity // markparity tag check oddparity odd check byte stopbits; // specify the number of stop bits. This member can have the following values: ://onestopbit 1 stop bit //twostopbits 2 stop bits //one5stopbits 1.5 stop bits In addition to using bcd to set some basic parameters of the serial port, you generally need to set the size of the serial port send and receive data buffer. And timeout, the effect of the timeout is that the specified number of characters are not read or sent within the specified time, and the read and write operations will still end. Windows uses the i/o buffer to temporarily store the serial input and output data. If the communication rate is high, a larger buffer should be set. We can use the api function setupcomm to set the size of the input and output buffers of the serial port. The prototype is as follows: bool setupcomm ( handle hfile, // serial port handle dword dwinqueue, // input buffer size (bytes) dword dwoutqueue ); // size of the output buffer (number of bytes)
Regarding the timeout setting of the read/write serial port, Windows provides us with a special structure commtimeouts, which is defined as follows: typedef struct _commtimeouts { dword readintervaltimeout; // read interval timeout dword readtotaltimeoutmultiplier; //read time coefficient dword readtotaltimeoutconstant; //read time Constant dword writetotaltimeoutmultiplier; // write time coefficient dword writetotaltimeoutconstant; //write time constant} commtimeouts, *lpcommtimeouts;
Members of the commtimeouts structure are in milliseconds. The formula for calculating the total timeout is:
Total timeout = time coefficient × number of characters required to be read/written + time constant For example, to read in 10 characters, the total timeout of the read operation is calculated as:
Read total timeout = readtotaltimeoutmultiplier × 10 + readtotaltimeoutconstant
Through the structure windowsapi provides us with two functions: getcommtimeouts and setcommtimeouts, the former obtains the current timeout setting, and the latter uses the modified commtimeouts to set the timeout, similar to setting the serial port.
Before reading and writing the serial port, use the purgecomm(...) function to clear the buffer. The function prototype:
Bool purgecomm(
Handle hfile, //serial handle
Dword dwflags ); // The operation parameter dwflags that needs to be completed specifies the operation to be completed, which can be a combination of the following values:
Purge_txabort interrupts all writes and returns immediately, even if the write operation has not completed.
Purge_rxabort interrupts all reads and returns immediately, even if the read operation has not completed.
Purge_txclear clear output buffer
Purge_rxclear clear input buffer
3.3 read and write serial port read and write serial port use readfile and writefile two functions, the prototype is as follows: bool readfile (handle hfile, / / ​​serial port handle lpvoid lpbuffer, / / ​​save the pointer to read data, dword nnumberofbytestoread, / / ​​to read The number of bytes of data lpdword lpnumberofbytesread, //the actual number of bytes read lpoverlapped lpoverlapped ); // overlapped, the synchronization is null
Bool writefile(
Handle hfile, // handle of the serial port
Lpcvoid lpbuffer, // the address to write data to
Dword nnumberofbytestowrite, // the number of bytes to write data to
Lpdword lpnumberofbyteswritten, //the actual number of bytes written
Lpoverlapped lpoverlapped); // overlapped, synchronously null. When doing synchronous operations, the read and write functions wait until the execution is finished, but the function returns immediately when the operation is asynchronous, but the read and write operations are not guaranteed. The structure is asynchronously controlled. The structure has an important member hevent. The member is the handle of the windows event object. It is commonly used to control thread synchronization and asynchronous operations. If it is an asynchronous operation, we can create event objects using createevent(...) and Assign the return value to the hevent, and then use waitforsingleobject or getoverlappedresult to wait for the read and write operations to complete, thus achieving the purpose of controlling the asynchronous operation. 3.4 Turn off the serial port When you do not use the serial port, you should close it to release the resources of windows for other programs. To close the serial port, just call closehandle (hcomm/* serial port handle */). 4 Serial communication in the Century Star configuration software as a general configuration software, Century Star to communicate with other devices such as plc, smart meters, serial communication is one of the main ways, based on the use of api The advantages of serial communication development, and considering the convenience and reusability of program development, in the Century Star, we encapsulate the serial communication API, and operate the serial port in a class manner, in which the serial port is opened and the serial port parameters are configured. The operation is set by the visualization window, and then implemented in the package class. The related operations are basically realized by processing the read and write data. Because different devices have different protocols, the operation of reading and writing the serial port is completed in the driver. In this way, our developers do not have to pay attention to too many other related operations, just rewrite the member functions of the serial port according to the actual device. 5 Conclusion Windows is the mainstream platform for current application development. vc++6.0 is a powerful development tool for the platform. Using windowsapi to develop serial communication programs allows us to understand the mechanism of serial communication more clearly, and developers can use it as needed. The api is flexible in programming, and serial communication is an indispensable technology in scada, so it is practical to master the development method of serial communication.

Consumer Electronic Cable Assembly

Consumer electronic Cable Assembly: The product is mainly used in the signal, power, power transmission machine control inside the home appliance.
Common consumer electronic cable assemblies include air conditioner power wiring harnesses, water dispenser wiring harnesses, computer internal power cords, coffee machines, egg beaters and other signal lines, TV wiring harnesses and other product wiring harnesses that we can call white goods. The wiring harness is the main body of the circuit network. There is no home appliance circuit without a home Wire Harness . At present, whether it is a high-end luxury home appliance or an economical ordinary home wire harness, the braided form is basically the same, which is composed of wires, connectors and wrapping tapes. Home appliance wires, also known as low-voltage wires, are different from ordinary household wires. Ordinary household wires are copper single-core wires with a certain hardness. The consumer electronic wire harnesses are all copper multi-core soft wires, some soft wires are as thin as a few hairs or even dozens of soft copper wires are wrapped in a plastic insulating tube (polyvinyl chloride), which is soft and not easy to break. Common specifications of wires in consumer electronics harnesses include wires with a nominal cross-sectional area of 0.5, 0.75, 1.0, 1.5, 2.0, 2.5, 4.0, 6.0, etc. square millimeters, each of which has an allowable load current value for use in different power electrical equipment. Different gauge wires.

The consumer electronics connection cable harness industry has had a profound impact on people's lives, cutting into all aspects of life from market conditions, industry services, service conditions, and market scale. Therefore, Kable-X has made an in-depth understanding of the consumer electronics industry to provide customers with better wiring harness.


In addition to Instrumentation Cable Assembly, we also have Vehicle cable assembly and Industrial Cable Assembly.

Consumer Electronic Cable Assembly



Consumer Electronic Cable Assembly,Power Cord Cable Assembly,Power Cord Cable For Sale,Cable Assembly Prototyping

Kable-X Technology (Suzhou) Co., Ltd , https://www.kable-x-tech.com