使用ioctl()控制串口的DTR RTS引脚在linux中调用 [英] Controlling DTR RTS pins of serial port using ioctl() Call in linux

查看:1257
本文介绍了使用ioctl()控制串口的DTR RTS引脚在linux中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在编写一个小代码来控制Linux上的USB到串口转换器芯片FT232的DTR和RTS线路(Mint Linux 13 Maya,x86)。



我已成功编写代码,使用termios读写数据到FT232芯片。现在我想控制DTR和RTS线,所以我使用ioctl()调用来设置和清除DTR和RTS线。



这里是代码



Hi i am writing a small code to control the DTR and RTS lines of USB to Serial port Converter chip FT232 on Linux (Mint Linux 13 Maya,x86).

I have successfully wrote code to read and write data to the FT232 chip using termios. Now i want to control the DTR and RTS lines so i am using ioctl() call to set and clear the DTR and RTS lines.

here is the code

#include <stdio.h>
    #include <fcntl.h>       /* File Control Definitions           */
    #include <termios.h>     /* POSIX Terminal Control Definitions */
    #include <unistd.h>      /* UNIX Standard Definitions          */
    #include <errno.h>       /* ERROR Number Definitions           */
    #include <sys/ioctl.h>   /* ioctl()                            */

    main(void)
    {
        int fd;     /*File Descriptor*/
        int status;

        fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY ); //Opening the serial port

        ioctl(fd,TIOCMGET,&status); /* GET the State of MODEM bits in Status */
        status |= TIOCM_RTS;        // Set the RTS pin
        ioctl(fd, TIOCMSET, status);

        getchar(); //To view the change in status pins before closing the port

        close(fd);
     }





代码在gcc上成功编译没有任何错误。我已将两个LED连接到FT232的RTS和DTR线。由于RTS和DTR线被反转,设置RTS会使LED关闭。 Led连接到RTS和DTR都是ON。



使用sudo ./serial运行代码



RTS和DTR Led都关闭,而不仅仅是RTS(编码状态为| = TIOCM_RTS;)并在getchar()之后打开。



为什么DTR与RTS线一起变低?



我无法使用TIOCM_CD,TIOCM_DTR等更改RI,DCD,DCD,DTR等其他调制解调器线路?

推荐答案

默认情况下,您的设备似乎配置为手抖动。您可以(并且应该)使用 tcsetattr 来配置您的设备。您也可以尝试使用 TIOCMBIS ioctl命令设置DTR和RTS状态。



您只能使用设置输出引脚RTS和DTR的状态。只能读取输入引脚DCD,DSR,RI和CTS的状态。



你应该阅读 Linux Serial HOWTO [ ^ ]和Linux Serial Programming HOWTO [ ^ ]。
It seems that your device is configured for hand shaking by default. You can (and should) use tcsetattr to configure your device. You may also try to use the TIOCMBIS ioctl command to set the DTR and RTS state.

You can only set the state of the output pins RTS and DTR. The state of the input pins DCD, DSR, RI, and CTS can only be read.

You should read the Linux Serial HOWTO[^] and the Linux Serial Programming HOWTO[^].


谢谢,

使用TIOCMBIS ioctl命令设置DTR和RTS状态解决了问题
thanks,
using TIOCMBIS ioctl command to set the DTR and RTS state solved the issue


这篇关于使用ioctl()控制串口的DTR RTS引脚在linux中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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