在这里 附上linux 串口编程的 代码,仅供自己学习查找
- #include<termios>
-
struct termios
-
{
-
unsigned short c_iflag; 输入模式标志
-
unsigned short c_oflag; 输出模式标志
-
unsigned short c_cflag; 控制模式标志
-
unsigned short c_lflag; 本地模式标志
-
unsigned char c_line; 线路规程
-
unsigned char c_cc[NCC]; 控制特征
-
speed_t c_ispeed; 输入速度
-
speed_ c_ospeed; 输出速度
-
}
- /* uart_api.h */
-
/* 串口配置函数 */
-
int set_com_config(int fd,int baud_rate,
-
int data_bits, char parity, int stop_bits)
-
{
-
struct termios new_cfg,old_cfg;
-
int speed;
-
-
/*保存并测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/
-
if (tcgetattr(fd, &old_cfg) != 0)
-
{
-
perror("tcgetattr");
-
return -1;
-
}
-
new_cfg = old_cfg;
-
cfmakeraw(&new_cfg); /* 配置为原始模式 */
-
new_cfg.c_cflag &= ~CSIZE;
-
/*设置波特率*/
-
switch (baud_rate)
-
{
-
case 2400:
-
{
-
speed = B2400;
-
}
-
break;
-
case 4800:
-
{
-
speed = B4800;
-
}
-
break;
-
case 9600:
-
{
-
speed = B9600;
-
}
-
break;
-
case 19200:
-
{
-
speed = B19200;
-
}
-
break;
-
case 38400:
-
{
-
speed = B38400;
-
}
-
break;
-
-
default:
-
case 115200:
-
{
-
speed = B115200;
-
}
-
break;
-
}
-
cfsetispeed(&new_cfg, speed);
-
cfsetospeed(&new_cfg, speed);
-
-
switch (data_bits) /*设置数据位*/
-
{
-
case 7:
-
{
-
new_cfg.c_cflag |= CS7;
-
}
-
break;
-
-
default:
-
case 8:
-
{
-
new_cfg.c_cflag |= CS8;
-
}
-
break;
-
}
-
-
switch (parity) /*设置奇偶校验位*/
-
{
-
default:
-
case 'n':
-
case 'N':
-
{
-
new_cfg.c_cflag &= ~PARENB;
-
new_cfg.c_iflag &= ~INPCK;
-
}
-
break;
-
-
case 'o':
-
case 'O':
-
{
-
new_cfg.c_cflag |= (PARODD | PARENB);
-
new_cfg.c_iflag |= INPCK;
-
}
-
break;
-
-
case 'e':
-
case 'E':
-
{
-
new_cfg.c_cflag |= PARENB;
-
new_cfg.c_cflag &= ~PARODD;
-
new_cfg.c_iflag |= INPCK;
-
}
-
break;
-
-
case 's': /*as no parity*/
-
case 'S':
-
{
-
new_cfg.c_cflag &= ~PARENB;
-
new_cfg.c_cflag &= ~CSTOPB;
-
}
-
break;
-
}
-
-
switch (stop_bits) /*设置停止位*/
-
{
-
default:
-
case 1:
-
{
-
new_cfg.c_cflag &= ~CSTOPB;
-
}
-
break;
-
-
case 2:
-
{
-
new_cfg.c_cflag |= CSTOPB;
-
}
-
}
-
-
/*设置等待时间和最小接收字符*/
-
new_cfg.c_cc[VTIME] = 0;
-
new_cfg.c_cc[VMIN] = 1;
-
tcflush(fd, TCIFLUSH); /*处理未接收字符*/
-
if ((tcsetattr(fd, TCSANOW, &new_cfg)) != 0) /*激活新配置*/
-
{
-
perror("tcsetattr");
-
return -1;
-
}
-
return 0;
-
}
-
-
/*打开串口函数*/
-
int open_port(int com_port)
-
{
-
int fd;
-
#if (COM_TYPE == GNR_COM) /* 使用普通串口 */
-
char *dev[] = {"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2"};
-
#else /* 使用USB转串口 */
-
char *dev[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2"};
-
#endif
-
if ((com_port < 0) || (com_port > MAX_COM_NUM))
-
{
-
return -1;
-
}
-
/* 打开串口 */
-
fd = open(dev[com_port - 1], O_RDWR|O_NOCTTY|O_NDELAY);
-
if (fd < 0)
-
{
-
perror("open serial port");
-
return(-1);
-
}
-
-
if (fcntl(fd, F_SETFL, 0) < 0) /*恢复串口为阻塞状态*/
-
{
-
perror("fcntl F_SETFL\n");
-
}
-
-
if (isatty(fd) == 0) /*测试打开的文件是否为终端设备*/
-
{
-
perror("This is not a terminal device");
-
}
-
return fd;
-
}
- /* com_writer.c */
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <errno.h>
-
#include "uart_api.h"
-
int main(void)
-
{
-
int fd;
-
char buff[BUFFER_SIZE];
-
if((fd = open_port(HOST_COM_PORT)) < 0) /* 打开串口 */
-
{
-
perror("open_port");
-
return 1;
-
}
-
if(set_com_config(fd, 115200, 8, 'N', 1) < 0) /* 配置串口 */
-
{
-
perror("set_com_config");
-
return 1;
-
}
-
do
-
{
-
printf("Input some words(enter 'quit' to exit):");
-
memset(buff, 0, BUFFER_SIZE);
-
if (fgets(buff, BUFFER_SIZE, stdin) == NULL)
-
{
-
perror("fgets");
-
break;
-
}
-
write(fd, buff, strlen(buff));
-
} while(strncmp(buff, "quit", 4));
-
close(fd);
-
return 0;
-
}
- /* com_reader.c */
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <errno.h>
-
#include "uart_api.h"
-
-
int main(void)
-
{
-
int fd;
-
char buff[BUFFER_SIZE];
-
-
if((fd = open_port(TARGET_COM_PORT)) < 0) /* 打开串口 */
-
{
-
perror("open_port");
-
return 1;
-
}
-
if(set_com_config(fd, 115200, 8, 'N', 1) < 0) /* 配置串口 */
-
{
-
perror("set_com_config");
-
return 1;
-
}
-
-
do
-
{
-
memset(buff, 0, BUFFER_SIZE);
-
if (read(fd, buff, BUFFER_SIZE) > 0)
-
{
-
printf("The received words are : %s", buff);
-
}
-
} while(strncmp(buff, "quit", 4));
-
close(fd);
-
return 0;
-
}
linux串口.rar
阅读(831) | 评论(0) | 转发(0) |