My project requires that Serial.read()
be able to accept extended ASCII characters (0x80
<=> 0xFF
). For example, I would expect reading the character \
to yield the decimal value 157
. Instead, the read()
command has two successful reads, with the values: 194
and 165
. Of course, I could create a lookup table and convert, but this is not a viable option. I am conforming to the Grbl
standard protocol, which uses these single-byte extended ASCII characters as interrupts. Such a two-byte structure precludes a simple peek/read approach necessitated by the protocol.
Edit I tried to simplify the question for this context, but failed to account for the fact that the Arduino IDE will send the input as Unicode. Regardless, it still does not work when sending bytes directly. Namely, if I send the byte 0x93
via C# as Serial.Write("\u0093")
, the Arduino's Serial.read()
does not see any bytes available. Sending normal ASCII, e.g., Serial.Write("\u0018")
, works fine.
1 Answer 1
Yes, of course, you can read any byte sequence. As you have experienced, if you send the byte values 194 and 165, your Arduino reliably reads 194 and 165.
Now, you have to be aware that there are tons of incompatible character encodings that could all be called "extended ASCII". I do not know which one you have in mind. Maybe there is such an encoding where "\" has the code point 157. Nowadays, however, almost any modern computer system uses Unicode, and serializes the characters using UTF-8. The character "\" is serialized in UTF-8 as the pair of bytes 194 and 165.
If you want to use the Grbl protocol, send interrupts as bytes, do not think about them as "characters".
-
Ack, of course I forgot to account for the IDE using unicode. Please see the edited question. Also, please note that I defined "extended ASCII" in the OP.Zane Claes– Zane Claes2021年04月29日 19:11:12 +00:00Commented Apr 29, 2021 at 19:11
-
@ZaneClaes: If by "extended ASCII" you mean the byte range 0x80–0xff, that's not what most people call "extended ASCII". In general, these words are taken to mean any of a myriad character encodings (generally 8-bit encodings) that are ASCII-compatible, and thus "extend" ASCII. If you "expect reading the character ¥ to yield the decimal value 157", then you are thinking of one such encoding.Edgar Bonet– Edgar Bonet2021年04月29日 19:26:54 +00:00Commented Apr 29, 2021 at 19:26
-
the fact that the phrase can have multiple interpretations is precisely why I clarified the exact value range I was talking about ;) The Grbl docs comments call these interrupts "extended ascii," so I was merely copying their convention.Zane Claes– Zane Claes2021年04月29日 19:39:52 +00:00Commented Apr 29, 2021 at 19:39
0x93
through a serial port in C#?" in a more appropriate venue.echo -ne '\x93' > /dev/ttyACM0
. But then, again, this is not about Arduino, it's about the shell/environment/programming language of your choice.