1,183
社区成员
发帖
与我相关
我的任务
分享
//串口相关的头文件
#include<stdio.h> /*标准输入输出定义*/
#include<stdlib.h> /*标准函数库定义*/
#include<unistd.h> /*Unix 标准函数定义*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> /*文件控制定义*/
#include<termios.h> /*PPSIX 终端控制定义*/
#include<errno.h> /*错误号定义*/
#include<string.h>
#define FALSE -1
#define TRUE 0
int fd = -1;
int zc_rs232_OpenPort(char *lpFileName, int baudRate, char parity,
int byteSize, int stopBits) {
//LOGI("%s", str);
fd = UART0_Open(lpFileName);
int set = UART0_Set(fd, baudRate, 0, byteSize, stopBits, parity);
if (fd == 0 && set == 0) {
return 0;
} else {
return -1;
}
}
int zc_rs232_ClosePort( ) {
int a = UART0_Close();
//LOGI("关闭 === %d", a);
if (a == 0) {
return 0;
} else {
return -1;
}
}
int zc_rs232_WritePort(char *strbyte, int strlens) {
int a = UART0_Send(strbyte, strlens);
//LOGI("发送数据 === %d", a);
if (a == strlen(strbyte)) {
return 0;
} else {
return -1;
}
}
int zc_rs232_ReadPort(int num, char *readbyte, int outtimes) {
memset(readbyte, 0x00, num);
int len = 0, ret = 0;
fd_set fs_read;
struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fd, &fs_read);
tv_timeout.tv_sec = (outtimes * 20 / 115200 + 2);
tv_timeout.tv_usec = 0;
ret = select(fd + 1, &fs_read, NULL, NULL, &tv_timeout);
//如果返回0,代表在描述符状态改变前已超过timeout时间,错误返回-1
if (FD_ISSET(fd, &fs_read)) {
len = read(fd, str, num);
} else {
return -1;
}
if (len > 0) {
return 0;
} else {
return -1;
}
}
/*******************************************************************
* 名称: UART0_Set
* 功能: 设置串口数据位,停止位和效验位
* 入口参数: fd 串口文件描述符
* speed 串口速度
* flow_ctrl 数据流控制
* databits 数据位 取值为 7 或者8
* stopbits 停止位 取值为 1 或者2
* parity 效验类型 取值为N,E,O,,S
*出口参数: 正确返回为1,错误返回为0
*******************************************************************/
int UART0_Set(int fd, int speed, int flow_ctrl, int databits, int stopbits,
int parity) {
int i;
int status;
int speed_arr[] = { B4000000, B115200, B19200, B9600, B4800, B2400, B1200,
B300 };
int name_arr[] = { 28800, 115200, 19200, 9600, 4800, 2400, 1200, 300 };
struct termios options;
/*tcgetattr(fd,&options)得到与fd指向对象的相关参数,并将它们保存于options,该函数还可以测试配置是否正确,该串口是否可用等。若调用成功,函数返回值为0,若调用失败,函数返回值为1.
*/
if (tcgetattr(fd, &options) != 0) {
//LOGI("SetupSerial 1");
return (FALSE);
}
//设置串口输入波特率和输出波特率
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:
//LOGI("Unsupported data size\n");
return (FALSE);
}
//设置校验位
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:
//LOGI("Unsupported parity\n");
return (FALSE);
}
// 设置停止位
switch (stopbits) {
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
//LOGI("Unsupported stop bits\n");
return (FALSE);
}
//设置等待时间和最小接收字符
options.c_cc[VTIME] = 150; /* 读取一个字符等待1*(1/10)s */
options.c_cc[VMIN] = 0; /* 读取字符的最少个数为1 */
//如果发生数据溢出,接收数据,但是不再读取 刷新收到的数据但是不读
tcflush(fd, TCIFLUSH);
//激活配置 (将修改后的termios数据设置到串口中)
if (tcsetattr(fd, TCSANOW, &options) != 0) {
//LOGI("com set error!\n");
return (FALSE);
}
return (TRUE);
}
/*******************************************************************
* 名称: UART0_Open
* 功能: 打开串口并返回串口设备文件描述
* 入口参数: fd :文件描述符 port :串口号(ttyS0,ttyS1,ttyS2)
* 出口参数: 正确返回为1,错误返回为0
*******************************************************************/
int UART0_Open(char* port) {
//LOGI("%s", port);
fd = open(port, O_RDWR | O_NOCTTY);
if (FALSE == fd) {
//LOGI("Can't Open Serial Port");
return (FALSE);
}
//LOGI("fd->open=%d\n", fd);
return fd;
}
/*******************************************************************
* 名称: UART0_Close
* 功能: 关闭串口并返回串口设备文件描述
* 入口参数: fd :文件描述符
* 出口参数: void
*******************************************************************/
int UART0_Close() {
int a = close(fd);
//LOGI("fd->close=%d\n", a);
return a;
}
/********************************************************************
* 名称: UART0_Send
* 功能: 发送数据
* 入口参数: fd :文件描述符
* send_buf :存放串口发送数据
* data_len :一帧数据的个数
* 出口参数: 正确返回为1,错误返回为0
*******************************************************************/
int UART0_Send(char *send_buf, int data_len) {
int len = 0;
len = write(fd, send_buf, data_len);
if (len == data_len) {
return len;
} else {
tcflush(fd, TCOFLUSH);
return FALSE;
}
}