[問題] RS232

看板LinuxDev作者 (^__^)時間17年前 (2007/11/08 09:07), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串1/1
小弟現在寫了一支,利用RS232讓兩塊板子互相溝通的程式 下面是我的程式碼 #include <stdlib.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <limits.h> #define SERIAL_PORT "/dev/ttyS1" #define BAUD_RATE B115200 int main() { int fd, end_of_comm=1; struct termios termios_current, termios_new; /* Open Serial Port */ if( (fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0 ) { /* Check Port Name */ printf("Error when open serial port file:%s\n", SERIAL_PORT); return 0; } tcgetattr(fd, &termios_current); /* Setup Serial Port */ bzero(&termios_new, sizeof(termios_new)); // Clear Setting cfmakeraw(&termios_new); termios_new.c_cflag = BAUD_RATE; // Baud Rate termios_new.c_cflag |= CLOCAL | CREAD; termios_new.c_cflag &= ~CSIZE; termios_new.c_cflag |= CS8; // 8bit termios_new.c_cflag &= ~PARENB; // no parity bit termios_new.c_cflag &= ~CSTOPB; // no stop bit tcflush(fd, TCIFLUSH); /* Set to Hardware Register */ tcsetattr(fd, TCSANOW, &termios_new); /* Start Communication */ while (end_of_comm) { int rec; char buf[129]; rec = read(fd, buf, 128); buf[rec] = '\0'; printf("Recv: %s\nSent: ", buf); scanf("%s", buf); rec = write(fd, buf, strlen(buf)); if ( buf[0] == '%' ) end_of_comm = 0; } tcsetattr(fd, TCSANOW, &termios_current); return 0; } 但是,兩塊板子用跳線連接後,並不如預期中的能互相傳送東西 請問各位大大們,能幫幫我嗎??我不知道問題是出在哪 兩塊板子都是執行這支程式!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.129.20.96

11/08 23:39, , 1F
nonblocking的read在沒讀到東西時,return -1,設定EAGAIN
11/08 23:39, 1F

11/08 23:42, , 2F
所以buf[rec]會segmentation fault嗎?
11/08 23:42, 2F

11/08 23:43, , 3F
另外baud rate是否仍須用cfset[io]speed()來設定?
11/08 23:43, 3F
文章代碼(AID): #17Cc4vOs (LinuxDev)
文章代碼(AID): #17Cc4vOs (LinuxDev)