读书人

看看网上找的读串口的程序不知道如

发布时间: 2012-04-13 13:50:24 作者: rapoo

看看,网上找的读串口的程序,不知道怎么不能运行
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

//串口句柄
int fd=0;

#define BYTE (unsigned char)
#define LEN 128

//波特率参数
int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300};
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300};

void opentty(); //打开串口
void closetty(); //关闭串口
void SetSpeed(int fd, int speed); //设置波特率
int SetParity(int fd, int databits, int stopbits, int parity); //设置数据位
int readtty(int fd); //读串口

int main() {
int item, pid, pid2, jump=0;
char data[37];
BYTE wbuf[128]={0}, rbuf[128]={0};

opentty();
SetSpeed(fd, 9600);
SetParity(fd, 8, 1, 'N');
readtty(fd);
closetty();

return 0;
}

//打开串口
void opentty() {
printf("打开串口!!\n");
fd = open("/dev/ttyS0", O_RDWR);
if (fd == -1) {
printf("\n");
perror("打开串口失败!");
abort();
} else
printf("串口句柄: %d\n", fd);
}

//关闭串口
void closetty() {
if (fd > 0) {
printf("关闭串口!!\n");
close(fd);
}
}


int readtty(int fd) {
struct timeval tv;
fd_set rdfds;
int ret, iRead, i;
BYTE rbuff[LEN], tmpbuff[LEN];

memset(rbuff, 0, sizeof(rbuff));
memset(tmpbuff, 0, sizeof(tmpbuff));
while (1) {
FD_ZERO(&rdfds);
FD_SET(fd, &rdfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select(fd+1, &rdfds, NULL, NULL, &tv);
if (ret < 0) {
printf("select error! %d\n", ret);
return -1;
} else if (FD_ISSET(fd, &rdfds)) {
memset(tmpbuff, 0, sizeof(tmpbuff));
iRead = read(fd, tmpbuff, sizeof(tmpbuff));
if (iRead > 0) {
printf("read=%d\n", iRead);
for (i=0; i < LEN; i++)
printf(" %x",tmpbuff[i]);
break;
} else
printf("\n错误\n");
}
printf("read running \n");
}

return 0;
}

//设置波特率
void SetSpeed(int fd, int speed) {
int i;
int status;
struct termios Opt;

tcgetattr(fd, &Opt);
for (i=0; i < sizeof(speed_arr)/sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status)
perror("设置波特率失败!");
else
tcflush(fd,TCIOFLUSH);
break;
}
}
printf("设置波特率成功!!!\n");
}

//设置数据位
int SetParity(int fd, int databits, int stopbits, int parity) {
struct termios options;

if (tcgetattr(fd, &options)) {
perror("设置效验1");


return -1;
}
options.c_cflag &= ~CSIZE;

/*设置数据位数*/
switch (databits) {
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr, "无法获取数据位大小");
return -1;
}

switch (parity) {
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr, "无法获取校验位");
return -1;
}

/* 设置停止位*/
switch (stopbits) {
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr, "无法获取停止位");
return -1;
}

/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd, TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd, TCSANOW, &options) != 0) {
perror("设置效验2,失败!");
return -1;
}
printf("设置数据位成功!");

return 0;
}

[解决办法]
你的是windows还是linux,上面的是liunx代码吧

读书人网 >C++

热点推荐