如何更改 LoRa 设备的波特率?

How do I change the baud rate for a LoRa device?

我有一个 LoRa 设备,它在 raspberry pi 的 USB 端口上使用默认波特率 57600。我想尝试更快的串行波特率。从设备睡眠状态返回后更改波特率的说明如下来自数据表:

我有一个主要问题是我不知道串口通信中的'break'代码是什么。我尝试了各种方法,包括以下内容(其中 usb_port 是打开端口的文件描述符):

ioctl(usb_port, TCSBRK, 0);

std::string msg;
msg="0X00";
write(usb_port, msg.c_str(), msg.size());

都没有运气。谁能看一下这个 c++ 函数并告诉我哪里出错了?

从文档的其他地方,指出写入“0x55”会在从睡眠返回时触发自动波特率检测设置。

注意:“/dev/ttyUSBPort1”的无线电设备文件描述符是对使用 udev 串行规则设置的 /dev/ttyUSB0 的引用。

const char* radiodevice = "/dev/ttyUSBPort1";
int openPort(void) {
    struct termios tty;
    memset(&tty, 0, sizeof tty);
    if ((usb_port = open(radiodevice, O_RDWR | O_NOCTTY | O_SYNC))>=0) {// | O_NOCTTY | O_SYNC
        std::cout << "DEVICE OPENED: " << radiodevice << " handle number: " << usb_port << std::endl;
    } else {
        fprintf(stderr, "unable to open serial radiodevice");
        return -1;
    }
    if(tcgetattr(usb_port, &tty) != 0) {
        printf("Error %i \n", errno);
    }
    cfsetispeed(&tty, B57600);
    cfsetospeed(&tty, B57600);
    tty.c_cflag     &=  ~PARENB;            // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;
    tty.c_cflag     &=  ~CRTSCTS;           // no flow control
    tty.c_cc[VMIN]   =  0;                  // read doesn't block
    tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
    tty.c_iflag &= ~ISTRIP;
    tty.c_iflag &= ~INLCR;
    tty.c_iflag &= ~IGNCR;
    tty.c_iflag &= ~ICRNL;
    tty.c_oflag &= OPOST;
    tty.c_lflag &= ICANON;
    tcflush(usb_port,TCIOFLUSH);
    usleep(10000);
    if (tcsetattr(usb_port, TCSANOW, &tty) != 0) {
        printf("Error %i\n", errno);
    }

    //FLASH UNIT LED LIGHTS TO CONFIRM INITIAL CONNECTION
    std::string msg="sys set pindig GPIO11 1\r\n";
    write(usb_port, msg.c_str(), msg.size());
    usleep(1000000);
    msg="sys set pindig GPIO11 0\r\n";
    write(usb_port, msg.c_str(), msg.size());
    usleep(10000);

    //WRITE SLEEP COMMAND TO DEVICE FOR 0.5 SECONDS
    std::cout << "WRITING SLEEP COMMAND" << std::endl;
    msg="sys sleep 5000\r\n";//
    write(usb_port, msg.c_str(), msg.size());
    usleep(100000);

    //ATTEMPT TO SEND BREAK COMMAND
    ioctl(usb_port, TCSBRK, 0);
    usleep(500000);

    //RESET SERIAL BAUD RATE
    cfsetispeed(&tty, B115200);
    cfsetospeed(&tty, B115200);

    //SET DEVICE TO AUTO-BAUD RATE DETECTION
    std::cout << "SENDING 0x55" << std::endl;
    msg="0x55";
    write(usb_port, msg.c_str(), msg.size());
    usleep(2000000);

    //FLASH UNIT LED LIGHTS TO CONFIRM FINAL CONNECTION AT NEW BAUD RATE
    std::cout << "BLINKING LIGHTS" << std::endl;
    msg="sys set pindig GPIO11 1\r\n";
    write(usb_port, msg.c_str(), msg.size());
    usleep(1000000);
    msg="sys set pindig GPIO11 0\r\n";
    write(usb_port, msg.c_str(), msg.size());

    return usb_port;
}

对于那些可能正在寻找此解决方案的人,这是我完成将 LoRa 设备设置为自动波特率模式并以新波特率打开端口的方法:

    int openPort(void) {
        struct termios tty;
        memset(&tty, 0, sizeof tty);
        //OPEN PORT WITH DEFAULT BAUD RATE 57600
        if ((usb_port = open(radiodevice, O_RDWR | O_NOCTTY | O_SYNC))>=0) {// | O_NOCTTY | O_SYNC
            std::cout << "DEVICE OPENED: " << radiodevice << " handle number: " << usb_port << std::endl;
        } else {
            fprintf(stderr, "unable to open serial radiodevice");
            return -1;
        }
        if(tcgetattr(usb_port, &tty) != 0) {
            printf("Error %i \n", errno);
        }
        cfsetispeed(&tty, B57600);
        cfsetospeed(&tty, B57600);
        tty.c_cflag     &=  ~PARENB;            // Make 8n1
        tty.c_cflag     &=  ~CSTOPB;
        tty.c_cflag     &=  ~CSIZE;
        tty.c_cflag     |=  CS8;
        tty.c_cflag     &=  ~CRTSCTS;           // no flow control
        tty.c_cc[VMIN]   =  0;                  // read doesn't block
        tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
        tty.c_iflag &= ~ISTRIP;
        tty.c_iflag &= ~INLCR;
        tty.c_iflag &= ~IGNCR;
        tty.c_iflag &= ~ICRNL;
        tty.c_oflag &= OPOST;
        tty.c_lflag &= ICANON;
        tcflush(usb_port,TCIOFLUSH);
        //usleep(10000);
        if (tcsetattr(usb_port, TCSANOW, &tty) != 0) {
            printf("Error %i\n", errno);
        }

        //FLASH UNIT LED LIGHTS TO CONFIRM INITIAL CONNECTION
        std::string msg="sys set pindig GPIO11 1\r\n";
        write(usb_port, msg.c_str(), msg.size());
        usleep(1000000);
        msg="sys set pindig GPIO11 0\r\n";
        write(usb_port, msg.c_str(), msg.size());
        usleep(10000);

        //WRITE SLEEP COMMAND TO DEVICE FOR 0.5 SECONDS
        std::cout << "WRITING SLEEP COMMAND" << std::endl;
        msg="sys sleep 5000\r\n";
        write(usb_port, msg.c_str(), msg.size());
        usleep(100000);

        close(usb_port);

        //OPEN PORT AT NEW BAUD RATE
        if ((usb_port = open(radiodevice, O_RDWR | O_NOCTTY | O_SYNC))>=0) {// | O_NOCTTY | O_SYNC
            std::cout << "DEVICE OPENED: " << radiodevice << " handle number: " << usb_port << std::endl;
        } else {
            fprintf(stderr, "unable to open serial radiodevice");
            return -1;
        }
        if(tcgetattr(usb_port, &tty) != 0) {
            printf("Error %i \n", errno);
        }

        //RESET SERIAL BAUD RATE
        cfsetispeed(&tty, B230400);<---SET NEW BAUD RATE VALUE
        cfsetospeed(&tty, B230400);<---SET NEW BAUD RATE VALUE
        tty.c_cflag     &=  ~PARENB;            // Make 8n1
        tty.c_cflag     &=  ~CSTOPB;
        tty.c_cflag     &=  ~CSIZE;
        tty.c_cflag     |=  CS8;
        tty.c_cflag     &=  ~CRTSCTS;           // no flow control
        tty.c_cc[VMIN]   =  0;                  // read doesn't block
        tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
        tty.c_iflag &= ~ISTRIP;
        tty.c_iflag &= ~INLCR;
        tty.c_iflag &= ~IGNCR;
        tty.c_iflag &= ~ICRNL;
        tty.c_oflag &= OPOST;
        tty.c_lflag &= ICANON;
        tcflush(usb_port,TCIOFLUSH);
        usleep(10000);
        if (tcsetattr(usb_port, TCSANOW, &tty) != 0) {
            printf("Error %i\n", errno);
        }

        //SEND BREAK COMMAND
        std::cout << "SENDING BREAK COMMAND" << std::endl;
        tcsendbreak(usb_port, 0);
        usleep(10000);

        //SET DEVICE TO AUTO-BAUD RATE DETECTION
        msg = "U";//CORRESPONDS TO DOCUMENTED CHARACTER 0x55
        write(usb_port, msg.c_str(), msg.size());

        //FLASH UNIT LED LIGHTS TO CONFIRM FINAL CONNECTION AT NEW BAUD RATE
        std::cout << "BLINKING LIGHTS" << std::endl;
        msg="sys set pindig GPIO11 1\r\n";
        write(usb_port, msg.c_str(), msg.size());
        usleep(1000000);
        msg="sys set pindig GPIO11 0\r\n";
        write(usb_port, msg.c_str(), msg.size());

        return usb_port;
    }