Serial Port problems in linux-general Linux technology-Linux technology and application information. For details, refer to the following section. [I = s] This post was last edited by lgcHR
Note: I am a newbie. I did not write the program myself. I just modified it a little.
First, paste my serial port program, and then explain the problem:
Receiver:
/******************* Head file define **************** *******/
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include "math. h"
# Define max_buffer_size 100/* define the max buffer */
/*************************************** ******************/
Int fd, s;
Int open_serial (int k)
{
If (k = 0)/* serial port */
{
Fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY );
Perror ("open/dev/ttyS0 ");
}
Else
{
Fd = open ("/dev/ttyS1", O_RDWR | O_NOCTTY );
Perror ("open/dev/ttyS1 ");
}
Printf ("% d \ n", fd );
If (fd =-1)
Return-1;
}
/*************************************** *****************************/
Int main (int argc, char * argv [])
{
Char hd [max_buffer_size];/* define the receive buffer */
Retv = read (fd, hd, 50);/* receive data is stored int the fd,
Retv is the receive bytes numbers */
If (retv =-1)
Perror ("error !!! ");/* Read status biaozhi judge */
Printf ("The data converted Ed is: \ n");/* output the converted ed data */
For (I = 0; I <50; I ++)
{
Printf ("% c", hd
); } Printf ("\ n "); Flag_close = close (fd ); If (flag_close =-1) Printf ("Close the Device failure \ n "); Return 0; }
The following is the sender program. # Include # Include # Include # Include # Include # Include # Include # Include /*************************************** ****/ Int fd;
Int flag_close;
Int open_serial (int k) { If (k = 0) { Fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY ); Perror ("open/dev/ttyS0 "); } Else { Fd = open ("/dev/ttyS1", O_RDWR | O_NOCTTY ); Perror ("open/dev/ttyS1 "); } If (fd =-1) Return-1; }
/*************************************** *****************************/ Int main (int argc, char * argv [])
Printf ("the char numbers sent is % d \ n", retv ); Flag_close = close (fd ); If (flag_close =-1) Printf ("Close the Device failure \ n "); Return 0; } After running the program, sending is normal, and receiving is garbled, I don't know what is going on. I watched it on the same terminal under a user. I don't know if this method is correct. Please give me expert guidance.
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