Serial Port problems in linux

Source: Internet
Author: User
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 */

Int flag_close, retv, I, ncount = 0;

Struct termios opt;

Int realdata = 0;

/*************************************** ****************************/

Open_serial (0);/* open the serial 1 */

/*************************************** ****************************/

Tcgetattr (fd, & opt );

Cfmakeraw (& opt );

/*************************************** **************************/

Cfsetispeed (& opt, B9600);/* bound rate is set 9600 */

Cfsetospeed (& opt, B9600 );

/*************************************** ****************************/

Tcsetattr (fd, TCSANOW, & opt );

Printf ("ready for processing ing data... \ n ");

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 [])

{

Char sbuf [] = {"Serial_Port test! \ N "};

Int sfd, retv, I;

Struct termios opt;

Int length = sizeof (sbuf );

/*************************************** ****************************/

Open_serial (0 );

/*************************************** ****************************/

Printf ("ready for sending data... \ n ");

Tcgetattr (fd, & opt );

Cfmakeraw (& opt );

/*************************************** **************************/

Cfsetispeed (& opt, B9600 );

Cfsetospeed (& opt, B9600 );

/*************************************** ****************************/

Tcsetattr (fd, TCSANOW, & opt );

Retv = write (fd, sbuf, length );

If (retv =-1)
{
Perror ("write ");
}

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.

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.