0

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.

7
  • the serial port is just a pipe ... what you stuff into it is up to you ... normally, the serial port carries 8 bit data (bytes) ... what that data means to you is not known to the serial port ... if you see two bytes then you are probably transmitting UNICODE, not ASCII ... anyway, it has nothing to do with the serial port – jsotola Apr 29 at 18:45
  • @jsotola silly me; I tried to simplify the question by using the IDE. I have edited the question to reflect that it is also not working when I send bytes directly, e.g., using the C# SerialPort class. – Zane Claes Apr 29 at 19:13
  • Your edited question is now a question about C#, not about Arduino. You have to ask “How do I send 0x93 through a serial port in C#?” in a more appropriate venue. – Edgar Bonet Apr 29 at 19:19
  • @EdgarBonet if you have an agnostic way to easily send/receive bytes directly from the serial port, I'm all ears. I'm unfamiliar with any shell-ish way to do so, and thus was using these proxies. – Zane Claes Apr 29 at 19:32
  • I do not know C#, and I do not know what you mean by “agnostic”. If by “shell-ish” you mean “from a Unix shell”, the following works in bash: echo -ne '\x93' > /dev/ttyACM0. But then, again, this is not about Arduino, it's about the shell/environment/programming language of your choice. – Edgar Bonet Apr 29 at 19:39
2

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”.

3
  • 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 Apr 29 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 Apr 29 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 Apr 29 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.