Embedded Serial Port Programming Problems (urgent)
Source: Internet
Author: User
Embedded Serial Port Programming Problems (urgent)-Linux general technology-Linux programming and kernel information, the following is a detailed description. This is my program source code
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define DEV "/dev/ttyS0"
Int OpenDev (char * name)
{
Int fd;
Fd = open (DEV, O_RDWR );
If (fd =-1)
{
Perror ("Can't Open Serial Port! \ N ");
Return-1;
}
Else
{
Printf ("fd = % d \ n", fd );
Return fd;
}
}
Int set_speed (int fd)
{
Int status;
Struct termios Opt;
Tcgetattr (fd, & Opt );
Tcflush (fd, TCIOFLUSH );
Cfsetispeed (& Opt, B9600 );
Cfsetospeed (& Opt, B9600 );
Status = tcsetattr (fd, TCSANOW, & Opt );
If (status! = 0)
{
Perror ("Can't Set Speed! \ N ");
Return-1;
}
Tcflush (fd, TCIOFLUSH );
Return 0;
}
Int set_Parity (int fd, int databits, int stopbits, int parity)
{
Struct termios options;
If (tcgetattr (fd, & options )! = 0)
{
Perror ("Set Parity error! \ N ");
Return 0;
}
Options. c_cflag & = ~ CSIZE;
Switch (databits)
{
Case 7:
Options. c_cflag | = CS7;
Break;
Case 8:
Options. c_cflag | = CS8;
Break;
Default:
Printf ("Unsupported data size \ n ");
Return 0;
}
Switch (parity)
{
Case 'N ':
Case 'N ':
Options. c_cflag & = ~ PARENB;
Options. c_iflag & = ~ INPCK;
Break;
Case 'O ':
Case 'O ':
Options. c_cflag | = (PARODD | PARENB );
Options. c_iflag | = INPCK;
Break;
Case 'E ':
Case 'E ':
Options. c_cflag | = PARENB;
Options. c_cflag & = ~ PARODD;
Options. c_iflag | = INPCK;
Break;
Case's ':
Case's ':
Options. c_cflag & = ~ PARENB;
Options. c_cflag & = ~ CSTOPB;
Break;
Default:
Printf ("Unsupported parity \ n ");
Return 0;
}
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