macos - 串行端口挂起

标签 macos unix serial-port posix ftdi

我有一个 USB 到串行 FTDI 适配器连接到我的 mac。我可以使用以下命令:

screen /dev/tty.usbserial-A601L9OC

这会为端口打开一个串行终端,一切正常。但是当我尝试通过以下命令向串口发送字符时:
root# echo 'a' > /dev/tty.usbserial-A601L9OC 

命令挂起,什么也没有发送。当我尝试在 c 程序中连接到它时,会发生类似的事情。程序在尝试打开串口时挂起:
int fd = open("/dev/tty.usbserial-A601L9OC", O_RDWR | O_NOCTTY | O_SYNC);

当我在端口上运行 stty 时,它会打印:
root# stty -f  /dev/tty.usbserial-A601L9OC
speed 9600 baud;
lflags: -icanon -isig -iexten -echo
iflags: -icrnl -ixon -ixany -imaxbel -brkint
oflags: -opost -onlcr -oxtabs
cflags: cs8 -parenb

这看起来是正确的。有没有人知道为什么这些命令在连接到串行端口时挂起并且从不发送但屏幕工作正常?任何帮助将不胜感激。

更新:从 stty 获取信息的结果
bash-3.2# stty -a -f /dev/tty.usbserial-A601L9OC
speed 9600 baud; 0 rows; 0 columns;
lflags: -icanon -isig -iexten -echo -echoe -echok -echoke -echonl
-echoctl -echoprt -altwerase -noflsh -tostop -flusho -pendin
-nokerninfo -extproc
iflags: -istrip -icrnl -inlcr -igncr -ixon -ixoff -ixany -imaxbel -iutf8
-ignbrk -brkint -inpck -ignpar -parmrk
oflags: -opost -onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;

最佳答案

使用设备 /dev/cu.usbserial-A601L9OC相反,并将速度也设置为 9600 波特。这是来自 Magnus' macrumor post 的示例:

strcpy(bsdPath, "/dev/cu.usbserial-A601L9OC");
fileDescriptor = open(bsdPath, O_RDWR);
if (-1 == fileDescriptor)
{
return EX_IOERR;
}

struct termios theTermios;
memset(&theTermios, 0, sizeof(struct termios));
cfmakeraw(&theTermios);
cfsetspeed(&theTermios, 9600);
theTermios.c_cflag = CREAD | CLOCAL;     // turn on READ and ignore modem control lines
theTermios.c_cflag |= CS8;
theTermios.c_cc[VMIN] = 0;
theTermios.c_cc[VTIME] = 10;     // 1 sec timeout
int ret = ioctl(fileDescriptor, TIOCSETA, &theTermios);

ret = read(fileDescriptor, &c, 1);

关于macos - 串行端口挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18821811/

相关文章:

shell - 如何从一个位置参数生成两个变量?

linux - 无法通过串口读取数据

python - pySerial readline 读取之前写入的数据

cocoa - Cocoa 中有一个框架来创建和操作 ISO 磁盘镜像吗?

macos - NSTableView:当 float subview 被删除时收到通知

c - 查找和计算大写和小写字符

linux - 让两个 Linux(虚拟)盒子通过串口通话

iphone - iPhone SDK 模拟器的推荐系统要求是什么?

c++ - 在 mac 上用 system( ) 打开 excel 文件?

c - 调用 printf() 时出现 EXC_BAD_ACCESS