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.

asked Apr 29, 2021 at 18:36
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 Commented Apr 29, 2021 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. Commented Apr 29, 2021 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. Commented Apr 29, 2021 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. Commented Apr 29, 2021 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. Commented Apr 29, 2021 at 19:39

1 Answer 1

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

answered Apr 29, 2021 at 18:48
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. Commented 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. Commented 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. Commented Apr 29, 2021 at 19:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.