[問題] linux使用serial port

看板C_and_CPP作者 (laladeer)時間9年前發表 (2015/11/05 01:57), 9年前編輯推噓3(305)
留言8則, 6人參與, 最新討論串1/1
開發平台(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
要不要先用類似minicom的程式確認input/output都有通?
11/05 16:41, 1F

11/05 16:50, , 2F
不就 fread?
11/05 16:50, 2F

11/05 16:52, , 3F
我沒用過 不過fread?
11/05 16:52, 3F

11/05 16:56, , 4F
阿....晚一步XD
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
不設termios開嗎?
11/06 22:07, 5F

11/07 20:13, , 6F
Echo 0000>> /dev/com2
11/07 20:13, 6F

11/07 20:14, , 7F
If Fopen !=0 echo busy
11/07 20:14, 7F

11/18 20:09, , 8F
謝謝 termios成功了
11/18 20:09, 8F
文章代碼(AID): #1MEhUTen (C_and_CPP)