//配置串列埠
sudo stty -f /dev/ttys1 115200 raw -echo -echoe -echok -crtscts
//更改許可權
sudo
chmod 777 /dev/ttys1
//向串列埠輸出資料
sudo
echo
"hello,world"
> /dev/ttys1
然後在另一主機的輸出端檢視輸出驗證是否正確。
cat /dev/ttys1
stty命令的用法請參考stty 命令說明及使用講解 - boyemachao的專欄 - csdn部落格
#include
#include
#include
#include
#include
#include
#include
#define bufflen 1024
#define rcvtimeout 2
intopenport
(int fd,
int comport)}if
(fcntl
(fd, f_setfl,0)
<0)
printf
("fcntl failed!\n");
else
printf
("fcntl=%d/n"
,fcntl
(fd, f_setfl,0)
);if(
isatty
(stdin_fileno)==0
)printf
("standard input is not a terminal device \n");
else
printf
("fd-open=%d\n"
, fd)
;return fd;
}int
setopt
(int fd,
int nspeed,
int nbits,
char nevent,
int nstop)
bzero
(&newtio,
sizeof
(newtio));
newtio.c_cflag |
= clocal | cread;
newtio.c_cflag &
=~csize;
switch
(nbits)
switch
(nspeed)
if(nstop ==1)
newtio.c_cflag &
=~cstopb;
else
if(nstop ==2)
newtio.c_cflag &
= cstopb;
newtio.c_cc[vtime]=0
; newtio.c_cc[vmin]=0
;tcflush
(fd, tciflush);if
((tcsetattr
(fd, tcsanow,
&newtio))!=
0)printf
("set done!\n");
return0;
}int
readdatatty
(int fd,
char
*rcv_buf,
int timeout,
int len)
else
if(retval)
}else
break;}
return pos;
}int
senddatatty
(int fd,
char
*send_buf,
int len)
return1;
}int
main()
if((isetopt =
setopt
(fdserial,
115200,8
,'n',1
))<0)
printf
("serial fdserial=%d\n"
, fdserial)
;tcflush
(fdserial,tcioflush)
;fcntl
(fdserial, f_setfl,0)
;char buffrcvdata[bufflen]=;
unsigned
int readdatanum =0;
while(1
)return1;
}
Linux下串列埠通訊
1.開啟串列埠 與其他的關於裝置程式設計的方法一樣,在linux下,操作 控制串列埠也是通過操作起裝置檔案進行的。在linux下,串列埠的裝置檔案是 dev ttys0或 dev ttys1等。因此要讀寫串列埠,我們首先要開啟串列埠 char dev dev ttys0 串列埠1 int fd op...
linux 下串列埠通訊
include include include include include include include include include define baudrate b115200 baud rate 115200 define device dev ttyama0 define size...
LINUX下串列埠通訊開發
摘要 1.開啟串列埠函式open port 中要實現的函式 1 open dev ttys0 o rdwr o noctty o ndelay 開啟串列埠0 2 fcntl fd,f setfl,0 恢復串列埠為阻塞狀態 3 isatty stdin fileno 測試是否為中斷裝置 非0即是中斷裝...