I had written some codes to change a GPS u-blox 7 NEO settings! But I would like firstly to check whether I can create the vital sentence at serial port. So I wrote some codes. But there are some unexpected outputs in serial port.
check the codes please:
void loop() {
if (Serial.available()) {
int inByte = Serial.read();
char payload[]= { 0xF0 , 0x03, 0 ,0 ,0 , 0, 0, 1};
if (inByte==49){ //49 = ascii for 1! When I send 1 to the arduino by serial port
for (int i=0; i<8; i++){Serial.println(payload[i], HEX);}
}
}
}
as you can see by this code I would like the arduino print the payload variable in the serial port with HEX format but look at the output of the serial port
FFFFFFF0
3
0
0
0
0
0
1
the 6 Fs in the beginning of the output are extra! I do not know how they come and from where!
Oh yes, the problem is caused by 0xF0 constant conversion to unsigned long size of first argument of function println - it is fullfilled by FF. In case of int, the value is 0x00F0 and also different overloaded function println(int,int) is used.
Budvar10:
Oh yes, the problem is caused by 0xF0 constant conversion to unsigned long size of first argument of function println - it is fullfilled by FF. In case of int, the value is 0x00F0 and also different overloaded function println(int,int) is used.
actually I did not understand your point about char and conversions. and why FFFFF?
The function Serial.println(payload*, HEX) is translated by different way in dependance on type of variables in argument.* If you look at Print.h and Print.cpp in Arduino core, there is several definitions of println. If payload is char - println(long,int) is used and 0xF0 constant is aligned to long value 0xFFFFFFF0 and if payload is int (it means the fist value is 0x00F0) and println(int,int) is used instead.
It is deeper in lib code, to simplyfy: number is printed without leading zeros. Printig works digit by digit using division and modulo of number until zero of multiply division is obtained. See printNumber function in Print.cpp.
The problem is not in negative or positive value but in variable type. For type char if you are calling println function there
is not defined function println(char,int) exactly, then compiler takes println(long,int).
Try this:
It seems as the same function call twice, but no. Fist is called version println(long, int) and second is called version println(unsigned char,int). Then, each negative value of char will be printed with many Fs in first call.
The solution should be also to add definition for println(char,int) into Print class.
Budvar10:
is not defined function println(char,int) exactly, then compiler takes println(long,int).
I understand that. However the "problem" arises from the way the compiler translates a char with a value above 127 into a long. It does not create a long with the same signed value as the char has.
I agree that a special println(char, int) could be written to deal with the situation.
However (and this shows my wish to have as little as possible to do with C/C++) it seems weird that the compiler actually has such a thing as a "signed" char. When would one ever use that?
I think signed chars came along later. I think it is because the C committee wanted things to be symmetric. It is the same reason they added the unary '+' along with the unary '-'.