[solved] serial communication error

Hello everybody
I'm having some problems communicating through a serial connection.

I have a MEGA connected by USB, and a serial cable connected to TX1/RX1 (18/19) to my computer.
I use "termite" and/or Matlab and/or the arduino serial monitor to send and recieve.

on the arduino the unedited sketch http://arduino.cc/en/Tutorial/MultiSerialMega is loaded.
This is what happens:

For these results i use Matlab to send a 'unit8' 1 (to 100) and i get back a 127 (63..). I show their binary forms to see if i could determine an error pattern (like L/Bendian or something).
Does anyone see a logical pattern here that can lead me to the error?
It is repeatable, the response is the same everytime.

sent 1 recieved 127.
sent 00000001 recieved 01111111.
sent 2 recieved 63.
sent 00000010 recieved 00111111.
sent 3 recieved 126.
sent 00000011 recieved 01111110.
sent 4 recieved 31.
sent 00000100 recieved 00011111.
sent 5 recieved 125.
sent 00000101 recieved 01111101.
sent 6 recieved 62.
sent 00000110 recieved 00111110.
sent 7 recieved 124.
sent 00000111 recieved 01111100.
sent 8 recieved 15.
sent 00001000 recieved 00001111.
sent 9 recieved 123.
sent 00001001 recieved 01111011.
sent 10 recieved 61.
sent 00001010 recieved 00111101.
sent 11 recieved 122.
sent 00001011 recieved 01111010.
sent 12 recieved 30.
sent 00001100 recieved 00011110.
sent 13 recieved 121.
sent 00001101 recieved 01111001.
sent 14 recieved 60.
sent 00001110 recieved 00111100.
sent 15 recieved 120.
sent 00001111 recieved 01111000.
sent 16 recieved 7.
sent 00010000 recieved 00000111.
sent 17 recieved 119.
sent 00010001 recieved 01110111.
sent 18 recieved 59.
sent 00010010 recieved 00111011.
sent 19 recieved 118.
sent 00010011 recieved 01110110.
sent 20 recieved 29.
sent 00010100 recieved 00011101.
sent 21 recieved 117.
sent 00010101 recieved 01110101.
sent 22 recieved 58.
sent 00010110 recieved 00111010.
sent 23 recieved 116.
sent 00010111 recieved 01110100.
sent 24 recieved 14.
sent 00011000 recieved 00001110.
sent 25 recieved 115.
sent 00011001 recieved 01110011.
sent 26 recieved 57.
sent 00011010 recieved 00111001.
sent 27 recieved 114.
sent 00011011 recieved 01110010.
sent 28 recieved 28.
sent 00011100 recieved 00011100.
sent 29 recieved 113.
sent 00011101 recieved 01110001.
sent 30 recieved 56.
sent 00011110 recieved 00111000.
sent 31 recieved 112.
sent 00011111 recieved 01110000.
sent 32 recieved 3.
sent 00100000 recieved 00000011.
sent 33 recieved 111.
sent 00100001 recieved 01101111.
sent 34 recieved 55.
sent 00100010 recieved 00110111.
sent 35 recieved 110.
sent 00100011 recieved 01101110.
sent 36 recieved 27.
sent 00100100 recieved 00011011.
sent 37 recieved 109.
sent 00100101 recieved 01101101.
sent 38 recieved 54.
sent 00100110 recieved 00110110.
sent 39 recieved 108.
sent 00100111 recieved 01101100.
sent 40 recieved 13.
sent 00101000 recieved 00001101.
sent 41 recieved 107.
sent 00101001 recieved 01101011.
sent 42 recieved 53.
sent 00101010 recieved 00110101.
sent 43 recieved 106.
sent 00101011 recieved 01101010.
sent 44 recieved 26.
sent 00101100 recieved 00011010.
sent 45 recieved 105.
sent 00101101 recieved 01101001.
sent 46 recieved 52.
sent 00101110 recieved 00110100.
sent 47 recieved 104.
sent 00101111 recieved 01101000.
sent 48 recieved 6.
sent 00110000 recieved 00000110.
sent 49 recieved 103.
sent 00110001 recieved 01100111.
sent 50 recieved 51.
sent 00110010 recieved 00110011.
sent 51 recieved 102.
sent 00110011 recieved 01100110.
sent 52 recieved 25.
sent 00110100 recieved 00011001.
sent 53 recieved 101.
sent 00110101 recieved 01100101.
sent 54 recieved 50.
sent 00110110 recieved 00110010.
sent 55 recieved 100.
sent 00110111 recieved 01100100.
sent 56 recieved 12.
sent 00111000 recieved 00001100.
sent 57 recieved 99.
sent 00111001 recieved 01100011.
sent 58 recieved 49.
sent 00111010 recieved 00110001.
sent 59 recieved 98.
sent 00111011 recieved 01100010.
sent 60 recieved 24.
sent 00111100 recieved 00011000.
sent 61 recieved 97.
sent 00111101 recieved 01100001.
sent 62 recieved 48.
sent 00111110 recieved 00110000.
sent 63 recieved 96.
sent 00111111 recieved 01100000.
sent 64 recieved 1.
sent 01000000 recieved 00000001.
sent 65 recieved 95.
sent 01000001 recieved 01011111.
sent 66 recieved 47.
sent 01000010 recieved 00101111.
sent 67 recieved 94.
sent 01000011 recieved 01011110.
sent 68 recieved 23.
sent 01000100 recieved 00010111.
sent 69 recieved 93.
sent 01000101 recieved 01011101.
sent 70 recieved 46.
sent 01000110 recieved 00101110.
sent 71 recieved 92.
sent 01000111 recieved 01011100.
sent 72 recieved 11.
sent 01001000 recieved 00001011.
sent 73 recieved 91.
sent 01001001 recieved 01011011.
sent 74 recieved 45.
sent 01001010 recieved 00101101.
sent 75 recieved 90.
sent 01001011 recieved 01011010.
sent 76 recieved 22.
sent 01001100 recieved 00010110.
sent 77 recieved 89.
sent 01001101 recieved 01011001.
sent 78 recieved 44.
sent 01001110 recieved 00101100.
sent 79 recieved 88.
sent 01001111 recieved 01011000.
sent 80 recieved 5.
sent 01010000 recieved 00000101.
sent 81 recieved 87.
sent 01010001 recieved 01010111.
sent 82 recieved 43.
sent 01010010 recieved 00101011.
sent 83 recieved 86.
sent 01010011 recieved 01010110.
sent 84 recieved 21.
sent 01010100 recieved 00010101.
sent 85 recieved 85.
sent 01010101 recieved 01010101.
sent 86 recieved 42.
sent 01010110 recieved 00101010.
sent 87 recieved 84.
sent 01010111 recieved 01010100.
sent 88 recieved 10.
sent 01011000 recieved 00001010.
sent 89 recieved 83.
sent 01011001 recieved 01010011.
sent 90 recieved 41.
sent 01011010 recieved 00101001.
sent 91 recieved 82.
sent 01011011 recieved 01010010.
sent 92 recieved 20.
sent 01011100 recieved 00010100.
sent 93 recieved 81.
sent 01011101 recieved 01010001.
sent 94 recieved 40.
sent 01011110 recieved 00101000.
sent 95 recieved 80.
sent 01011111 recieved 01010000.
sent 96 recieved 2.
sent 01100000 recieved 00000010.
sent 97 recieved 79.
sent 01100001 recieved 01001111.
sent 98 recieved 39.
sent 01100010 recieved 00100111.
sent 99 recieved 78.
sent 01100011 recieved 01001110.
sent 100 recieved 19.
sent 01100100 recieved 00010011.

I've tried many things. If I send and receive on the same arduino USBserialport, i get the correct return.
if i use an UNO to send data to the Mega, i receive correctly.
if i use the arduino serial monitor on 1 pc and a arduino serial monitor on another pc i get the error.
i tried using software serial on the UNO, i get the error.

(i've mentioned this problem before [solved] reading serial information from external source with Uno - Project Guidance - Arduino Forum but the workaround does not work anymore, and i also get wrong values if i connect my (battery powered) sensor to TX1/RX1. I'll try again on the exact laptop that seemed to work with the workaround if i get the chance)

thanks

For these results i use Matlab to send a 'unit8' 1 (to 100)

Is that anything like a uint8_t?

If you are sending a byte (a uint8_t), I'd expect you to read a byte on the Arduino side, and to write a byte on the Arduino side.

I'd expect you to read a byte on the Arduino side, and to write a byte on the Arduino side

it does, but they do not appear to be the same.

it does, but they do not appear to be the same.

It does?

  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);

Despite the stupid name, inByte is not a byte. It is an int.

you're right the example does read integers. But intergers are what i send.
And does it matter? As long as the same format is read and sent the same 8 ones and zeros should be transmitted back, no?

And even commands like Serial1.println("Hello world") are recieved garbeled. But i find it much more difficult to check values and possibly see the errors when working with strings in a terminal. That is why a set up the matlab 1to100 test.

should i use:

byte inByte = Serial1.read();

instead?

you're right the example does read integers. But intergers are what i send.

Really? Earlier, you said you were sending unit8s.

And does it matter? As long as the same format is read and sent the same 8 ones and zeros should be transmitted back, no?

That's what I'm trying to get you to test. Serial.write() writes some number of bits. How many? I don't know. I'd expect it to send 8 when the argument is a byte. I'd expect it to send 16 when the argument is an int. But, what it really does, I don't feel like looking up.

It would have taken you far less time to change 2 words in the sketch and test than to argue about it.

And even commands like Serial1.println("Hello world") are recieved garbeled.

Then, it is just possible that it is your matlab code that is wrong.

according to the Arduino core libs the return value of read() is an int.....

int HardwareSerial::read(void) // from stream - must be able to return error values.
and yes write() expects a byte... // from Print

in short MultSerial.ino looks OK (except for the name of the variable, I agree 100%)

So time to post complete Matlab code

Not arguing, not arguing. I don't know enough for that.
Just went home after first post, leaving the project at work, so I can't try till tomorrow.

Unit8 was a typo. Should be uint8.

The serial.println does not require the matlab code however. The garbeled output can be read by Termite or arduino serial monitor.

Thanks for thinking with me so far. To be continued tomorrow.

according to the Arduino core libs the return value of read() is an int.....

But, it does that only so that it can return something when there is nothing to read. If no one was fool enough to call Serial.read() without calling Serial.available() first, Serial.read() could return a uint8_t.

and yes write() expects a byte... // from Print

It isn't clear what happens when it is passed a larger value. Is the high order byte sent? The low order byte? Both bytes?

That's why I recommended a test getting the value from Serial.read() into a byte. The high order byte is discarded, and the low order byte retained. Then, Serial.write() is passed a known quantity, and behaves in a known fashion.

Unit8 was a typo. Should be uint8.

No. It should be uint8_t. u is for unsigned. int is obvious. 8 is the number of bits. _t means that it is a standard type.

The serial.println does not require the matlab code however. The garbeled output can be read by Termite or arduino serial monitor.

But, the Serial Monitor can not send byte (binary) data. It can only send character data (ASCII).

Keep us posted, as you learn more. I agree with rob that seeing the matlab code would be useful.

and a serial cable connected to TX1/RX1 (18/19) to my computer

Does the serial cable output TTL instead of rs232 so it is compatible with the mega?

Matlab:
this is the Matlab code used to get the in/out list in my first post:
Com24 being a usb to serial connected to my computer, connected to 18/19 on the Mega
Com50 being the arduino usb connection.

SOin=serial('Com24');
set(SOin,'BaudRate',9600);
fopen(SOin);

SOout=serial('Com50');
set(SOout,'BaudRate',9600);
fopen(SOout);

for t1=1:100
 fwrite (SOin,t1,'uint8');
 pause(0.1);
 A=fread(SOout,1,'uint8');
 disp(['sent ' num2str(t1) ' recieved ' num2str(A) '.']);
 disp(['sent ' dec2bin(t1,8) ' recieved ' dec2bin(A,8) '.'])
end



fclose(SOin);
fclose(SOout);

println:
if i change the line in the MultiserialMega example:
Serial1.write(inByte);
to
Serial1.println("Hello world");

and send anything to Serial, the response is:

«:!¿Q![1B]'7åë[00]!¿Q![1B]'7åë

(the forum changed it some, but you get the point)

byte:
if i change
int inByte = Serial1.read();
to
byte inByte = Serial1.read();
(both instances)
There is no difference in the in/out response


Does the serial cable output TTL instead of rs232 so it is compatible with the mega?

I'm sorry i do not know exactly what that means. Is there a way to check this?

I have no problem communicating computer to computer, or computer to GPS for instance when using these cables.
I have a comport on the motherboard of my computer. Should i use that one? (I've read somewhere that it may have 12V signal and could damage the arduino)

Does the serial cable output TTL instead of rs232 so it is compatible with the mega?

Ahhh, I probably need one of these:
http://www.elechouse.com/elechouse/index.php?main_page=product_info&products_id=650

Ahhh, I probably definitely need one of these:

I fixed that for you.

Yep,that did it.

Both with software serial on the UNO and with MultiSerialMega on the MEGA.