一、封装串口文件
#ifndef _USART_H
#define _USART_H
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<termios.h>
#include<errno.h>
#include<string.h>
int UART0_Open(int fd, char* port);
void UART0_Close(int fd);
int UART0_Set(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity);
int UART0_Init(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity);
int UART0_Send(int fd, char* send_buf, int data_len);
int UART0_Recv(int fd, char* rcv_buf, int data_len);
#endif
int UART0_Open(int fd, char* port)
{
fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("Can't Open Serial Port\n");
return -1;
}
if(fcntl(fd, F_SETFL, 0) < 0)
{
printf("fcntl failed!\n");
return -1;
}
else
{
printf("Open Serial Port Succeed!");
}
return 1;
}
void UART0_Close(int fd)
{
close(fd);
}
int UART0_Set(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity)
{
int i;
int status;
int speed_arr[] = { B115200, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200, 19200, 9600, 4800, 2400, 1200, 300};
struct termios options;
if( tcgetattr(fd, &options) != 0)
{
printf("SetupSerial 1\n");
return -1;
}
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
cfsetispeed(&options, speed_arr[i]);
cfsetospeed(&options, speed_arr[i]);
}
}
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
switch(flow_ctrl)
{
case 0 ://不使用流控制
{
options.c_cflag &= ~CRTSCTS;
break;
}
case 1 ://使用硬件流控制
{
options.c_cflag |= CRTSCTS;
break;
}
case 2 ://使用软件流控制
{
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 5 :
{
options.c_cflag |= CS5;
break;
}
case 6 :
{
options.c_cflag |= CS6;
break;
}
case 7 :
{
options.c_cflag |= CS7;
break;
}
case 8:
{
options.c_cflag |= CS8;
break;
}
default:
{
printf("Unsupported data size\n");
return -1;
}
}
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 -1;
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
printf("Unsupported stop bits\n");
return -1;
}
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
printf("com set error!\n");
return -1;
}
return 1;
}
int UART0_Init(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity)
{
int err;
if (UART0_Set(fd, speed, flow_ctrl, databits, stopbits, parity) == -1)
{
return -1;
}
else
{
return 1;
}
}
int UART0_Send(int fd, char* send_buf, int data_len)
{
int len = 0;
len = write(fd, send_buf, data_len);
if (len == data_len)
{
printf("Data sent is %s\n", send_buf);
return 1;
}
else
{
tcflush(fd,TCOFLUSH);
return -1;
}
}
int UART0_Recv(int fd, char* rcv_buf, int data_len)
{
int len,fs_sel;
fd_set fs_read;
struct timeval time;
FD_ZERO(&fs_read);
FD_SET(fd, &fs_read);
time.tv_sec = 10;
time.tv_usec = 0;
fs_sel = select(fd + 1, &fs_read, NULL, NULL, &time);
printf("fs_sel = %d\n",fs_sel);
if(fs_sel)
{
len = read(fd, rcv_buf, data_len);
return len;
}
else
{
return -1;
}
}
二、main文件
#include <iostream>
#include "serial.h"
using namespace std;
int main(){
int fd = 0;
char port_name[] = "/dev/ttyUSB0";
if(UART0_Open(fd, port_name) == -1)
{
return -1;
}
if(UART0_Init(fd, 115200, 0, 8, 1, 'N') == -1){
return -1;
}
char buff_send[5] = {'a','b','c','d'};
if(UART0_Send(fd, buff_send, 5) == -1){
return - 1;
}
char buff_receive[100] = "";
int len = UART0_Recv(fd, buff_receive, 5);
printf("收到[%d]个字符\n", len);
return 0 ;
}