Serial communication with variable larger than one byte

I want to transfer a series of numbers to another microprocessor, and every number is larger than 1 byte, like 2040... The command Serial.write can only send one byte. How do I send a larger number? Thanks.

Numbers larger than a byte are just a collection of severals bytes. Send them one byte at a time.

dhunt: Numbers larger than a byte are just a collection of severals bytes. Send them one byte at a time.

Thank you, but I'm very new to programming. Could you state a little further in detail?

2040

      111 1111 1000 (11 bits)
0000 0111 1111 1000 (16 bits)
word w = 2040;   // a word is 16 bits
byte lowbyte = 0;
byte highbyte = 0;
word temp1 = 0;

temp1 = (w & 255);  // 248
lowbyte = temp1;

temp1 = (w & 65280);  // 1792
temp1 = (temp1 / 256);  // 7
highbyte = temp1;

// highbyte = 7   lowbyte = 248
// 0000 0111       1111 1000
// 0000 0111 1111 1000

There's probably a way to do that more efficiently, but I think it demonstrates the process, however tediously.

qiuqiuaaa: I want to transfer a series of numbers to another microprocessor, and every number is larger than 1 byte, like 2040... The command Serial.write can only send one byte. How do I send a larger number? Thanks.

You could do a Serial.print which sends the number as an ASCII string. Then the receiving end can do Serial.read(), a digit at a time, until it assembles the number that was sent. So say you want to send 5678, you really send '5', '6', '7', '8'. The receiving end gets 5, put it somewhere, then when it gets 6 it multiplies the previous number by 10 and adds in the 6 (so now we have 56). Then when the 7 comes we do that again (now we have 567). And finally for the 8, so we end up with 5678.

Thanks for all your replies. I know how to do it now.

qiuqiuaaa: I want to transfer a series of numbers to another microprocessor, and every number is larger than 1 byte, like 2040... The command Serial.write can only send one byte. How do I send a larger number? Thanks.

Serial.write can send any number of bytes, not just one byte, by doing a buffered write. so if your buffer is byte buffer[256];

Serial.write((uint8_t*)buffer,256);

qiuqiuaaa: Thanks for all your replies. I know how to do it now.

I think i'ts better for you to share your work here, so you could help someone who has same problem with you,, like me.. :D

Another way to skin the cat is to use Bill Porter's excellent EasyTransfer library, if your aim is to transfer data between arduinos. It takes away all the work, you just define what is sent, and done.

Constantin: Another way to skin the cat is to use Bill Porter's excellent EasyTransfer library, if your aim is to transfer data between arduinos. It takes away all the work, you just define what is sent, and done.

thank you! it's really help me a lot! :D