linux环境下串口测试问题

班公湖里洗过脚 2012-04-20 10:14:11
我在linux环境下写了一个C语言的串口测试程序,测试串口的自发自接功能
源代码如下
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/time.h>




void init_ttyS(int fd)
{
struct termios newtio;

bzero(&newtio, sizeof(newtio));

newtio.c_lflag &= ~(ECHO | ICANON);
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_oflag &= ~(OPOST);

newtio.c_cc[VTIME] = 5; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 9 chars received */

tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

}//end init_ttyS

int main(void)
{
int fd;
int nread,i,ss;

char buff[]="Hello\n",*bufff;
// open seriel port
fd = open("/dev/ttyS0", O_RDWR);

if (fd == -1)
{
printf("open device error\n");
}
else
{
init_ttyS(fd); // init device
printf("fd=%d\n",fd);
printf("start send\n");
nread=write(fd,buff,8);
printf("nread=%d,%s\n",nread,buff);
while(1)
{
ss=read(fd,bufff,6);
if(ss>0)
{
printf("ss=%d,%s\n",ss,bufff);
break;
}
}
}
close(fd);
return 0;

} // end main

运行之前将串口的2号和3号引脚连接起来
运行时收不到字符,求解
...全文
249 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我测试的功能是自发自收,一个串口就行了,
  • 打赏
  • 举报
回复
你只打开了一个串口,发不出当然收不到

21,600

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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