Linux
下串口编?/p>
参考:
1. POSIX
操作系统串口编程指南
2. UNIX
环境高级编程
?/p>
Linux
下,标准的串口设备节点名?/p>
/dev/ttyS*
,如果是
USB
转串口,则为
/dev/ttyUSB*
,其?/p>
'*'
代表
0
?/p>
1...
这类数字?/p>
一、访问串?/p>
1
打开串口
打开串口使用
open
系统调用,例如:
#include <stdio.h>
/* Standard input/output definitions */
#include <string.h>
/* String function definitions */
#include <unistd.h>
/* UNIX standard function definitions */
#include <fcntl.h>
/* File control definitions */
#include <errno.h>
/* Error number definitions */
#include <termios.h>
/* POSIX terminal control definitions */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on sueess or -1 on error.
*/
int
open_port(void)
{
int fd;
/* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.