Compile the c serial port program in linux: Infileincludedfromserial. c: 1: 0:

Source: Internet
Author: User
Compile the c serial port program in linux: Infileincludedfromserial. c: 1: 0: -- Linux general technology-Linux technology and application information. See the following for details. Serial Port program serial. c
CODE: # include "serial. h"

Static struct termios termios_old, termios_new;

// Set the serial communication rate
Int _ BaudRate (int baudrate)
{
Switch (baudrate)
{
Case B2400:
Return (2400 );
Case B4800:
Return (4800 );
Case B9600:
Return (9600 );
Case B19200:
Return (19200 );
Case B38400:
Return (38400 );
Case B57600:
Return (57600 );
Case B115200:
Return (115200 );
Default:
Return (9600 );
}

}

Int BaudRate (int baudrate)
{
Switch (baudrate)
{
Case 0:
Return (B2400 );
Case 1:
Return (B4800 );
Case 2:
Return (B9600 );
Case 3:
Return (B19200 );
Case 4:
Return (B38400 );
Case 5:
Return (B57600 );
Case 6:
Return (B115200 );
Default:
Return (B9600 );
}
}



// Set parameters such as serial data bit, stop bit, parity bit, speed, and timeout setting
Int setPara (int serialfd, int speed, int databits, int stopbits, int parity)
{
Bzero (& termios_new, sizeof (termios_new ));
Cfmakeraw (& termios_new );
Termios_new.c_cflag = BaudRate (speed );
Termios_new.c_cflag | = CLOCAL | CREAD;

Termios_new.c_cflag & = ~ CSIZE;
Switch (databits) // sets the data bit
{
Case 0:
Termios_new.c_cflag | = CS8;
Break;
Case 1:
Termios_new.c_cflag | = CS7;
Break;
Case 2:
Termios_new.c_cflag | = CS6;
Break;
Case 3:
Termios_new.c_cflag | = CS5;
Break;
Default:
Termios_new.c_cflag | = CS8;
Break;
}

Switch (parity) // set the parity
{
Case 0: // as no parity
Termios_new.c_cflag & = ~ PARENB; // Clear parity enable
Break;
Case 1:
Termios_new.c_cflag | = PARENB; // Enable parity
Termios_new.c_cflag & = ~ PARODD;
Break;
Case 2:
Termios_new.c_cflag | = PARENB;
Termios_new.c_cflag | = ~ PARODD;
Break;
Default:
Termios_new.c_cflag & = ~ PARENB; // Clear parity enable
Break;
}
Switch (stopbits) // set Stop Bit
{
Case 0:
Termios_new.c_cflag & = ~ CSTOPB;
Break;
Case 1:
Termios_new.c_cflag | = CSTOPB;
Break;
Default:
Termios_new.c_cflag & = ~ CSTOPB;
Break;
}
Tcflush (serialfd, TCIFLUSH); // Update the termios_new and do it NOW
Termios_new.c_cc [VTIME] = 1;/* unit: 1/10 second .*/
Termios_new.c_cc [VMIN] = 1;/* minimal characters for reading */
Tcflush (serialfd, TCIFLUSH );
Return tcsetattr (serialfd, TCSANOW, & termios_new );
}

Int nwrite (int serialfd, const char * data, int datalength)
{

Int len = 0, total_len = 0; // modify8.8
For (total_len = 0; total_len <datalength ;)
{
Len = 0;

Len = write (serialfd, & data [total_len], datalength-total_len );
If (len> 0)
Total_len + = len;
}
Return (total_len );
}

Int GetBaudRate ()
{
Return (_ BaudRate (cfgetospeed (& termios_new )));
}

Void closePort (int serialfd)
{
Tcsetattr (serialfd, TCSADRAIN, & termios_old );
Close (serialfd );
}

Int openPort (int index)
{
Char * device;
Switch (index)
{
Case 0:
Device = "/dev/ttyS0 ";
Break;
Case 1:
Device = "/dev/ttyS1 ";
Break;
Case 2:
Device = "/dev/ttyS2 ";
Break;
Case 3:
Device = "/dev/ttyS3 ";
Break;
Default:
Device = "/dev/ttyS0 ";
}

Int fd = open (device, O_RDWR | O_NOCTTY | O_NONBLOCK );
If (fd <0)
{

Return-1;
}
Tcgetattr (fd, & termios_old );

Return fd;
}
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.