Is there possible to not using Serial tx rx to do the communication? The serial communication between arduino is kind of trouble due to the maximum transmission size of 1 byte , and i need the value cost at least 2 byte.
you can transmit a 16bt integer in binary as two bytes and a float as four bytes
see post #11 of
Java to Arduino - transmit float as 4 bytes
hk199899:
Is there possible to not using Serial tx rx to do the communication? The serial communication between arduino is kind of trouble due to the maximum transmission size of 1 byte , and i need the value cost at least 2 byte.
You can use lowByte and highByte
void loop()
{
static int number = 0;
Serial.write(highByte(number));
Serial.write(lowByte(number));
number++;
if (number == 1024)
number = 0;
delay(100);
}
Recombine at the other side.
sterretje:
the combination of two byte is like this?void loop()
{
byte higherbyte , lowerbyte;
int number;
higherbyte = Serial.read();
lowerbyte = Serial.read();
number = (higherbyte<<8)+lowerbyte;
}
some sample code
// encode 16 bit int as two bytes and decode
void setup() {
Serial.begin(115200);
Serial.setTimeout(60*60*1000ul);
}
// encode 16 bit unsigned integers data into two bytes (characters)
void intEncode(byte ch[2], const unsigned int data)
{
ch[0]= (data>>8) & 0xff; // top byte of data
ch[1]=data & 0xff; // lower 8 bits of data
}
// decode the above data
void intDecode(const byte ch[10], unsigned int *data)
{
*data=(ch[0] << 8) | ch[1];
}
void loop() {
unsigned int i, x={0},y;
byte ch[2];
Serial.print("enter an unsigned int ? ");
x=Serial.parseInt();
intEncode(ch, x); // encode int as two bytes
Serial.print("\nencoded characters ");
for(i=0;i<2;i++) // print the bytes
{ Serial.print(" " ); Serial.print(ch[i], HEX);}
intDecode(ch, &y); // decode bytes to an int
Serial.print("\ndecoded data ");
Serial.println(y);
}
a run gives
enter an unsigned int ? 67
encoded characters 0 43
decoded data 67
enter an unsigned int ? 7896
encoded characters 1E D8
decoded data 789625678
enter an unsigned int ?
encoded characters 64 4E
decoded data 25678