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...
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
#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
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):
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 ?