c++ - 如何设置和清除串口的RTS线。 C++ POSIX

标签 c++ ubuntu serial-port posix

我想控制串口上的RTS线来唤醒一个设备。这是一些粗略的测试代码来打开串口。我需要将 RTS 线路设置为低电平 500 毫秒。我无法通过谷歌搜索/理解如何控制线路。有什么提示吗?

编辑:或控制 CTS/DTR,除 Tx/Rx 线之外的任何其他线连接到机器人的引脚。

#include <iostream>
#include <string.h>
#include <cstdio>
#include <fstream>

#include <sys/types.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>   /* Standard types */
#include <fcntl.h>    /* File control definitions */
#include <errno.h>    /* Error number definitions */
#include <termios.h>  /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#include <getopt.h>



using namespace std;

int fd_global;
FILE* fp_global;
int serialport_init(const char* serialport, int baud);
int serialport_read_until(int fd);
int fpeek(FILE* stream);
bool fexists(const char *filename);


void* screen_thread(void* arg) {
    while(1) {
    }
}



int main(void) {

    pthread_t screen_thread_id;
    int flags;
    int baudrate = B9600;  // default
    fd_global = serialport_init((char*)"/dev/ttyUSB0", baudrate);
    if(fd_global==-1) {
        cout << "Open port: error\n";
        return -1;
    }

    fp_global = fdopen(fd_global, "r");


    if (-1 == (flags = fcntl(fd_global, F_GETFL, 0))) flags = 0;
    fcntl(fd_global, F_SETFL, flags | O_NONBLOCK);

    //pthread_create(&screen_thread_id, NULL, screen_thread, (void*)NULL);    

    serialport_read_until(fd_global);
    return 0;
}

int serialport_read_until(int fd)
{
    char b[1];
    while(1) { 
        int n = read(fd, b, 1);  //Read byte at a time
        if(n==-1) { 
            printf("N");fflush(stdout);
            perror("READ: ");
         }
        else if( n==0 ) {
            printf("Z");fflush(stdout);
        } else if(n>0) {

            printf(":%d:",n);fflush(stdout);


        }
        usleep(100*1000);

            if (fexists("/dev/ttyUSB0")) 
                printf("CONNECTED\n");
            else
                printf("NOT CONNECTED\n");

    }
    return 0;
}

bool fexists(const char *filename)
{
  ifstream ifile(filename);
  return ifile;
}



int serialport_init(const char* serialport, int baud)
{
    struct termios toptions;
    int fd;

    // Open port
    fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)  {
        perror("init_serialport: Unable to open port ");
        return -1;
    }

    // Read current termios settings
    if (tcgetattr(fd, &toptions) < 0) {
        perror("init_serialport: Couldn't get term attributes");
        return -1;
    }

    // Set baud rate variable
    speed_t brate = baud;
    switch(baud) {
    case 2400:   brate=B2400;   break;
    case 4800:   brate=B4800;   break;
    case 9600:   brate=B9600;   break;
#ifdef B14400
    case 14400:  brate=B14400;  break;
#endif
    case 19200:  brate=B19200;  break;
#ifdef B28800
    case 28800:  brate=B28800;  break;
#endif
    case 38400:  brate=B38400;  break;
    case 57600:  brate=B57600;  break;
    case 115200: brate=B115200; break;
    }
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);

    // Setup termios for 8N1
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;

    // Reccomended settings
    toptions.c_cflag &= ~CRTSCTS;   // no flow control
    toptions.c_cflag |= CREAD | CLOCAL;  // turn on read & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    toptions.c_oflag &= ~OPOST; // make raw

    // Setting when read() releases
    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html (Still a little confusing)
    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 20;

    // Apply settings
    if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
        perror("init_serialport: Couldn't set term attributes");
        return -1;
    }

    return fd;
}

最佳答案

这行得通吗?我明天会测试它。

int status;

status |= TIOCM_RTS;
ioctl(fd, TIOCMSET, &status);

usleep(500*1000);

status &= ~TIOCM_RTS;
ioctl(fd, TIOCMSET, &status);

关于c++ - 如何设置和清除串口的RTS线。 C++ POSIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15181870/

相关文章:

C++ Mastermind - 一种情况不起作用

c++ - 未定义的 pthread 引用甚至使用 lpthread 创建

linux - 将 TCP 数据重定向到 ttyS0

python - ubuntu 服务器上的文件写入权限

C#串口问题——太简单了,不过不行

c++ - 如何将虚拟 com 端口写入 TCP 驱动程序?

c++ - 调试时 Visual Studio 2013 崩溃?

c++ - Kinect for Windows v2 深度到彩色图像未对齐

ubuntu - 无法使用 Mono 运行 .net 应用程序 - 找不到 mscorlib.dll(版本不匹配?)

postgresql - 无法通过 Pgadmin 4 连接到远程服务器上的 PostgreSQL