向串口发送数据的方式

1、远程发送,使用libssh

如Windows通过ssh向Arm板发送指令,Arm板依据指令向自己的串口ttyTHS1发送数据。

2、本地发送,使用QSerialPort 

如Arm板向自己的串口ttyTHS1发送数据。

以向 /dev/ttyTHS1 串口发送 0xAA为例,DeepSeek示例代码如下

#include <QSerialPort>
#include <QDebug>

void sendHexViaSerial(const QString& portName, uint8_t byte) {
    QSerialPort serial;
    serial.setPortName(portName);  // "/dev/ttyTHS1"
    serial.setBaudRate(QSerialPort::Baud115200);  // 根据设备调整
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);

    if (!serial.open(QIODevice::WriteOnly)) {
        qDebug() << "Failed to open serial port:" << serial.errorString();
        return;
    }

    char data = static_cast<char>(byte);  // 0xAA
    if (serial.write(&data, 1) == -1) {
        qDebug() << "Failed to write data:" << serial.errorString();
    } else {
        qDebug() << "Sent:" << QByteArray(1, data).toHex();
    }

    serial.close();
}

int main() {
    sendHexViaSerial("/dev/ttyTHS1", 0xAA);
    return 0;
}

 

posted @ 2025-04-12 16:56  夕西行  阅读(63)  评论(0)    收藏  举报