普通单片机的串口操作比较容易,但是基于Linux系统的串口操作难不难呢?其实,基于Linux操作系统的串口操作分为两个部分:串口驱动部分(底层驱动与设备注册)与串口的应用程序(用户程序)。一般厂家或是Linux内核已经提供了基于开发板的串口驱动,只需要修改或是注册相应的串口设备,就可以直接使用了。因此,只需要写用户应用程序即可。
我手头上有一块AT91SAM9261EK的开发板,为了学习Linux买的,现在用到了,因此,学习一下串口的操作。
程序如下:
//rs232_test.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BAUDRATE B9600
static int open_com(char *dev_name, long com_baud, int com_data,
int com_pry, int com_stop)
{
uint baud = 0, data = 0, parity = 0, stop = 0;
struct termios strNewTio;
int fd;
//set baudrate:
switch(com_baud) {
case 50: baud = B50; break;
case 75: baud = B75; break;
case 110: baud = B110; break;
case 134: baud = B134; break;
case 150: baud = B150; break;
case 200: baud = B200; break;
case 300: baud = B300; break;
case 600: baud = B600; break;
case 1200: baud = B1200; break;
case 1800: baud = B1800; break;
case 4800: baud = B4800; break;
case 9600: baud = B9600; break;
case 19200: baud = B19200; break;
case 38400: baud = B38400; break;
case 57600: baud = B57600; break;
case 115200: baud = B115200; break;
case 230400: baud = B230400; break;
default:
baud = B9600;
}
//set databits:
switch(com_data) {
case 5: data = CS5; break;
case 6: data = CS6; break;
case 7: data = CS7; break;
case 8: data = CS8; break;
default:
data = CS8;
}
//set parity:
switch(com_pry) {
case 0: parity = 0; break;
case 1: parity = PARENB | PARODD; break;
case 2: parity = PARENB; break;
default:
parity = 0;
}
//set stopbits:
switch(com_stop) {
case 1: stop = 0; break;
case 2: stop = CSTOPB; break;
default:
stop = 0;
}
fd = open(dev_name, O_RDWR | O_NOCTTY );
if (fd < 0) {
perror("com open() error");
return -1;
}
//set parameters:
tcgetattr(fd, &strNewTio);
strNewTio.c_cflag = baud | data | parity | stop | CREAD | CLOCAL;
strNewTio.c_iflag = 0;
strNewTio.c_lflag = 0;
strNewTio.c_oflag = 0;
if (tcsetattr(fd, TCSANOW, &strNewTio) < 0) {
close(fd);
perror("com tcsetattr() error");
return -1;
}
return fd;
}
int main(int argc, char *argv[])
{
struct timeval tp;
long time1=0;
long time2=0;
int fd1,fd2;
struct termios tio1,tio2;
int res,cnt,i;
int mode = 1; //rs232 mode
unsigned char c;
unsigned char dev1[10];
unsigned char dev2[10];
int tmp1,tmp2;
if (argc == 0x0) {
printf("Usage : rs232 com_no\n");
return 0;
}
else {
tmp1 = atoi(argv[1]);
if (tmp1 > 16 ) {
printf("Usage : rs232_loopback com_no com_no\n");
return 0;
}
else {
sprintf(dev1, "/dev/ttyS%d", tmp1);
}
}
fd1 = open_com(dev1, 9600, 8, 0, 1);
if (fd1 < 0) {
printf("can not open %s\n", dev1);
close(fd1);
return 0;
}
else
{
printf("Open COM %s Success!!\n", dev1);
}
// clear the noise, made by change UART mode
usleep(10000);
char wr = 'k';
char rd [1000];
int ret;
rd[0] = 0;
while(1)
{
printf("=======%d\n ", i++);
//write(fd1, &wr, 1);
printf("=send:%c ", wr);
usleep(100);
ret = read(fd1, rd, 512);
if (ret)
{
printf("\n=========receive:%s\n", rd);
write(fd1, rd, strlen(rd));
memset(rd, 0, 512);
}
}
return 0;
}
然后连接好此串口的串口线,通过ftp等放到开发板的根文件系统里,在执行目录下执行:
# ./rs232_test 1 //这里打开/dev/ttyS1
打开成功,就会提示成功的信息,就可以通过串口助手往相应的串口上发送数据了。