作者laladeer (laladeer)
看板C_and_CPP
标题[问题] linux使用serial port
时间Thu Nov 5 09:57:46 2015
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
linux
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
暂时没有
问题(Question):
在linux的系统上想与arduino通讯,参考网路资料,使用fopen的方式进行开启/dev/ttyA
CM0,用fprintf可以写入,arduino接收正确。
但是没有找到读取的相关资料,想请问我该用什麽方式去读取arduino回传的资讯,还是
需要用什麽其他的serial library?
--
Sent from my Android
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.124.249.110
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1446688669.A.A31.html
1F:→ james732: 要不要先用类似minicom的程式确认input/output都有通?11/05 16:41
2F:推 johnjohnlin: 不就 fread?11/05 16:50
3F:→ bdvstg: 我没用过 不过fread?11/05 16:52
4F:→ bdvstg: 阿....晚一步XD11/05 16:56
#include <sys/poll.h>
#include <sys/unistd.h>
#include <stdio.h>
#define SERIAL_DEV "/dev/ttyUSB0"
#define SPEED 9600
void read_port()
{
int serial_fd = 0;
struct pollfd fds[1];
fds[0].fd = serial_fd;
fds[0].events = POLLIN ;
int pollrc = poll( fds, 1, 1000);
if (pollrc < 0)
{
perror("poll");
}
else if( pollrc > 0)
{
if( fds[0].revents & POLLIN )
{
char buff[1024];
ssize_t rc = read(serial_fd, buff, sizeof(buff) );
if (rc > 0)
{
/* You've got rc characters. do something with buff */
printf("%d\n", buff);
}
else
{
printf("0\n");
}
}
}
}
int main()
{
int i = 0;
while (1)
if (1)
{
printf("%d : buff = \n", i);
read_port();
i++;
sleep(1);
}
else
{
sleep(1);
}
return 0;
}
※ 编辑: laladeer (140.124.249.110), 11/05/2015 22:27:32
5F:推 soso7885: 不设termios开吗? 11/06 22:07
6F:推 IhateOGC: Echo 0000>> /dev/com2 11/07 20:13
7F:→ IhateOGC: If Fopen !=0 echo busy 11/07 20:14
8F:→ laladeer: 谢谢 termios成功了 11/18 20:09