LINUX Serial Communication Source __linux

Source: Internet
Author: User

Main function

int Openport (char *dev)//Open serial port

int setport (int fd, int baud,int databits,int stopbits,int parity)//Set serial port, baud rate, data bit, stop bit, checksum

int readport (int fd,char *buf,int len,int maxwaittime)/read data, parameter is serial port, buf, length, timeout time

int writeport (int fd,char *buf,int len)//Send data

void ClearPort (int fd)//If the data does not conform to the specification, this function can be called to refresh the serial port read and write data

If there is a bug, please reply to me in time, email:41063473@qq.com.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/time.h>
int Openport (char *dev)
{
int fd = open (Dev, o_rdwr| o_noctty| O_ndelay);
if ( -1 = = FD)
{
Perror ("Can" "T Open serial Port");
return-1;
}
Else
return FD;

}

int setport (int fd, int baud,int databits,int stopbits,int parity)
{
int baudrate;
struct Termios newtio;
Switch (baud)
{
Case 300:
baudrate=b300;
Break
Case 600:
baudrate=b600;
Break
Case 1200:
baudrate=b1200;
Break
Case 2400:
baudrate=b2400;
Break
Case 4800:
baudrate=b4800;
Break
Case 9600:
baudrate=b9600;
Break
Case 19200:
baudrate=b19200;
Break
Case 38400:
baudrate=b38400;
Break
Default:
baudrate=b9600;
Break
}
Tcgetattr (Fd,&newtio);
Bzero (&newtio,sizeof (newtio));
Setting C_cflag
Newtio.c_cflag &=~CSIZE;
Switch (databits)/* Set the number of data bits * *
{
Case 7:
Newtio.c_cflag |= CS7; 7 Bit data bits
Break
Case 8:
Newtio.c_cflag |= CS8; 8 Bit data bits
Break
Default
Newtio.c_cflag |= CS8;
Break
}
switch (parity)//Set Checksum
{
Case ' n ':
Case ' N ':
Newtio.c_cflag &= ~parenb; /* Clear parity enable * *
Newtio.c_iflag &= ~INPCK; /* Enable Parity checking * *
Break
Case ' O ':
Case ' O ':
Newtio.c_cflag |= (parodd | PARENB); /* Set to the Magic Magic Test *
Newtio.c_iflag |= INPCK; /* disnable Parity checking * *
Break
Case ' E ':
Case ' E ':
Newtio.c_cflag |= Parenb; /* Enable Parity * *
Newtio.c_cflag &= ~parodd; /* Conversion to even-parity *
Newtio.c_iflag |= INPCK; /* disnable Parity checking * *
Break
Case ' S ':
Case ' s ':/*as no parity*/
Newtio.c_cflag &= ~parenb;
Newtio.c_cflag &= ~cstopb;break;
Default
Newtio.c_cflag &= ~parenb; /* Clear parity enable * *
Newtio.c_iflag &= ~INPCK; /* Enable Parity checking * *
Break
}
Switch (stopbits)/Set Stop bit
{
Case 1:
Newtio.c_cflag &= ~CSTOPB; 1
Break
Case 2:
Newtio.c_cflag |= CSTOPB; 2
Break
Default
Newtio.c_cflag &= ~CSTOPB;
Break
}
Newtio.c_cc[vtime] = 0;
Newtio.c_cc[vmin] = 0;
Newtio.c_cflag |= (clocal| Cread);
Newtio.c_oflag|=opost;
Newtio.c_iflag &=~ (ixon| Ixoff| Ixany);
Cfsetispeed (&newtio,baudrate);
Cfsetospeed (&newtio,baudrate);
Tcflush (FD, Tciflush);
if (tcsetattr (fd,tcsanow,&newtio)!= 0)
{
Perror ("Setupserial 3");
return-1;
}
return 0;
}
int readport (int fd,char *buf,int len,int maxwaittime)/read data, parameter is serial port, buf, length, timeout time
{
int No=0;int Rc;int Rcnum=len;
struct Timeval TV;
Fd_set READFD;
tv.tv_sec=maxwaittime/1000; SECOND
tv.tv_usec=maxwaittime%1000*1000; Usecond
Fd_zero (&AMP;READFD);
Fd_set (FD,&AMP;READFD);
Rc=select (FD+1,&AMP;READFD,NULL,NULL,&AMP;TV);
if (rc>0)
{
while (LEN)
{
Rc=read (fd,&buf[no],1);
if (rc>0)
no=no+1;
len=len-1;
}
if (no!=rcnum)
return-1; If the received length is not the same as the desired length, return-1
return rcnum; Receive length as expected length, return length
}
Else
{
return-1;
}
return-1;
}
int writeport (int fd,char *buf,int len)//Send data
{
Write (Fd,buf,len);
}
void ClearPort (int fd)//If the data does not conform to the specification, this function can be called to refresh the serial port read and write data
{
Tcflush (Fd,tcioflush);
}
Main ()
{
int Fd,rc,i,ret;
unsigned char rbuf[256];
unsigned char wbuf[256]= "";
for (i=0;i<256;i++)
Wbuf[i]=i;
Char *dev = "/DEV/TTYS0"; String number/DEV/TTYS0 corresponds to serial port 1
FD = Openport (dev); Open serial port
if (fd>0)
{
Ret=setport (fd,4800,8,1, ' o '); Set serial port, baud rate, data bit, stop bit, check
if (ret<0)
{
printf ("Can ' t Set serial port!/n");
Exit (0);
}
}
Else
{
printf ("Can ' t Open serial port!/n");
Exit (0);
}
while (1) {
Rc=readport (fd,rbuf,5,500); Reads 5 bytes, timeout is 500 milliseconds
if (rc>0)
{
Writeport (FD,WBUF,RC);
printf ("recv:%d/n", RC);
for (i=0;i<rc;i++)
printf ("%02x", Rbuf[i]);
printf ("n");
}
Else
printf ("recv none/n");
}
Close (FD);
}

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.