请教关于linux串口读写的问题

jeky_zhang2013 2014-05-20 11:37:45
最近在研究linux 串口读写,碰到一个问题,在电脑端口给平板(android系统)发送AT命令(通讯命令),通过工具发送是成功的。
我想能否直接在平板侧实现串口发送,脱离PC。电脑侧串口设备对应平板中的为/dev/ttyGS1.我的程序为:
testmux.c
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
int nread;
int fd;
struct termios attr;
fd_set fds;
struct timeval tv;
unsigned char buf[512];
fd = open("/dev/ttyGS1", O_RDWR);
if (fd == -1)
return -1;
printf("hello, Serial port is OK\n");
tcgetattr(fd, &attr);
// 设置最少接收字符个数为0
attr.c_cc[VMIN] = 0;
// 不处理iflag、oflag和lflag
attr.c_iflag = 0;
attr.c_oflag = 0;
attr.c_lflag = 0;
attr.c_cflag = B9600 | CS8 | CREAD;
tcsetattr(fd, TCSANOW, &attr);
write(fd, "AT+CGMI", 7);
FD_ZERO(&fds);
FD_SET(fd, &fds);
// 设置超时为5秒
tv.tv_sec = 10;
tv.tv_usec = 0;
nread=read(fd, buf, 512);
printf("readlength=%d\n", nread);
buf[nread]='\0';
printf("%s\n", buf);

if (select(fd+1, &fds, NULL, NULL, &tv) <= 0)
{
printf("receive null!!\n");
return -1;
}
close(fd);
return 0;
}



在x86机器上交叉编译是正常的,push进平板,运行/testmux,打印hello, Serial port is OK,就一直等待,超时后退出。
我怀疑是消息格式的问题,通过在电脑侧发送命令,查看系统日志,发现该串口设备有两个口,一进一出
generic ttyGS1: dual speed IN/ep5in OUT/ep4out

我发现这个串口的操作顺序是反的,它先读取我发送的消息,再写产生的结果。我现在想知道怎么让串口读取指定的字符,等待高手指教~~

消息读取和写日志信息:
[0.0 in_irq swapper][ep4out,>>>count=7]
<3>[2014:05:20 02:52:32] [ 264.779581] [0.0 in_irq swapper]41 54 2B 43 47 4D 49 AT+CGMI
<3>[2014:05:20 02:52:36] [ 268.540791] [0.0 in_irq swapper]gs_read_complete: status 0
<3>[2014:05:20 02:52:36] [ 268.548361] [0.0 in_irq swapper][ep4out,>>>count=1]
<3>[2014:05:20 02:52:36] [ 268.555022] [0.0 in_irq swapper]0D .
<3>[2014:05:20 02:52:36] [ 268.572345] [1328.1 mspAppDS]gs_write: ttyGS1 (d3a20840) writing 16 bytes
<3>[2014:05:20 02:52:36] [ 268.581117] [1328.1 mspAppDS][ttyGS1,<<<count=16]
<3>[2014:05:20 02:52:36] [ 268.587590] [1328.1 mspAppDS]0D 0A 68 75 61 77 65 69 0D 0A 0D 0A 4F 4B 0D 0A ..manufactureinfo....OK..
<3>[2014:05:20 02:52:36] [ 268.602452] [0.0 in_irq swapper]gs_write_complete:
<3>[2014:05:20 02:52:36] [ 268.609161] [0.0 in_irq swapper][ep5in,<<<count=16]
<3>[2014:05:20 02:52:36] [ 268.615777] [0.0 in_irq swapper]0D 0A 68 75 61 77 65 69 0D 0A 0D 0A 4F 4B 0D 0A ..manufactureinfo....OK..
...全文
106 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeky_zhang2013 2014-05-20
  • 打赏
  • 举报
回复
自己顶一下啊,难到是发送和接收不能在同一个文件中么?

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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