如何将大于 127 的 ASCII 值发送到串口 [英] How to send ASCII values greater than 127 to serial port

查看:98
本文介绍了如何将大于 127 的 ASCII 值发送到串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我将任何大于 127 的 ASCII 值发送到 com 端口时,我都会在串行端口上得到垃圾输出值.

Whenever i send any ASCII value greater than 127 to the com port i get junk output values at the serial port.

 ComPort.Write(data);

推荐答案

严格来说仅ASCII包含 128 个可能的符号.
您需要使用不同的字符集来发送 33 个控制字符和 94 个 ASCII 字母和符号以外的任何字符.

Strictly speaking ASCII only contains 128 possible symbols.
You will need to use a different character set to send anything other than the 33 control characters and the 94 letters and symbols that are ASCII.

为了让事情变得更加混乱,ASCII 被用作几个较大(冲突)字符集的起点.此列表并不全面,但最常见的是:

To make things more confusing, ASCII is used as a starting point for several larger (conflicting) character sets. This list is not comprehensive, but the most most common are:

  1. Extended_ASCII,它是包含 128 个字符的 ASCII.
  2. ISO 8859-1 是 ASCII,包含代表所有西方语言所需的所有字符欧洲语言.
  3. Windows-1252 是 ISO 8859-1,有一些改动(包括可打印的字符)在 0x80-0x9F 范围内).
  4. ISO 8859-2,相当于东欧语言.
  5. Windows-1250 也适用于东欧语言,但与 8859 几乎没有相似之处-2.
  6. UTF-8 也是从 ASCII 派生而来,但具有从 128 到255.
  1. Extended_ASCII which is ASCII with 128 more characters in it.
  2. ISO 8859-1 is ASCII with all characters required to represent all Western European languages.
  3. Windows-1252 is ISO 8859-1 with a few alterations (including printable characters in the 0x80-0x9F range).
  4. ISO 8859-2 which is the equivalent for Eastern European languages.
  5. Windows-1250 is also for Eastern european languages, but bears little resemblance to 8859-2.
  6. UTF-8 is also derived from ASCII, but has different characters from 128 to 255.

回到您的问题:您的编码设置为 ASCII,因此您无法发送该字符集之外的任何字符.您需要为您发送的数据将编码设置为适当的字符集.根据我的经验(公认在美国和西欧)Windows-1252 是最常用的.

Back to your problem: your encoding is set to ASCII, so you are prevented from sending any characters outside of that character set. You need to set your encoding to an appropriate character set for the data you are sending. In my experience (which is admittedly in USA and Western Europe) Windows-1252 is the most used.

在 C# 中,您可以通过串行端口发送字符串或字节.如果你发送一个字符串,它使用 SerialPort.Encoding 将该字符串转换为要发送的字节.您可以将其设置为适合您的目的的适当编码,或者您可以手动转换带有 System.Text.Encoding 对象.

In C#, you can send either a string or bytes through a Serial Port. If you send a string, it uses the SerialPort.Encoding to convert that string into bytes to send. You can set this to the appropriate encoding for your purposes, or you can manually convert convert a string with a System.Text.Encoding object.

在com端口上设置编码器:

Set the encoder on the com port:

ComPort.Encoding = Encoding.GetEncoding("Windows-1252");

或手动编码字符串:

System.Text.Encoding enc = System.Text.Encoding.GetEncoding("Windows-1252");
byte[] sendBuffer = enc.GetBytes(command);
ComPort.write(sendBytes, 0, sendBuffer.Length);

两者在功能上都做同样的事情.

both do functionally the same thing.

0x96 是 Windows-1252 中的有效字符这会输出一个长连字符.(正常的连字符是 0x2D)

0x96 is a valid character in Windows-1252 This outputs a long hyphen. (normal hyphen is 0x2D)

System.Text.Encoding enc = System.Text.Encoding.GetEncoding("windows-1252");
byte[] buffer = new byte[]{0x96};
Console.WriteLine(enc.GetString(buffer));

这篇关于如何将大于 127 的 ASCII 值发送到串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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