1

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 fine for years now suddenly the serial port object has started dropping bit 7, so I get 0x3F instead of 0xBF.

I'm sure that this is something silly I've done because I've tinkered with my code in that area recenlty - however I cannot see what I've done that causes loss of the 8th bit.

My port gets initialized like this:

    BaudRate              9600  int  
    DataBits              8 int  
    DiscardNull true    bool
    DtrEnable             true  bool
    Encoding    {System.Text.ASCIIEncoding} System.Text.Encoding                  Handshake           None  System.IO.Ports.Handshake
    NewLine           "\n"  string
    Parity            None  System.IO.Ports.Parity
    ParityReplace   63  byte
    PortName              "COM4"    string
    ReadBufferSize  128 int
    ReadTimeout 250 int
    ReceivedBytesThreshold  1   int
    RtsEnable             true  bool
    StopBits              One   System.IO.Ports.StopBits
    WriteBufferSize 64  int
    WriteTimeout    1500    int

Any ideas how I restore the port to 8-bit operation?

Tim Long
  • 13,508
  • 19
  • 79
  • 147

3 Answers3

12

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.

Waylon Flinn
  • 19,969
  • 15
  • 70
  • 72
  • http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx for 8 bit encoding – StefanE Aug 24 '11 at 16:13
  • 2
    StefanE: UTF-8 is most certainly what the OP does *not* want. – Gabe Aug 24 '11 at 16:37
  • Yeah, UTF-8 results in wide characters, the device I'm talking to has no concept of unicode, it is pure ASCII, or rather, ANSI (ISO 8859 8-bit ASCII). – Tim Long Aug 26 '11 at 00:06
  • I think I will need to go with CP-1252. Curiously, the character code is 0xDF, which prints as ß - it's supposed to be a degree symbol, so goodness knows what ancient encoding existed where that was actually true. – Tim Long Aug 26 '11 at 00:14
  • @TimLong Did this get you what you needed? – Waylon Flinn Oct 10 '11 at 21:44
  • The only character sets I could find that translate that byte to that symbol are Japanese Shift JIS (Win-932) and the practically-identical classic Japanese Macintosh encoding. – Nyerguds Apr 12 '17 at 07:41
3

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);
  • Thanks. I have found that Encoding.Default also works, but I guess that is fragile and depends on the system locale. – Tim Long Aug 26 '11 at 00:05
0

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

dbasnett
  • 11,334
  • 2
  • 25
  • 33