Serial.Write(0x0);
What does the argument ie (0x0) symbolize?
The comment states that this statement makes the Arduino send a null byte to the PC.
Context of use:
Serial data communication requires the Arduino to send a "break field" before the "sync field" is sent to the slave node. This is the reason that the programmer used Serial.Write(0x0) ie to create a break field.
marellasunny:
Serial.Write(0x0);
What does the argument ie (0x0) symbolize?
The comment states that this statement makes the Arduino send a null byte to the PC.
Context of use:
Serial data communication requires the Arduino to send a "break field" before the "sync field" is sent to the slave node. This is the reason that the programmer used Serial.Write(0x0) ie to create a break field.
The "break field" and "sync field" may be part of some serial protocols but required? Bull.
0x is how you tell the compiler that you are specifying a number written as hexadecimal, base 16 instead of base 10 where base 10 is the default ordinary numbers.
0b is how to tell that the number is binary.
I forget (and won't bother to look up) the letter for pretty much useless octal but there is one.
If you want to know more, look up the word 'radix'.
Is there any "simple" reason why I would be sending a Hexadecimal number using Serial.Write(0x0)? Is it always done this way in Serial communication?
As a comparison,I know that Serial.print() sends a ASCII value but would this also be a plausible statement for 'sending' Serial data?
Stupid question:You mean,its sending a integer ie Just a single number and not a whole byte?
In other words,its just sending a decimal equivalent of the number 0xAA, for example, and its not sending this as a whole byte of 8 bits
marellasunny:
Is there any "simple" reason why I would be sending a Hexadecimal number using Serial.Write(0x0)? Is it always done this way in Serial communication?
As a comparison,I know that Serial.print() sends a ASCII value but would this also be a plausible statement for 'sending' Serial data?
Without seeing the code in which it is used it is impossible to give a specific answer as to why it is necessary to send a byte with the value 0.
As others have said there are various ways for describing the byte value 0 - 0x0 is just one of them.
And an integer is two bytes long. The shortest thing that can be sent is a single byte.
void loop() // run over and over
{
// debug();
//return;
int i=0;
int a;//a is the identifier, b,c are used to calculate the parity
//Serial.flush();//wait for sent buffer to empty
//delay(2);
for(i=1;i<=1;i++)
{
for(int xzy=0;xzy<=100;xzy++){
Serial.end();
Serial.begin(9600);
[b] Serial.write(0x0);
[/b] delay(1);
Serial.end();//close serial
Serial.begin(19200);
//sync field
[b] Serial.write(0x55);
[/b] delay(1);
//calculate the identifier
int by[6];
for (int j = 0; j < 6; j++)
[b] by[j] = (i >> j) & 0x01; //don't quite understand how this works in hex. I know bitshift and the & 00000001 preserve the bit digit we want[/b]
//calculate the identifier
int p0 = (by[0] ^ by[1] ^ by[2] ^ by[4]);
int p1 = !(by[1] ^ by[3] ^ by[4] ^ by[5]);
a = (p1 << 7) + (p0 << 6) + i;
Serial.write(a);// protected identifier(6bit data and 2 bit parity)
delay(8);//waiting for the slave to response
/*if(Serial.available()) {
int inByte = Serial.read();
Serial1.print(inByte, BYTE);
} */
}
}
}
OK, you have posted the code. But I (foolishly?) assumed you would also explain what it is for - what is it communicating with - stuff like that.
There is no magic about Hex nunbers. It's just another way of writing numbers. We normally use decimal numbers. If you don't understand something using Hex notation convert the number to decimal or binary on a piece of paper. I have a convenient book from years ago which conveniently lists all the numbers from 0 - 255 in Decimal, Hex, Octal, Binary and Ascii characters.
I understand now that they are all equivalent but have different symbols,hence 0x or 0b etc.
But,I don't understand how one could add a integer to a hexadecimal number like part of the above code,reposted here:
int i=1;
int by[6];
for (int j = 0; j < 6; j++)
by[j] = (i >> j) & 0x01;//I presume one could use the bit shift operator only for binary representation. So,this statement here seems to be saying I am adding a binary shifted number to a hexadecimal 1. Does that mean the computer fills the first 7 places with 0s'and add-up?
All the number representations are for us humans. The computer only works in binary. As far as the computer is concerned, there is no difference in any of the representations, it is all 1's and 0's as far as it is concerned.
marellasunny:
Stupid question:You mean,its sending a integer ie Just a single number and not a whole byte?
In other words,its just sending a decimal equivalent of the number 0xAA, for example, and its not sending this as a whole byte of 8 bits
The serial write function sends bytes, but that can be changed. If you feed it a 16-bit int, it sends the low 8 bits.
The reason to code hex or binary or single-quote characters is to improve readability of the code.
Shift and other bit logic operations are nice fast ways to work with bits. Learn and practice the operations and what they are for should become apparent. Don't and they will be mysterious things to ask questions about.