[問題] linux使用serial port
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1446688669.A.A31.html
→
11/05 16:41, , 1F
11/05 16:41, 1F
推
11/05 16:50, , 2F
11/05 16:50, 2F
→
11/05 16:52, , 3F
11/05 16:52, 3F
→
11/05 16:56, , 4F
11/05 16:56, 4F
#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
推
11/06 22:07, , 5F
11/06 22:07, 5F
推
11/07 20:13, , 6F
11/07 20:13, 6F
→
11/07 20:14, , 7F
11/07 20:14, 7F
→
11/18 20:09, , 8F
11/18 20:09, 8F