How to send an integer value through serial port? (Arduino to Pc)

Hey Guys,

I am having trouble and can not send values greater than 255 via serial port. Can you help me please.

I appreciate your help Thanks

Please post your code.

Thanks for the reply,

here is my code

 int bearing = ((highByte<<8)+lowByte)/10; 
   Serial.write(bearing);

the maximum value that it send is 255, not more than 255 can be sent

Send highByte and lowByte separately, and construct bearing on the receiving side.

oh okay, thank you for the reply

Hi, I tried to send and receive high bytes and low bytes, on the receiving side I am using C#

I don't know what went wrong, It doesn't give the desired results,

Could you please provide a example code.

Thanks

Try sending your bytes to serial monitor as hex just to see what you are sending.

if ( highbyte < 0x10 ) Serial.print( “0” ); // leading 0, if needed to keep columns straight
Serial.print( highbyte, HEX ); // this shows high first then low, yours may not
Serial.print( " - " );
if ( lowbyte < 0x10 ) Serial.print( “0” ); // leading 0, if needed to keep columns straight
Serial.println( lowbyte, HEX ); // I show them paired, it makes reading easier

Yah sure, it is for debugging.

Serial.write will only send one byte, so max is 255.

Use serial.print to send the ASCII string of your number.

Ints take 2 bytes as low and high. ASCII can run 1 to 5 bytes plus delimiter so never less. Sometimes code doesn't have all day to transfer info.

I would suggest sending a CRC byte at the end of every data packet though. Serial needs to be checked.

Thank you very much for all replies,

Sending one integer number, I didn't how hard it could be :(

"I would suggest sending a CRC byte at the end of every data packet though. Serial needs to be checked."

What is CRC byte and how do you do it? can you please show me a simple example, I am totaly new with Arduino

Thank you

Ints take 2 bytes as low and high. ASCII can run 1 to 5 bytes plus delimiter

How many bytes does -32000 take?

Got me there Paul. More than 5 characters plus delimiter.

Farhad, one simple form of CRC (Circular Redundancy Check) is to start with a byte (unsigned 8 bits) = 0xFF and subtract each byte from the data in turn from that byte, which always returns 1 byte using unsigned math. So subtract the 1st byte and from the result subtract the 2nd and from that result subtract the 3rd, etc, until the end of data. At that point you have 1 byte CRC to match with the CRC check byte given at the end of the message. If they don't match, there was an error in serial transmit/receive or the code if it hasn't been debugged.

You can collect the data in a buffer and run it through a loop to do this or you can subtract each byte (and count and do other processing) as it comes in, the buffer is the serial line and your loop is Arduino loop(). Either way takes a bit of thought and from having done both ways I find that buffering and then using string commands to parse and lex is actually more work but hey the people who are only familiar with string processing will tell you different.

Try here for CRC header. The 16 bit one should be the best compromise.

http://nongnu.org/avr-libc/user-manual/group__util__crc.html

Remember to put “#include <util/crc16.h>” at the top of your sketch.

Look at EasyTransfer - it's a library that sends any data across serial, with CRC. It's simple to implement, and in the basic serial mode, has good example programs.

Farhad: I am having trouble and can not send values greater than 255 via serial port. Can you help me please.

Why not send the integer value as a series of characters representing a decimal integer number? Pretty common.