linux pc中的rs 232 pin配置 [英] rs 232 pin configuration in linux pc

查看:11
本文介绍了linux pc中的rs 232 pin配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多例子展示了如何通过电脑的串口进行通信.但是有没有办法配置 rs 232 的引脚?我只需要将 tx 引脚设置一段时间,然后将其重置,依此类推.有没有办法找到rs 232的引脚地址?谢谢你.如果有地址,那么我们如何访问引脚或更改该地址中引脚的状态?

There are a lot of examples which shows on how to communicate through the serial port of the pc. But is there a way to configure the pins of rs 232? I just need to set the tx pin for some time and then reset it and so on. is there a way to find the address of the pins of rs 232? Thank you. If there is an address then how can we access the pin or change the state of the pin in that address?

推荐答案

控制引脚

对于其他引脚 DTR CTS 等,您需要使用 ioctl() 来切换引脚.

Control Pins

For the other pins DTR CTS etc, you will need to use ioctl() to toggle the pin.

这里是一个简单的例子(没有错误检查),为 DTR 行做这件事:

Here is a simple example (no error checking) to do that for the DTR line:

#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>

int f = open( "/dev/ttyS0", O_RDWR | O_NOCTTY);
int pins;    
ioctl( f, TIOCMGET, &pins);
pins = pins | TIOCM_DTR;
ioctl( f, TIOCMSET, &pins) // the order you do this depends 
sleep(1);
ioctl( f, TIOCMGET, &pins);
pins = pins & ~TIOCM_DTR;
ioctl( f, TIOCMSET, &pins)

open 和 tty_ioctl 的手册页中描述了各种标志

The various flags are described in the man page for open and tty_ioctl

使用 TX 引脚可能有点棘手;理论上输出通常为 1,但是您可以设置一个中断"一段时间,将其设置为 0.您可能可以使用以下内容,但我还没有尝试过:

Using the TX pin is probably a bit tricker; in theory the output is normally 1, but then you can set a 'break' for a period of time which set it to 0. You could probably use the following, but I havent tried it:

ioctl( f, TIOCSBRK)

注意

请注意,在传统的 rs232 中,电平名义上为 +/- 12v(介于 +/-3,15V 之间),其中负数为 1,正数为零,这可能与您的预期相反.但是现在很多串口使用TTL或3v3电平代替.

Caution

Note that in traditional rs232 the levels are notionally +/- 12v ( between +/-3,15V) where negative is 1 and positive is zero, which might be the opposite of what you are expecting. But these days a lot of serials port use TTL or 3v3 levels instead.

我在使用 DTR 作为输出 GPIO 的应用程序中使用了上述内容;请记住根据需要使用适当的电阻器或其他缓冲,以免炸毁 PC 串行端口.

I used the above in an application where we used DTR as an output GPIO; remember to use appropriate resistors or other buffering as needed, so you don't blow up your PC serial port.

带有 USB 串行加密狗的 YMMV.

YMMV with USB serial dongles.

这篇关于linux pc中的rs 232 pin配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆