SerialPort encoding - how do I get 8 bit ASCII?

前端 未结 3 1814
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 16:34

I\'m having a problem with a program that communicates over a serial port. One of the characters it must send and receive is the degree symbol, ASCII 0xBF. It\'s been working fi

相关标签:
3条回答
  • 2021-01-23 17:16

    The degree symbol in both 1252 and 28591 appears to be xB0, not xBF.

    0 讨论(0)
  • 2021-01-23 17:26

    System.Text.ASCIIEncoding isn't the right encoding to use for 8-bit communication. Instead use e.g. the "ISO 8859-1 Latin 1; Western European (ISO)" code page:

    port.Encoding = Encoding.GetEncoding(28591);
    
    0 讨论(0)
  • 2021-01-23 17:29

    Your problem is that ASCIIEncoding is a 7 bit encoding. You're looking for something that supports Extended ASCII.

    The following will get you Codepage 1252.

    System.Text.Encoding.GetEncoding(1252);
    

    This will get you ISO 8859-1.

    System.Text.Encoding.GetEncoding(28591);
    

    Both of these are considered Extended ASCII and both contain symbols (mostly with the same byte representation) typically associated with this set, such as © , ¥ , and º . See the referenced links for more information about which characters are included in each encoding.

    0 讨论(0)
提交回复
热议问题