Search code examples
c#serial-portnon-ascii-characters

SerialPort in C# doesn't send correctly Extended ASCII char


I'm using SerialPort in C# and I discovered that I'm not able to send extended ASCII Characters.

If I send a char lower than 128 it's transmitted correcly, otherwise I transmit always 0x3F

comport.Write(((char)127).ToString());  ///I receive 0x7F 
comport.Write(((char)128).ToString());  ///I receive 0x3F

Is there a particular setting that allows me to send a char from 0x00 to 0xFF?


Solution

  • From docs:

    By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater than 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.

    So you should just change Encoding property of comport object.