【发布时间】:2021-06-05 13:40:20
【问题描述】:
我一直在用 WiringPi 在 C++ 中测试 UART 通信。
问题:
C++ 似乎没有将整个数据输出到 UART 端口/dev/ttyAMA0。也许我做错了?
调查:
注意:我正在使用 minicom,minicom --baudrate 57600 --noinit --displayhex --device /dev/ttyAMA0 来检查接收到的数据。
还有! UART 端口、RX 和 TX 引脚短接在一起。
python 代码运行良好,但是当我尝试在 C++ 中实现它时,收到的数据不同。
预期收到的数据应该是:ef 01 ff ff ff ff 01 00 07 13 00 00 00 00 00 1b。
接收数据比较:
| Language Used | Data Received from Minicom |
|---|---|
| Python | ef 01 ff ff ff ff 01 00 07 13 00 00 00 00 00 1b |
| C++ | ef 01 ff ff ff ff 01 |
使用的代码
Python:
import serial
from time import sleep
uart = serial.Serial("/dev/ttyAMA0", baudrate=57600, timeout=1)
packet = [239, 1, 255, 255, 255, 255, 1, 0, 7, 19, 0, 0, 0, 0, 0, 27]
uart.write(bytearray(packet))
C++:
注意:使用g++ -Wall -O3 -o test hello.cpp -lwiringPi编译的代码
#include <iostream>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>
using namespace std;
int main()
{
int serial_port;
if (wiringPiSetup() == -1)
exit(1);
if ((serial_port = serialOpen("/dev/ttyAMA0", 57600)) < 0)
{
fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
return 1;
}
cout << "Sending data to UART" << std::endl;
serialPuts(serial_port, "\xef\x01\xff\xff\xff\xff\x01\x00\x07\x13\x00\x00\x00\x00\x00\x1b");
return 0;
}
【问题讨论】: