What are the serial Programming Problems in linux ???

Source: Internet
Author: User
What are the serial Programming Problems in linux ??? -- Linux general technology-Linux programming and kernel information. The following is a detailed description. In recent days, I learned serial programming and wrote code according to the tutorial in the book. During the test, it was okay to send data to the serial port through the write function (I have two comports on my machine, the system is XP, and virtualora 10 is installed with VirtualBox, and the data sent by fedora is received through the XP Terminal Service). However, when I use the read function to receive data, only garbled characters are received, and there is no waiting. As long as a compiled document is executed, the code runs directly and a garbled string is returned. I really don't understand what is going on here. I posted the code. Please help me to see if I was wrong. Thank you.

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include


Int fd;


Struct termio
{
Unsigned short c_iflag;
Unsigned short c_oflag;
Unsigned short c_cflag;
Unsigned short c_lflag;
Unsigned char c_line;
Unsigned char c_cc [NCCS];

};


Int open_port (int comport)
{
If (comport = 1)
{
Fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
If (fd =-1)
{
Perror ("Can't Open Serial Port ");
Return (-1 );
}
}
Else if (comport = 2)
{
Fd = open ("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY );
If (fd =-1)
{
Perror ("Can't Open Serial Port ");
Return (-1 );
}
}
Else if (comport = 3)
{
Fd = open ("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY );
If (fd =-1)
{
Perror ("Can't Open Serial Port ");
Return (-1 );
}
}



If (fcntl (fd, F_SETFL, 0) <0)
Printf ("fcntl failed! \ N ");
Else
Printf ("fcntl = % d \ n", fcntl (fd, F_SETFL, 0 ));

If (isatty (STDIN_FILENO) = 0)
Printf ("standard input is not a terminal device \ n ");
Else
Printf ("isatty success! \ N ");

Printf ("fd-open = % d \ n", fd );
Return fd;

}



Int set_opt (int fd, int nSpeed, int nBits, char nEvent, int nStop )//
{
Struct termios newtio, oldtio;
If (tcgetattr (fd, & oldtio )! = 0)
{
Perror ("SetupSerial 1 ");
Return-1;
}
Bzero (& newtio, sizeof (newtio ));
//
Newtio. c_cflag | = CLOCAL | CREAD;
Newtio. c_cflag & = ~ CSIZE;
//
Switch (nBits)
{
Case 7:
Newtio. c_cflag | = CS7;
Break;
Case 8:
Newtio. c_cflag | = CS8;
Break;
}
//
Switch (nEvent)
{
Case 'O ':
Newtio. c_cflag | = PARENB;
Newtio. c_cflag | = PARODD;
Newtio. c_iflag | = (INPCK | ISTRIP );
Break;
Case 'E ':
Newtio. c_iflag | = (INPCK | ISTRIP );
Newtio. c_cflag | = PARENB;
Newtio. c_cflag & = ~ PARODD;
Break;
Case 'N ':
Newtio. c_cflag & = ~ PARENB;
Break;
}
//
Switch (nSpeed)
{
Case 2400:
Cfsetispeed (& newtio, B2400 );
Cfsetospeed (& newtio, B2400 );
Break;
Case 4800:
Cfsetispeed (& newtio, B4800 );
Cfsetospeed (& newtio, B4800 );
Break;
Case 9600:
Cfsetispeed (& newtio, B9600 );
Cfsetospeed (& newtio, B9600 );
Break;
Case 115200:
Cfsetispeed (& newtio, B115200 );
Cfsetospeed (& newtio, B115200 );
Break;
Case 460800:
Cfsetispeed (& newtio, B460800 );
Cfsetospeed (& newtio, B460800 );
Break;
Default:
Cfsetispeed (& newtio, B9600 );
Cfsetospeed (& newtio, B9600 );
Break;
}
//
If (nStop = 1)
Newtio. c_cflag & = ~ CSTOPB;
Else if (nStop = 2)
Newtio. c_cflag | = CSTOPB;
//
Newtio. c_cc [VTIME] = 0;
Newtio. c_cc [VMIN] = 0;
//
Tcflush (fd, TCIFLUSH );
//
If (tcsetattr (fd, TCSANOW, & newtio ))! = 0)
{
Perror ("com set error ");
Return-1;
}
Printf ("set done! \ N ");
Return 0;
}



Int main ()
{
Int I, j;
Char na [30];
Fd = open_port (1 );
Printf ("fd = % d \ n", fd );
J = set_opt (fd, 115200,8, 'n', 1 );
Printf ("j = % d \ n", j );
I = read (fd, na, 31 );
Printf ("I = % d, na = % s \ n", I, na );
Close (fd );
Return;
}

[ This post was last edited at, 657371]
Related Article

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.