Linux and Windows serial communication

Source: Internet
Author: User
Tags posix readfile

Serial port is commonly used in the data transmission between the computer and the external serial device, because the serial communication is convenient and easy to use, so widely used. Now the international serial port new technology and new specifications launched, in combination with all aspects of social needs, the development of serial communication space is huge. Serial communication technology because of its own advantages and characteristics, and the extensive application of computer technology into the life and production of various fields, the world's hundreds of millions of communications equipment in the serial communication. The data is being transmitted. In an application system, the use of Windows and Linux operating system, the rational allocation of resources, each to take the director, is to achieve the high performance of the system effective way. In order to enable two different operating systems to work together for resource and data sharing, it is necessary to communicate in both questions. This topic studies the serial Communication program design of Linux operating system and window operating system. The main modules of serial communication program design are visual c++6 respectively. 0 programming environment and Unux development. In this paper, the similarities and differences of serial communication programs in two programming environments and their comparisons are also given.

1. Development environment
1. 1 Hardware environment
A PC capable of running Windows 9X/2000/XP and Linux systems, with two serial ports or two PCs with serial ports, RS232 serial cable.
1. 2 Software Environment
Visual c++6. 0,GCC Programmer, WINDOWS9X/2000/XP operating system and LIUNX operating system.
1. 3 schematic diagram of the design
PCI runs the Windows system, PCII runs the Linux system. They enable serial communication across platforms via the RS232 serial line connection.

2. Programming of serial Communication in Windows platform
2 1 Open the serial port
Embedded software development, system integration. Using the CreateFile function to open the string El handle, first see that the serial port handle in the class is handle In-hcomm, the handle after the application of the CreateFile () function is created, the specified serial port handle is obtained, after which almost all functions to operate the serial port, Must be done through Iti-hcomm. Because the serial port handle is opened, the asynchronous (overlapping) I/O operation mode is set, the time-out of the serial port must be explained, and the timeout variable is set.
2. 2 Setting up the serial port
After opening the communication device handle, it is often necessary to do some initialization configuration work on the serial port. This needs to be done through an DCB structure. The DCB structure contains information such as baud rate, number of data bits, parity, and stop bits. The DCB structure is used as a buffer when querying or configuring the properties of the serial port. After you open the serial port with CreateFile, you can call the Getcommstate function to get the initial configuration of the serial port. To modify the configuration of the serial port, the DCB structure should be modified before calling the SetCommState function to set the serial port.
2. 3 Creating Threads
Write the data in the main thread, because the write is controllable, and when you read it you do not know when the data will arrive, so create a thread specifically for the read thread. In this thread, the ReadFile function is used to read the serial port, and the Waitcommevent function is used to detect the line status. There are many functions needed to establish a read thread: ReadFile, if you want to detect communication status, such as CTS signal, RINGLN and so on, then use Setcommmask,waitcommevent,clearcommerror. In the Serialpor class, the monitoring thread function is the Commthread function. In the Serialport::commthread () function: First use the PurgeComm function to clear the buffer and invoke the communication event monitoring function. When an event is monitored, if the asynchronous operation cannot be completed immediately, the function returns False (0), and the GetLastError function returns Error-io-pending, indicating that the operation is performed in the background. When this occurs, the system sets the member Hevent parameter value in the overlapped structure to no signal state before the Waitcommevent function returns, and then waits for a specific communication event or an error to be set to a signaled state. So whenever a character arrives, it generates an event. When a read event is detected, the Receivechar function is called to receive the data, and when the event is detected, the Writechar function is called to send the data. Call the EnterCriticalSection function to obtain the critical section of the serial port to ensure that no other functions or threads in this process use this serial resource. Use the ReadFile function to read out the number of bytes in the buffer, ReadFile (HANDLE nl-hcom,lpvoid rxbuff,dword dwlenth. lpdword&bytesread,lpoverlapped
MOV); The parameter Rxbuff points to the receive buffer, Dwlenth indicates the number of bytes to read from the serial port, where 1,bytesread indicates the number of bytes actually read from the serial device, Iti-ov points to the asynchronous i/0 structure.
2. 4 Read/write serial
Use ReadFile and WriteFile read and write serial port, in the use of ReadFile and WriteFile read and write serial port, can be synchronous execution, can also overlap execution. When executed synchronously, the function is not returned until the operation is complete. This means that threads are blocked during synchronous execution, resulting in a decrease in efficiency. When overlapped, both functions return immediately, even if the operation is not completed, and the time-consuming i/0 operation is performed in the background. The ReadFile function simply reads a person's specified number of characters in the serial input buffer, even if the operation is complete. The WriteFile function is not only to copy the specified number of characters into the output buffer, but also to wait for these characters to be sent out from the serial port to complete the operation.
If the operation succeeds, both functions return true. It is important to note that when ReadFile and WriteFile return false. Not necessarily that the operation failed, the thread should call the GetLastError function to parse the returned results. For example, the function returns False if the action is not completed when the operation is overlapped, and the GetLastError function returns error-io-pending.
2. 5 Closing the serial port
Call the Closeport () function to close the serial port.

3, the Linux platform for the serial communication program design
3. 1 Open the serial port
in Linux under the serial port file is a bit =j=/dev, the serial port L is/DEV/TTYSO, serial port 2 is/DEV/TTYSL, by calling Open () opening the serial device, return a shaping file handle, all the port operation is for this handle. If an open () error occurs, it is returned. 1. The open () function has 2 parameters, where parameter 1 is the name of the device to open, and Parameter 2 is open.
3. 2 Set the serial port
to operate serial port operation, first of all the serial port communication baud rate, check bit, output and other parameters to set the mode. The serial port settings under Linux are actually set according to the POSIX specification. The POSIX specification is proposed by the IEEE to define a range of portable operating system interfaces. All parameters of the serial port settings are included in the Termios structure. Linux provides two functions to operate on Termios, which are tcgetattr () and tcsetattr (). The Tcgetattr () function obtains the settings of the specified serial port, and the tesetattr () function is used to set the parameters of the serial port.
3. 3 read-write serial port
Serial port configuration is completed, the serial port as a file to read and write. Call the Write () function to read and write the port, return the actual number of bytes read and write, and return 11 if an error occurs. Write the data in the main thread, because the write is controlled, write
(fd,&writedata,sizeof (WriteData)), and read the time does not know when the data will arrive, so to establish a thread specifically to read the data, in this thread, Read the serial port by using read in the loop. Close the serial port close (FD) at the end of the program. After the serial port configuration is completed, the serial port as a file to read and write. Calls the read () function read-write port, returns the actual number of bytes read and write, and returns 11 if an error occurs.
3. 4 Close the serial port
Close the serial port is to close the file. Use Close () to close the open serial port, the only parameter is to open the serial port of the file descriptor.

4, Windows and Linux serial communication similarities and differences
4. 1 windows and Linux serial communication the same point
Windows and Linux start automatically detect the standard serial device, and the opening, closing, reading and writing of the serial port function and the function of the operation file is the same. A few simple steps to access the serial port are:
(1) Open the serial device, get handle;
(2) Set the serial communication parameters (Windows is set structure body Dcb,linux is set structure Termios), serial communication initialization settings, including data bits, baud rate, Parameters such as flow control,
(3) Prepare to send/Receive data,
(4) Send/Receive data,
(5) Close the serial device used.
In addition, there are many xwindows development tools under Linux.
4. 2 windows and Linux serial communication different points
in Linux to implement serial communication programming and the Windows environment is not the same: The Linux serial port combines a lot of terminal features (for example, in the I,iy mode will automatically handle some character terminal control), There's a big difference in how Windows works. When you add an extended multi-port serial communication card, Windows adds new hardware through the Control Panel, and Linux is configured with/ete/serial in a system-initiated configuration script. The Conf file is completed. When opening the serial port, Windows will need to provide the logical name of the serial port, such as coml;linux need to provide the path and name of the serial port, and case-sensitive, female fl/dev/ttyso. Linux supports none,even,odd and space four checks, while Windows also supports mask. Under Linux, Parity Generation and detection (PARENB) and input parity test (INPCK) are two different things: the generation and detection of parity bits produces odd and even bits of output characters, and the input parity test verifies the parity of the input characters. The stop bit for Windows is only 1 bits, 1. 5-bit and 2-bit three kinds, Linux does not have 1. 5 bits. Linux supports different baud rates for input and output and can be set separately. When transferring files over a serial port, note that the line terminator in Linux is a newline character (ASCIll3), and the carriage return Line (ASCIIIO/ASCILL3) under Windows.
Reference Link: http://d.g.wanfangdata.com.cn/Periodical_kxjsygc200903041.aspx

Linux and Windows serial communication

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.