0

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

Adam
  • 71
  • 9
  • Can you please explain what you mean by external USART adapter? Are you connecting your Arduino to a computer's USB port? By monitoring the transaction you mean on a terminal software on that computer? Do you see the "" sentence correctly? – Marcos G. Jul 04 '19 at 19:40
  • One more question: why do you read once and then 14 times more? – Marcos G. Jul 04 '19 at 20:20
  • external USART adapter = USB to UART adapter. – Adam Jul 04 '19 at 20:33
  • I see the sentence correctly on the Arduino serial monitor, but the rest is a mess. I am connecting the Arduino by USB to my computer. I read each byte individually, as I only want to process byters 10 and 11 at this stage, I need to store them in variables, but that is not shown in the code. At this stage I would be happy to just have the string displayed correctly. – Adam Jul 04 '19 at 20:36

1 Answers1

0

Let us dissect your question:

I am trying to send a HEX line to a device via serial communication, and read the response back...

You don't say what device, one has to assume the said device is neither the Arduino you talk about later nor a computer.

...while monitoring the transaction.

This is simply not possible unless you have a multipoint serial link (RS485, for instance). If you want to monitor a serial link you need to do it from either side or use a special piece of equipment (hardware sniffer) or implement your own sniffer using a couple of additional serial ports.

... but I am not reading the correct transmission and any response through USB.

If your Arduino is connected to your computer via USB, in the general scenario (which you might not be but you did not care to explain), you're already using the Arduino's hardware serial port for that purpose (serial monitor on your computer).

So, where is "the device" connected to your Arduino? If you mean you have connected it to pins 0 and 1 while you keep the Arduino connected to your computer through USB then we have found your problem.

From the official documentation:

On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.

Finally, you say:

...although an external USART adapter does show the correct response.

Apparently, by that you mean you are connecting your "device" to the computer using a USB-to-serial adapter while the Arduino is not intervening at all. I guess you mean you have a terminal program like PuTTY or RealTerm for Windows or Minicom on Linux, you open a session to the virtual serial port (created when you connect your cable), type the command and press intro. If that's the case, be aware that the command you're writing with your Arduino is not terminated with CR or LF. Does your device require line termination? One can only guess...

Now with your code. Not knowing what you want to do, or with a lot of gaps filled by guessing, it's not really easy to give you much feedback.

But this line seems particularly enigmatic:

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

With one_shot_FAST being a byte array (for all purposes a pointer), so when you use &one_shot_FAST[0] you're asking to write the address that the first element of the array occupies in memory. I'm not sure of what is your idea about this, but I think you might want to drop the & to send the actual bytes on your command instead.

There are other odd things in your code: you write to the port and instantly (likely even before the data leaves for the TX buffer) you read one byte. I imagine that would result in Serial.read() giving back -1.

EDIT: I have tested your code and the write part (obviously I don't have the hardware to answer on the command) works fine. The square and dashes you see are most likely due to the fact that you are using the Arduino serial console, which is trying to display HEX values bigger than 127 as ASCII so it doesn't know how to display them. If you use a terminal able to display HEX the command will be correctly displayed. And, you have of course the problem of trying to connect more than two devices to the bus.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16
  • Thank you for responding Marcos. I'm editing my question to make it more clear to what I am doing/trying to achieve. – Adam Jul 05 '19 at 07:48
  • regarding the following line of code: `Serial.write(&one_shot_FAST[0], sizeof(one_shot_FAST));` It is working correctly, removing the `[0]` or `&` does not yield the correct response from the third party device. – Adam Jul 05 '19 at 08:10
  • Regarding the immediate response, I removed the first line of `while(!Serial.available());` (I put it back now and commented it out, sorry that was sloppy). If I have it as part of my active code, I never get a response from the device (or maybe I do but I am unable to see it on my serial monitor). – Adam Jul 05 '19 at 08:16
  • Hello Adam, thank you for adding the sketch. I have a better picture now. If you have two ports on your Arduino, one for your computer and another one for the 3rd device, why are you not instantiating a second port with `Serial1.begin(baudrate)`? And the TX line from the 3rd party device goes to two places apparently (RX on the Arduino and RX on one of the BT transceivers). You cannot do that, the serial bus is point-to-point, you cannot connect two devices in parallel. – Marcos G. Jul 05 '19 at 09:03
  • If you want to monitor the bus you can try [port forwarding](https://stackoverflow.com/a/56377015/11476836) if you have a couple of free ports on a computer. Otherwise, you can buy a bunch of RS485 transceivers and make your bus multipoint. Regarding the `Serial.write()`, I don't see how it can work, but I might be wrong. I'll think about it. – Marcos G. Jul 05 '19 at 09:05
  • You are right on the 'Serial.write()', you can give the array name or the address of its first element apparently. The documentation is not that good... I've tested your code with the simulator but there seems to be a problem with byte values bigger than 127 such that 0xAA is not written as it should. I guess this is just a simulator issue. I will edit my answer but the underlying issues of the share bus line and the two Arduino ports remain... – Marcos G. Jul 05 '19 at 11:27
  • I have added a couple of lines with the conclusions of the tests I made on my hardware. – Marcos G. Jul 05 '19 at 21:10