reprint Please specify from: Blog.csdn.net/mingojiang
Directory
A serial communication basis
1.1 Serial communication principle and characteristics
1.2 Transmission mode of serial communication
1.3 Synchronization technology of serial communication
1.4 Serial Interface Standard
Two API functions for serial communication
2.1 Opening the serial port
2.1.1 Whether the serial port has a drive
2.1.2 Connecting the serial port
2.1.3 Serial port logical port number greater than 10 cannot open the problem
2.2 Serial Port configuration
2.2.1 Setting the buffer size
2.2.2 Setting the serial port status
2.2.3 setting events to be notified
2.2.4 emptying the buffer
2.3 Receiving data asynchronously
Three sample code
3.1 Connecting the serial port and setting parameters
3.2 Sending and receiving data
3.3 Closing the serial port
a serial communication basis
Referring to the serial port reminds people of the same mouth, they are the computer's two more important means of communication.
serial port: also called COM port, the byte bits by the ranks of the team to transmit, each byte accounted for a fixed time long, slow, but the transmission distance, there are 9 needles and 25 needles Two, is a male socket (socket has a needle raised), currently less than 25 needle use; modem\ mouse \usb mouth \ old-fashioned camera and so are used serial port.
Parallel Port: The byte of the bits with multiple lines at the same time transmission, the speed of the serial port 8 times times, transmission distance is limited, the general computer internal data transmission in this way, the usual use of printers, scanners, etc. for 25-pin, female socket (socket 25 pinhole).
1.1 Serial communication principle and characteristics
The serial port is the encoding converter between the CPU and the serial device, and when the CPU sends data through the serial port, the byte data is queued to the serial bit, and the serial bit is converted to byte data when the serial port receives the data. Therefore, the appropriate driver must be installed.
Serial communication is characterized by low cost, and can be transmitted on the existing telephone network, which is the way the family is connected to the Internet via the telephone line. Just configure a corresponding communication interface, such as Modem. 1.2 transmission mode of serial communication
simplex: can only be transferred from one end to the other, such as only from A to B or B to a, such as watching TV, only allow television to send data to the television, do not allow television to send data to the television station. In simplex transmission mode, two communication is used, one channel transmits data, and one channel transmits control signal.
Half-duplex: allow the transmission of information, but not at the same time, such as walkie-talkie, a when speaking, B can not speak, B speaking when a can not speak.
Full Duplex: allows dual simultaneous communication, such as talking on the phone. 1.3 Synchronization technology of serial communication
After the physical connection is established, a mechanism is needed to make the other party interpret the data sent correctly, and send the bin bits after the data is sent, how the receiver recognizes the data and how to correctly assemble the correct bytes. This requires synchronous technology. Data synchronization technology generally solves the following problems:
² Determining the start time of sending data
² transmission Rate of data sent
² time required to send data
² Send time interval
3.1 Asynchronous transfer
In bytes transmission, asynchronous transmission is also called start-and-stop mode, before and after the transmitted bytes and starting and ending position, the beginning and end of the signal is high level, the receiver detects the low level signal is started to receive, the signal is signaled that the transmission is complete.
3.2 Synchronous transmission
In the data block for transmission, before and after the block plus a special byte to start and end, high transmission efficiency, high Line utilization, equipment burden is also large. 1.4 Serial Interface Standard
Common standard has rs-232c,rs-485,rs-422, among them, rs-232c is widely used in computer serial communication. The RS-232C standard requires no more than 15 meters of normal lines.
two API functions for serial communication
API function serial port programming, can use simple query mode or timing mode, can also adopt complex event-driven mode, so-called event-driven mode is when there is data in the input buffer, will automatically call a method to perform the corresponding operation. The timing method is to determine in a certain time interval that the data in the buffer is written, This method is inefficient, the query way is more backward one way. So the design of a good serial communication program is generally used event-driven, real-time, efficient, flexible and other characteristics.
The General serial communication Program is divided into the following parts:
Ø Open serial port: Open communication resources, set communication parameters, set communication events, create read, write events, enter waiting for serial message loop.
Ø read serial port information: When the serial ports occur Ev_rxchar (received the character and put into the input buffer) message read serial port, data transmission error processing, string processing such as carriage return, space and the corresponding conversion into data, if the analog data to be tested and other functions.
Ø Write serial port information: The information to be sent is written to the serial, corresponding error handling.
Ø Disconnect the serial port: Closes the event, clears the communication event, discards the communication resource, and shuts down.
2.1 Open Serial 2.1.1 Serial port whether there is driver
How to determine whether the serial port in the PC is normal, whether the driver is installed, the serial port name (logical port name) is how much. If the PC has a serial port at the same time drive normal, then in the registry HKEY_LOCAL_MACHINE\HARDWARE\DeviceMap directory, contains the character "Serial "or" VCom "entry below the value is that there can be multiple items, as shown in the following figure:
Item Serialcomm has a value----COM11, indicating that there is a usable serial port, if the directory contains characters serial or vcom under the entry does not have any value, indicates that there is no serial port or driver is not normal. Here's the code to get the serial number logical name:
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
HKEY Htestkey;
if (error_success = = RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT ("Hardware\\devicemap\\serialcomm"), 0, Key_read, & Htestkey)) {
TCHAR Achclass[max_path] = TEXT (""); Buffer for Class name
DWORD cchclassname = MAX_PATH; Size of class string
DWORD csubkeys=0; Number of subkeys
DWORD Cbmaxsubkey; Longest subkey size
DWORD Cchmaxclass; Longest class string
DWORD cValues; Number of values for key
DWORD Cchmaxvalue; Longest Value name
DWORD Cbmaxvaluedata; Longest Value data
DWORD Cbsecuritydescriptor; Size of security descriptor
FILETIME Ftlastwritetime; Last Write Time
DWORD I, RetCode;
TCHAR Achvalue[max_value_name];
DWORD cchvalue = max_value_name;
Get the class name and the value count.
RetCode = RegQueryInfoKey (
HKey,//Key handle
Achclass,//Buffer for class name
&cchclassname,//Size of class string
NULL,//Reserved
&csubkeys,//Number of subkeys
&cbmaxsubkey,//Longest subkey size
&cchmaxclass,//Longest class string
&cvalues,//number of values for this key
&cchmaxvalue, &