Wrong data being send

Hello everybody.

I have a problem with my Arduino DUE. I’m recieving data from an FPGA (that come from a sensor), and I want to send those data to my computer by Serial communcation. However, by using Serial.write (I can’t use print since it’s too slow), i’m not recieiving binary data (which are the data sent by the FPGA) but the correspind extended ASCII code, which I don’t want. How can I recieve binary data by using Serial.write ?

Here is my code :

int i = 0;
byte h;
byte l;
byte hrec;
byte lrec;
int Baud = 1000000;

byte buf[4096];


void setup() {
 i = 0;
 Serial.begin(115200);
 Serial2.begin(Baud);
 Serial.println("<Arduino is ready>");
}

void loop() {

  if (Serial2.available()>0) {
  buf[i] = Serial2.read();
  buf[i+1] = Serial2.read();
  buf[i+2]  = Serial2.read();
  buf[i+3]  = Serial2.read();
  i = i+4;}

  if (i == 4096){
  Serial.write(buf, 4096);
  i = 0;
  }
}

And I have already read https://forum.arduino.cc/index.php?topic=396450.0 but since he isn’t using the write function, I can’t use it.

Thx !

  if (Serial2.available()>0) {

You are looking for 1 or more bytes and then reading 4 bytes.

That should be:

  if (Serial2.available()>3) {

This may be a problem

int Baud = 1000000;

the maximum value for an int is 32767.

You should be using

unsigned long Baud = 1000000;

...R

Hey guys, thx for you reply and sorry for my late reply !

Robin2:
This may be a problem

int Baud = 1000000;

the maximum value for an int is 32767.

You should be using

unsigned long Baud = 1000000;

...R

I have tried this, but no success, still recieving character.

johnwasser:

  if (Serial2.available()>0) {

You are looking for 1 or more bytes and then reading 4 bytes.

That should be:

  if (Serial2.available()>3) {

I have tested with only 1 data being sent, still no success.

Hi,

could you have a program running on the PC, send the data as a 'character' and translate it when it arrives there?

Peter

Peterd51:
Hi,

could you have a program running on the PC, send the data as a 'character' and translate it when it arrives there?

Peter

Hi,

I have thought about this possibility, but wanted to avoid it, since following the seria.write() documentation, it is supposed to send bytes only, and thus make things easier. The problem with translating the char is that i'm going beyond the scope of the standard ASCII table, since i have 8 bits signed data. I can manage value from 0 to 127, but how about -128 to -1 ?

Robin2:
This may be a problem

int Baud = 1000000;

the maximum value for an int is 32767.

You should be using

unsigned long Baud = 1000000;

…R

It’s a Due - 32 bit ints.

It is binary data! The serial monitor can only handle ascii and hence interprets it as ascii.

Demo code

uint8_t data[256];
void setup()
{
  Serial.begin(57600);
  while (!Serial);

  for (uint16_t cnt = 0; cnt < sizeof(data); cnt++)
  {
    data[cnt] = cnt;
  }

  Serial.write(data, sizeof(data));

}

void loop() {
  // put your main code here, to run repeatedly:

}

The output in realterm, configured to show hex values of the received data

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 
18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 
48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 
60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 
78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 
90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 
A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF 
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7 
D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF 
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF

Hi,

(I can't use print since it's too slow)

How fast do you want all this to happen?

Tom... :slight_smile:

sterretje:
It is binary data! The serial monitor can only handle ascii and hence interprets it as ascii.

Demo code

uint8_t data[256];

void setup()
{
  Serial.begin(57600);
  while (!Serial);

for (uint16_t cnt = 0; cnt < sizeof(data); cnt++)
  {
    data[cnt] = cnt;
  }

Serial.write(data, sizeof(data));

}

void loop() {
  // put your main code here, to run repeatedly:

}




The output in realterm, configured to show hex values of the received data


00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17
18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47
48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F
60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77
78 79 7A 7B 7C 7D 7E 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F
90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7
A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3 D4 D5 D6 D7
D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF

Ok then do you know how can I get it without interpreting it as a char on my PC ?

TomGeorge:
Hi,
How fast do you want all this to happen?

Tom… :slight_smile:

I need to read data sent at 2 M baud, and write them to my computer knowing that data are sent every 50 microseconds.

Rash45:
Ok then do you know how can I get it without interpreting it as a char on my PC ?

What are you going to do with the data on a PC? Anyway, don't treat the data as char. Write an application or maybe use realterm (can save to file).

sterretje:
What are you going to do with the data on a PC? Anyway, don't treat the data as char. Write an application or maybe use realterm (can save to file).

Ok thanks, I got something that I can read now ! I changed the type from char to byte, and then to string.
However, I'm still getting a problem : At the begining of the communication, data are sent normally, then for a certain amount of time, lots of data will be lost, and then again data will be sent normally. This occurs evey time I launch the communication. Got any ideas why ?
I noticed that the Arduino buffer was overfilled when data begin to "dissapear", but for no apparent reason.

I don't know much about the Due but posting your updated code might help some of us to help you.

What is the Arduino buffer? If it's in the Due, try to send to the PC at a higher baudrate than at what you are receiving.

What are you currently using to view the data on the PC? Serial Monitor?

And I ask again, what are you going to do with the data on the PC?

don't forget that the serial buffer in an Arduino board is very small. so you can only hold a somewhat small amount of data received before your sketch reads it. this can cause missed data. if this was your this problem It would become better with slower buads...not faster.

Hi,
What is the application that requires the high sample rate?
What quantity are you monitoring and collecting data?

Tom... :slight_smile: