linux下的串口接收程序

lian3139 2009-06-30 05:24:16
我在linux下写了一个串口接收程序,不停地向串口发送“Network formed, waiting for RX“,但却接收不到,请大家帮忙看看什么问题

#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> /*错误号定义*/

#define FALSE -1
#define TRUE 0

/*struct termio
{ 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];
};
*/

struct termios Opt;

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B57600, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 57600,

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);
}
}
}

/*设置校验位*/
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 sizen"); 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 parityn");
return (FALSE);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bitsn");
return (FALSE);
}
/* 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("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}

int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}

int main(int argc, char **argv){
int fd;
int nread;
char buff[30];
char temp[]="Network formed, waiting for RX";
char *dev = "/dev/ttyS0"; //串口1
fd = OpenDev(dev);
set_speed(fd,57600);
if (set_Parity(fd,8,1,'N') == FALSE) {
printf("Set Parity Errorn");
exit (0);
}
nread=sizeof(temp);

while (1) //循环读取数据
{
while(read(fd, buff, nread)==nread)
{

printf( "%s", buff);
}
}
//close(fd);
// exit (0);
}
...全文
543 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lian3139 2009-06-30
  • 打赏
  • 举报
回复
还是不行
==nread==nread
wanggang_0717 2009-06-30
  • 打赏
  • 举报
回复
学习
pottichu 2009-06-30
  • 打赏
  • 举报
回复


while (1) //循环读取数据
{
while(read(fd, buff, nread)==nread)
{

printf( "%s", buff);
}
}

==============================
修改一下:

read(fd, buff, nread);
printf("%s \n", buff);



很可能已经收到数据了,但存在 printf的缓冲区里,没有刷出来而已。
加 \n 把它刷出来。
lian3139 2009-06-30
  • 打赏
  • 举报
回复
我已经在windows下试过了,可以吗正常接收的,我想是我写的程序问题
pottichu 2009-06-30
  • 打赏
  • 举报
回复
你现在 windows 下找个串口调试助手,确定单片机能正常发送数据再说吧。
lian3139 2009-06-30
  • 打赏
  • 举报
回复
发送是由单片机向pc串口发送的
pottichu 2009-06-30
  • 打赏
  • 举报
回复
你这个是接收程序阿。
发送程序呢? 你在哪里发送的 ?

23,121

社区成员

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

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