linux串口读写的问题

jack_shen 2009-08-25 03:46:41
#define RSDEV_NAME "/dev/tts/0"

/*
*brief 设置串口通信速率
*param fd 类型 int 打开串口的文件句柄
*param speed 类型 int 串口速度
*return void
*/
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(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 != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}

/**
*brief 设置串口数据位,停止位和效验位
*param fd 类型 int 打开的串口文件句柄
*param databits 类型 int 数据位 取值 为 7 或者8
*param stopbits 类型 int 停止位 取值为 1 或者2
*param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
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,"Unsupported parity\n");
return (FALSE);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n' && parity != 'N')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 30; /* 设置超时3 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
/**
*如果不是开发终端之类的,只是串口传输数据,
*而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯
**/
options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG |IEXTEN);
options.c_oflag &= ~OPOST;

if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}

/**打开串口
* dev 串口设备名
* mode 打开方式
**/

int opendev(char *dev,mode_t mode)
{
int fd;
fd = open(dev, mode);
if (-1 == fd){
perror("Can't Open Serial Port");
return -1;
}
else{
fcntl(fd, F_SETFL, FNDELAY);
return fd;
}
}
/********************************** input232.c 写串口********************************/


void rs232_write(void)
{

int rsfd = 0;
int nwrite;
char input_buf[64];
rsfd = opendev(RSDEV_NAME,O_RDWR | O_NOCTTY | O_NDELAY);
if(rsfd < 0){
printf("open error:\n");
return;
}
set_speed(rsfd,9600); /*设置速率B9600*/
if (set_parity(rsfd,8,1,'N') == FALSE){ /*8位数据位,一位停止位*/
printf("Set Parity Error\n");
return;
}
while(1) {
fgets(input_buf,sizeof(input_buf),stdin);
printf("input_buf = %s", input_buf);
nwrite = write(rsfd, input_buf, strlen(input_buf));
if ( nwrite == -1 ) {
perror("ERROR!");
exit(1);
}else {
printf("ret=%d\n", nwrite);
}
}
}
/********************************** read232.c 读串口********************************/


int read_rs232(int rsfd);
void rs232_read(void)
{

int rsfd = 0;
// int nwrite;
fd_set fdR;
struct timeval timev;
int n = 0;
// char input_buf[64];
rsfd = opendev(RSDEV_NAME,O_RDWR | O_NOCTTY | O_NDELAY);
if(rsfd < 0){
printf("open error:\n");
return;
}
set_speed(rsfd,9600); /*设置速率B9600*/
if (set_parity(rsfd,8,1,'N') == FALSE){ /*8位数据位,一位停止位*/
printf("Set Parity Error\n");
return;
}
while(1) {
FD_ZERO(&fdR);
FD_SET(rsfd, &fdR);
timev.tv_sec = 0;
timev.tv_usec = 100000;
n = select(rsfd+1, &fdR, NULL, NULL, &timev);
LOGMSG2("select ret %d", n);
if(n <= 0)
continue;
if (FD_ISSET(rsfd,&fdR)) {
printf("rs232 recv\n");
read_rs232(rsfd);
}
}
}
int read_rs232(int rsfd)
{
int retbytes = 0,nread = 0;
int all_bytes = 0;
struct net2net_buf netbuf;
// int i;
memset(&netbuf,0,sizeof(netbuf));
while((nread = read(rsfd, &netbuf.buf[retbytes], 512))>0)
{
printf("nread:%d\n",nread);
retbytes += nread;
all_bytes += nread;
}
return all_bytes;
}

为什么rs232_read函数总是读取不到数据,在LOGMSG2("select ret %d", n);
这里总是返回0????
...全文
276 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jack_shen 2009-08-27
  • 打赏
  • 举报
回复
问题找到了。
jack_shen 2009-08-26
  • 打赏
  • 举报
回复
希望哪位大哥回答一下.
jack_shen 2009-08-26
  • 打赏
  • 举报
回复
版主大哥请看看。谢谢了。

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧