Serial communication

HI:

I have a simple code for commuinicating with a RS-232 device (i'm using a DollaTek MAX3232 board).
I can make the communicaciton work fine with an arduino "one" board, but when I try to use the MKR board, I get weird results(I get data, but the values are not what the are supose to be). This is the code (the code waits for the serial to have 63 bytes and the prints it in the serial monitor):

 byte asd[100];
void setup()
{
  Serial.begin(4800);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Goodnight moon!");
  Serial1.begin(4800);
}

void loop(){
  if (Serial1.available()) {
   Serial1.readBytesUntil('XX', asd, 63);
   for (int i = 0; i <= 63; i++){
  Serial.print(i+1);
  Serial.print(" / ");
  Serial.println(asd[i]);

   }
  }
}

I suspect it has something to do with the fact that MKR has only one port, and I using it also for displaying what I'm getting from the serial device...

Can anybody help?

        Serial1.readBytesUntil('XX', asd, 63);

The readBytesUntil() function take a single character as its first parameter. To make things worse you have tried to put 2 characters into a single byte so there are 2 reasons why this does not do what you think it does

1. Should 'XX' be --

"XX"
or
'X'
or
'\n' (C++ representation for Newline charcater)

3. Should byte asd[100]; be --
char asd[100];

Thanks for the help:

I don't know why, when using the "one" board, I was using
"XX"
But when using MKR1010 I get this error:

Compilation error: invalid conversion from 'const char*' to 'char' [-fpermissive]

Anyway, I have change the 'XX' to 'X', and still I'm getting the same strange resutls..

When using "char asd[100]" I get not valid answer:

1 / �

2 / �

3 / �

4 / �

5 / �

6 / �

7 / �

8 / �

9 / �

10 /

11 /

12 /

13 /

When I use byte asd[100], I get valid answer, is just that the numbers I get are not the correct ones

1 / 232

2 / 254

3 / 232

4 / 255

5 / 254

6 / 232

7 / 255

8 / 254

9 / 255

10 / 0

11 / 0

12 / 0

13 / 0

14 / 0

WHen using t the "one board", using the same code (except for the "XX" thing, I get the correct values.

1 / 128

2 / 67

3 / 21

4 / 0

5 / 0

6 / 0

7 / 0

8 / 18

9 / 0

10 / 0

11 / 0

12 / 0

13 / 5

14 / 0

15 / 6

Here is the code for the "one" board:


#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

 byte asd[100];
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(4800);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Goodnight moon!");
  mySerial.begin(4800);
}

void loop() { // run over and over

  if (mySerial.available()) {
   mySerial.readBytesUntil("XX", asd, 64);
   for (int i = 0; i <= 63; i++){
      Serial.print(i+1);
      Serial.print(" / ");
      Serial.println(asd[i]);
   }
  }
}

I think the issue is coming somehow from the "readBytesUntil".

The machine I have connected to the serial port sends 3 times the same information (21 bytes x 3 = 63 bytes). For the porpuse of understand it, you just need to know that byte 3 has to be 21. So as it sends it three times, byte 3, byte 24 and byte 45 of "asd" have to be 21(rest can vary):
(this is what I get from the "one" board")

1 / 128

2 / 64

3 / 21

4 / 0

.
.
.
22 / 128

23 / 0

24 / 21
.
.
.
.
42 / 234

43 / 128

44 / 0

45 / 21

46 / 0
.
.
.
63 / 234

So I know the data I get from the "one" board is correct

And this is what I get from the MKR board:

1 / 136

2 / 255

3 / 248

4 / 136

5 / 253

6 / 248

7 / 254

8 / 136

9 / 253

10 / 248

11 / 254

12 / 168

13 / 255

14 / 253

15 / 254

16 / 168

17 / 253

18 / 254

19 / 168

20 / 255

21 / 253

22 / 0
...
(rest all 0)

62 / 0

63 / 0

So MKR with "readBytesUntil" function only gets 21 bytes ....

Byte 3 changes in the MKR board, sometimes I get 248, sometimes I get 252..., so is not a problem of apling a simple formula...

If you have control of the data that is sent I suggest that you take a look at Serial input basics - updated

Thanks for the help, don't really have the capacity to fully understand what is been told there...

I have just realize that I don't get the fully correct data neither in the "one" board.

What is sure is:

  • I should get 21x3 bytes.
  • byte 3, byte 24 and byte 45(and only this) should be 21 (15 in HEX)
  • byte 1, byte 21 and byte 42(and only this) shold be 224 (E0 in HEX)
  • byte 20, byte 41 and byte 63(and only this) should 235 (eb in HEX)

The real interesting data I want lays on the other bytes...

I can see all of this is accomplish using and RS-232 to USB converter in windows, monitoring with Serial port monitor software:

image

Is there anything you advice me to do?

Do you have control of the format of the data being sent ?

Will the bytes of data that you are interested in always be in the same position in the data stream ?

Will the size of the data stream as posted by you always be the same or could it vary ?

How is the end of the 21 x 3 data stream marked, maybe by a Carriage Return, or is the end of each 21 byte packet marked in some way ?

What is sending the data stream ?

Please post the text of a sample 3 x 21 data stream rather than a picture of it

Do you have control of the format of the data being sent ?

No. each time an event is produce the data is sent from the device, I do not have a way to modify the data.

Will the bytes of data that you are interested in always be in the same position in the data stream ?

The data always consists of 63 bytes. This 63 bytes are the same 21 bytes repeat 3 times, as I explained before, some bytes are always repeat inside those 21 bytes(the rest can be different), so I think is fairly easy to recognize the data... if I'm able to get the data right!!!

Will the size of the data stream as posted by you always be the same or could it vary ?

It's always the same, 63 bytes. The event that sends those 63 bytes can vary in time, so I might get one in 30 seconds or two in 5 seconds...

How is the end of the 21 x 3 data stream marked, maybe by a Carriage Return, or is the end of each 21 byte packet marked in some way ?

As far as I understand is with "the third time the "EB" byte appears.

What is sending the data stream ?

Is a machine that counts laps of a toy car. When the car passes through a certain point, the machines sends the data containing the lap time and some more data....

Please post the text of a sample 3 x 21 data stream rather than a picture of it

I don't have a way of putting the text directly (other than writing it down..) i'll post a couple of captures from serial port monitor(software for windows for capturing serial communitcations):


image

In this last image you can see thoose 63 bytes (16 x 3 +15 = 63)

e0 f9 15 02 00 02 00 1b a9 a8 02 00 02 00 00 14
70 26 61 00 eb e0 f9 15 02 00 02 00 1b a9 a8 02  
00 02 00 00 14 70 26 61 00 eb e0 f9 15 02 00 02   
00 1b a9 a8 02 00 02 00 00 14 70 26 61 00 eb

This is the 21 bytes repeat three times:
e0 f9 15 02 00 02 00 1b a9 a8 02 00 02 00 00 14
70 26 61 00 eb e0 f9 15 02 00 02 00 1b a9 a8 02
00 02 00 00 14 70 26 61 00 eb e0 f9 15 02 00 02
00 1b a9 a8 02 00 02 00 00 14 70 26 61 00 eb

That does not sound right. How would you know that you are counting the third instance of "EB" in a particular message and not the first one of another message having read two from the previous one ?

Is there a time gap between receiving each burst of 63 bytes and, if so, us it consistent ?

Your original sketch reads bytes until it gets an 'X' but it is not part of the data

Was that just a fudge to get it to read 63 bytes ?