首页 > 解决方案 > Serial communication with Arduino Uno in hexadecimal, and monitoring response with serial monitor

问题描述

I have a project where I need to send an array of hex bytes from an Arduino Uno to a third party device through serial communication, the device performs an action, and replies with an array of bytes, also in hex after a short time (+-500ms).

A third-party USART USB interface is used to record the response from the third-party device to a computer, with a Bluetooth link between the third-party device TX and the USART RX. The Arduino TX and third-party device RX are wired.

Now the problematic bit:

My project requires that the Arduino also captures the response from the third-party device, processes the response, and depending on the value, triggers different outputs (LED's).

I also need the ability to debug the Arduino, to make sure that the hex values that it receives are being processed correctly and that the output is accordingly triggered.

The code that I currently have (compiling, but not working as I anticipate it) is hown below:

byte one_shot_FAST[] = {0xAA, 0x00, 0x00, 0x20, 0x00, 0x01, 0x00, 0x02, 0x23};

int IncomingByte = 0;

uint8_t q1 = 0;
uint8_t q2 = 0;
uint16_t qtot = 0;
int qVal = 0;
int qValMax = 65535;

const int triggerPin = 2;
int triggerState = LOW;


void setup() {

 Serial.begin(19200);
 pinMode(triggerPin, INPUT);

     Serial.println("<Arduino is ready>");
 
}

void loop() {

triggerState = digitalRead(triggerPin);

if (triggerState == HIGH){
  Serial.write(&one_shot_FAST[0], sizeof(one_shot_FAST));
  //while(!Serial.available());
  IncomingByte = Serial.read();

for (int i=0; i<13; i++) {
  while(!Serial.available()); //wait for character
  IncomingByte = Serial.read();
  Serial.println(IncomingByte,HEX);
delay(10);
}

I expect the following response: AA 00 00 22 00 03 00 00 03 2B 01 E1 35

but I get a string of "#" and a square block after each "#"

This is what the schematic looks like: Architecture diagram

标签: arduinohexarduino-unouart

解决方案


让我们剖析你的问题:

我正在尝试通过串行通信向设备发送 HEX 线,然后读回响应...

你不说是什么设备,你必须假设所说的设备既不是你后面说的 Arduino,也不是电脑。

...同时监控交易。

除非您有一个多点串行链路(例如 RS485),否则这是不可能的。如果您想监控串行链路,您需要从任一侧执行此操作,或使用特殊设备(硬件嗅探器)或使用几个额外的串行端口实现您自己的嗅探器。

...但我没有通过 USB 读取正确的传输和任何响应。

如果您的 Arduino 通过 USB 连接到您的计算机,在一般情况下(您可能不是,但您不想解释),您已经为此目的使用了 Arduino 的硬件串行端口(计算机上的串行监视器) .

那么,“设备”在哪里连接到您的 Arduino?如果您的意思是您已将其连接到引脚 0 和 1,而您通过 USB 将 Arduino 连接到您的计算机,那么我们发现了您的问题。

官方文档

在 Uno、Nano、Mini 和 Mega 上,引脚 0 和 1 用于与计算机通信。将任何东西连接到这些引脚可能会干扰该通信,包括导致上传到板失败。

最后,你说:

...尽管外部 USART 适配器确实显示了正确的响应。

显然,您的意思是您正在使用 USB 转串口适配器将您的“设备”连接到计算机,而 Arduino 根本没有干预。我猜你的意思是你有一个终端程序,比如 Windows 上的 PuTTY 或 RealTerm 或 Linux 上的 Minicom,你打开一个到虚拟串行端口的会话(在你连接电缆时创建),输入命令并按 intro。如果是这种情况,请注意您使用 Arduino 编写的命令不会以 CR 或 LF 终止。您的设备是否需要线路终端?只能猜测...

现在使用您的代码。不知道你想做什么,或者有很多猜测填补了空白,给你很多反馈并不容易。

但这条线似乎特别神秘:

Serial.write(&one_shot_FAST[0], sizeof(one_shot_FAST));

作为one_shot_FAST一个字节数组(出于所有目的,一个指针),所以当您使用时,&one_shot_FAST[0]您要求写入数组的第一个元素在内存中占用的地址。我不确定您对此有何想法,但我认为您可能希望放弃&发送实际字节的命令。

您的代码中还有其他奇怪的东西:您写入端口并立即(甚至可能在数据离开 TX 缓冲区之前)读取一个字节。我想这会导致Serial.read()回馈-1

编辑:我已经测试了您的代码并且写入部分(显然我没有硬件来回答命令)工作正常。您看到的正方形和破折号很可能是由于您使用的是 Arduino 串行控制台,它试图将大于 127 的 HEX 值显示为 ASCII,因此它不知道如何显示它们。如果您使用能够显示 HEX 的终端,该命令将正确显示。而且,您当然会遇到尝试将两个以上的设备连接到总线的问题。


推荐阅读