âchar inChar = (char)Serial.read();â why is the âcharâ in brackets? and what is it called
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
New contributor
add a comment |Â
up vote
2
down vote
favorite
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
New contributor
1
This is actually more a C/C++ language question. The hint is "Please check the return value type forSerial.read()
".
â Mikael Patel
3 hours ago
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
3 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
New contributor
I am sure this is a very simple answer, but I don't know to look it up on google
In the example "SerialEvent" in the arduino IDE (v1.8.7), there is the following function
void serialEvent()
while (Serial.available())
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == 'n')
stringComplete = true;
On the third line "char inChar = (char)Serial.read();" the (char) is given in brackets before the serial read.
Why is this? and more specifically how is called ?
serial
serial
New contributor
New contributor
New contributor
asked 4 hours ago
will.mendil
132
132
New contributor
New contributor
1
This is actually more a C/C++ language question. The hint is "Please check the return value type forSerial.read()
".
â Mikael Patel
3 hours ago
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
3 hours ago
add a comment |Â
1
This is actually more a C/C++ language question. The hint is "Please check the return value type forSerial.read()
".
â Mikael Patel
3 hours ago
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
3 hours ago
1
1
This is actually more a C/C++ language question. The hint is "Please check the return value type for
Serial.read()
".â Mikael Patel
3 hours ago
This is actually more a C/C++ language question. The hint is "Please check the return value type for
Serial.read()
".â Mikael Patel
3 hours ago
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
3 hours ago
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
3 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read()
is int
, because it will return -1
, when you try to read without any bytes being available. The casting with (char)
will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float
value to an int
value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available()
, because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
1
Thank you ever so much !
â will.mendil
3 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read()
is int
, because it will return -1
, when you try to read without any bytes being available. The casting with (char)
will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float
value to an int
value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available()
, because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
1
Thank you ever so much !
â will.mendil
3 hours ago
add a comment |Â
up vote
2
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read()
is int
, because it will return -1
, when you try to read without any bytes being available. The casting with (char)
will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float
value to an int
value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available()
, because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
1
Thank you ever so much !
â will.mendil
3 hours ago
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
It is called type casting and will convert one type to another. The return type of Serial.read()
is int
, because it will return -1
, when you try to read without any bytes being available. The casting with (char)
will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float
value to an int
value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available()
, because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
It is called type casting and will convert one type to another. The return type of Serial.read()
is int
, because it will return -1
, when you try to read without any bytes being available. The casting with (char)
will drop the high byte of the (2-byte long) integer, leaving only the lower byte. This byte will contain the actual read data, in which you are interested here.
Casting is a complex thing, because there can be different ways to convert different types (for example maintaining the bit-content vs. maintaining the decimal value). The casting, that you are seeing here, should only be done, if you really know, what you are doing. The bits will not be changed to maintain the correct value, but the bit-content will be preserved, unless the new type is smaller than the former. In this case the rest is lost. Also, if you try to cast a float
value to an int
value with this, you will get a totally different decimal value out of it, because the numbers are encoded in the bits in a totally different way. Here it is known to be OK, especially, if you are checking for available bytes before reading with Serial.available()
, because the library puts the ASCII data in the lower byte.
For further explanations you should consult C/C++ guides.
answered 3 hours ago
chrisl
2,0512211
2,0512211
1
Thank you ever so much !
â will.mendil
3 hours ago
add a comment |Â
1
Thank you ever so much !
â will.mendil
3 hours ago
1
1
Thank you ever so much !
â will.mendil
3 hours ago
Thank you ever so much !
â will.mendil
3 hours ago
add a comment |Â
will.mendil is a new contributor. Be nice, and check out our Code of Conduct.
will.mendil is a new contributor. Be nice, and check out our Code of Conduct.
will.mendil is a new contributor. Be nice, and check out our Code of Conduct.
will.mendil is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f56359%2fchar-inchar-charserial-read-why-is-the-char-in-brackets-and-what-is%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
This is actually more a C/C++ language question. The hint is "Please check the return value type for
Serial.read()
".â Mikael Patel
3 hours ago
So it basically forces the value returned from serial.read() to be a char? Do you know how this is named, so I can look up the documentation?
â will.mendil
3 hours ago