SerialPort 编码 - 我如何获得 8 位 ASCII? [英] SerialPort encoding - how do I get 8 bit ASCII?

查看:24
本文介绍了SerialPort 编码 - 我如何获得 8 位 ASCII?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用通过串行端口进行通信的程序时遇到问题.它必须发送和接收的字符之一是度数符号,ASCII 0xBF.多年来一直运行良好,现在突然串行端口对象开始丢弃第 7 位,所以我得到的是 0x3F 而不是 0xBF.

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.

我确信这是我所做的一些愚蠢的事情,因为我最近在该领域修改了我的代码 - 但是我看不出我所做的会导致第 8 位丢失.

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

任何想法如何将端口恢复为 8 位操作?

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

推荐答案

你的问题是 ASCIIEncoding 是 7 位编码.您正在寻找支持扩展ASCII的东西.

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

以下内容将为您提供代码页1252.

System.Text.Encoding.GetEncoding(1252);

这将使您获得 ISO 8859-1.

System.Text.Encoding.GetEncoding(28591);

这两个都被认为是 扩展 ASCII 并且都包含通常与此相关的符号(大多数具有相同的字节表示)设置,例如 ©, ¥, 和 º.有关每种编码中包含哪些字符的详细信息,请参阅参考链接.

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.

这篇关于SerialPort 编码 - 我如何获得 8 位 ASCII?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆