open_port: Permission denied
"Cannot open /dev/ttyS?: Permission denied"
(2006-09-08 09:37:17)
means that everyone has read and write permission on it. Use "chmod" to change permissions. There are more complicated (and secure) ways to get access like belonging to a "group" that has group permission. Some programs change the permissions when they run
but restore them when the program exists normally. But if someone pulls the plug on your PC it's an abnormal exit and correct permissions may not be restored.
Unless stty is set for clocal, the CD pin may need to be asserted in order to open a serial port. If the physical port is not connected to anything, or if it's connected to something that is not powered on (such an external modem) then there will be no voltage
on CD from that device. Thus the "cannot open" message. Either set clocal or connect the serial port connector to something and power it on.
Even if a device is powered on and connected to a port, it may sometimes prevent opening the port. An example of this is where the device has negated CD and the CD pin on your PC is negated (negative voltage).
"Operation not supported by device" for ttyS?
This means that an operation requested by setserial, stty, etc. couldn't be done because the kernel doesn't support doing it. Formerly this was often due to the "serial" module not being loaded. But with the advent of PnP, it may likely mean that there is no
modem (or other serial device) at the address where the driver (and setserial) thinks it is. If there is no modem there, commands (for operations) sent to that address obviously don't get done. See What
is set in my serial port hardware?
If the "serial" module wasn't loaded but "lsmod" shows you it's now loaded it might be the case that it's loaded now but wasn't loaded when you got the error message. In many cases the module will automatically loaded when needed (if it can be found). To force
loading of the "serial" module it may be listed in the file: /etc/modules.conf or /etc/modules. The actual module should reside in: /lib/modules/.../misc/serial.o.
-
下面是一个简单的读取串口数据的例子
#include<stdio.h> /*标准输入输出定义*/
#include<stdlib.h> /*标准函数库定义*/
#include<unistd.h> /*Unix标准函数定义*/
#include<sys/types.h> /**/
#include<sys/stat.h> /**/
#include<fcntl.h> /*文件控制定义*/
#include<termios.h> /*PPSIX终端控制定义*/
#include<errno.h> /*错误号定义*/ #define
FALSE -1
#defineTRUE 0 /***@brief
设置串口通信速率
*@paramfd 类型 int 打开串口的文件句柄
*@paramspeed 类型 int 串口速度
*@returnvoid*/ int
speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
intname_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300, };
voidset_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);