0
votes

I'm trying to send data to an Arduino via a serial communication. I'm using a buffer to store bytes I'd like to send and then use the fwrite command to write them to serial.

Sender (Matlab)

disp(bufferSend);
fwrite(xbee,bufferSend,'uint8');
disp('sent:');    
xbee.ValuesSent

the display command shows that 255 bytes hav2 been sent:

 2     1     0     0     0     1     1    13    17     0    30     0    12    14     0     0     0     1    0   
[...] 0     0    0     [...]   0

sent: 

255

Receiver (Arduino)

the received values are correct till the 7-th byte. Only 70 bytes are received;

The Arduino serial monitor prints one byte at the time with the following code:

 byte getData;
 [...]
 while(xbee.available()>0)
 {  
   getData = xbee.read();  


   Serial.print("Received: ");
   Serial.print(getData);
   Serial.print("         Bin:  ");
   Serial.print(getData,BIN);
   Serial.print("         Hex:  ");
   Serial.print(getData,HEX);
   Serial.print("         cont:  ");
   Serial.print(cont);
   Serial.println();
   [...]
 }

Here is the output:

Matlab communication established. 

Received: 2         Bin:  10         Hex:  2         cont:  2

Received: 1         Bin:  1         Hex:  1         cont:  3

Received: 0         Bin:  0         Hex:  0         cont:  4

Received: 0         Bin:  0         Hex:  0         cont:  5

Received: 0         Bin:  0         Hex:  0         cont:  6

Received: 1         Bin:  1         Hex:  1         cont:  7

Received: 1         Bin:  1         Hex:  1         cont:  8

Received: 141         Bin:  10001101         Hex:  8D         cont:  9

Received: 145         Bin:  10010001         Hex:  91         cont:  10

Received: 128         Bin:  10000000         Hex:  80         cont:  11

Received: 143         Bin:  10001111         Hex:  8F         cont:  12

Received: 128         Bin:  10000000         Hex:  80         cont:  13

Received: 134         Bin:  10000110         Hex:  86         cont:  14

Received: 135         Bin:  10000111         Hex:  87         cont:  15

Received: 128         Bin:  10000000         Hex:  80         cont:  16

Received: 128         Bin:  10000000         Hex:  80         cont:  17

Received: 128         Bin:  10000000         Hex:  80         cont:  18

Received: 128         Bin:  10000000         Hex:  80         cont:  19

Received: 128         Bin:  10000000         Hex:  80         cont:  20

Received: 128         Bin:  10000000         Hex:  80         cont:  21

Received: 128         Bin:  10000000         Hex:  80         cont:  22

Received: 128         Bin:  10000000         Hex:  80         cont:  23

Received: 128         Bin:  10000000         Hex:  80         cont:  24

Received: 128         Bin:  10000000         Hex:  80         cont:  25

Received: 128         Bin:  10000000         Hex:  80         cont:  26

Received: 128         Bin:  10000000         Hex:  80         cont:  27

Received: 128         Bin:  10000000         Hex:  80         cont:  28

Received: 128         Bin:  10000000         Hex:  80         cont:  29

Received: 128         Bin:  10000000         Hex:  80         cont:  30

Received: 128         Bin:  10000000         Hex:  80         cont:  31

Received: 128         Bin:  10000000         Hex:  80         cont:  32

Received: 128         Bin:  10000000         Hex:  80         cont:  33

Received: 128         Bin:  10000000         Hex:  80         cont:  34

Received: 64         Bin:  1000000         Hex:  40         cont:  35

Received: 64         Bin:  1000000         Hex:  40         cont:  36

Received: 64         Bin:  1000000         Hex:  40         cont:  37

Received: 64         Bin:  1000000         Hex:  40         cont:  38

Received: 64         Bin:  1000000         Hex:  40         cont:  39

Received: 64         Bin:  1000000         Hex:  40         cont:  40

Received: 64         Bin:  1000000         Hex:  40         cont:  41

Received: 64         Bin:  1000000         Hex:  40         cont:  42

Received: 64         Bin:  1000000         Hex:  40         cont:  43

Received: 64         Bin:  1000000         Hex:  40         cont:  44

Received: 64         Bin:  1000000         Hex:  40         cont:  45

Received: 64         Bin:  1000000         Hex:  40         cont:  46

Received: 64         Bin:  1000000         Hex:  40         cont:  47

Received: 64         Bin:  1000000         Hex:  40         cont:  48

Received: 64         Bin:  1000000         Hex:  40         cont:  49

Received: 64         Bin:  1000000         Hex:  40         cont:  50

Received: 64         Bin:  1000000         Hex:  40         cont:  51

Received: 64         Bin:  1000000         Hex:  40         cont:  52

Received: 64         Bin:  1000000         Hex:  40         cont:  53

Received: 64         Bin:  1000000         Hex:  40         cont:  54

Received: 64         Bin:  1000000         Hex:  40         cont:  55

Received: 64         Bin:  1000000         Hex:  40         cont:  56

Received: 64         Bin:  1000000         Hex:  40         cont:  57

Received: 64         Bin:  1000000         Hex:  40         cont:  58

Received: 64         Bin:  1000000         Hex:  40         cont:  59

Received: 32         Bin:  100000         Hex:  20         cont:  60

Received: 32         Bin:  100000         Hex:  20         cont:  61

Received: 32         Bin:  100000         Hex:  20         cont:  62

Received: 32         Bin:  100000         Hex:  20         cont:  63

Received: 32         Bin:  100000         Hex:  20         cont:  64

Received: 0         Bin:  0         Hex:  0         cont:  65

Received: 0         Bin:  0         Hex:  0         cont:  66

Received: 0         Bin:  0         Hex:  0         cont:  67

Received: 0         Bin:  0         Hex:  0         cont:  68

Received: 0         Bin:  0         Hex:  0         cont:  69

Received: 0         Bin:  0         Hex:  0         cont:  70

Received: 0         Bin:  0         Hex:  0         cont:  71



[...]

If the eigth value (i.e 13) is placed in the first positions on the buffer, Arduino receives it correctly.

Is it related to the fwrite function and buffer dimensions? Is it a case of buffer overflow?

Solution

Slow down the Baudrate! In my case 19200 worked

2

2 Answers

1
votes

This looks like a hardware issue. It appears that the bits of the the values are getting corrupted.

141 is 13 with the first bit flipped

145 is 17 with the first bit flipped

128 is 0 with the first bit flipped

143 is 30 shifted right, and the first bit flipped

You haven't said how the matlab is connected to xbee. Could the problem be incompatible voltage levels or baudrate? A problem like this would show symptoms of the first bytes being okay, and then later the bytes go bad as the communication gets out of sync. If this were the problem, then the errors would appear with some randomness. Are the received values always the same for a given set of bytes?

The communication between the xbee modules is probably okay, as there should be a checksum involved to make sure the packets are not corrupted.

That leaves the serial communication between matlab and the first xbee, and the serial communication between the shield and the arduino.

You can also try a Serial.readBytes() call to see if that makes a difference.

1
votes

What you are experiencing is the simple case of too much to do, and not enough time to do it. This is the reason why the best serial I/O routines are run my an interrupt, which basically forces all program level processing to stop when a byte comes in to the receiving shift register (often a UART), so that it can be saved in a buffer before the next byte comes in. In the absence of an interrupt routine, you can not write code to process each byte as it comes in, because as you see, you run the risk of failing to get back to the primary task of saving bytes quickly enough. You may not think you are "processing" each byte, but print() functions have a tremendous amount of overhead.

I would suggest re writing your receive routine to do nothing but add bytes to a buffer, keeping a count of how many bytes were received, and keeping tabs on how much time has passed with NO DATA received. Here you'll have to make a decision how long is long enough to consider the incoming message ended. THEN... when the message is completely stored, you can go back and print out your analysis of what you revived. You will almost surely be able operate at closer to the maximum baud rate by following this approach! ;-)