简体   繁体   English

如何在C#中的COM PORT中发送ASCII?

[英]How to send ASCII in COM PORT in C#?

I want to send ASCII command to serial Port ? 我要发送ASCII命令到串口吗? Below is ASCII command 下面是ASCII命令

VER < CR>< LF>(There is no space between command) VER <CR> <LF>(命令之间没有空格)

char[] _mass = new char[]{'V','E','R','CR','LF'};

_serialPort.Write(_mass, 0, _mass.Length);

How TO Create command and send it. 如何创建命令并发送。

Characters in C# are UTF-16 encoded by default. 默认情况下,C#中的字符是UTF-16编码的。 When you want to communicate over the serial port, you usually use ASCII. 当您想通过串行端口进行通信时,通常使用ASCII。

So you need to tell C# which encoding you want and then have it convert it to a byte array. 因此,您需要告诉C#您想要哪种编码,然后将其转换为字节数组。 Byte arrays are just bits without an implicit encoding. 字节数组只是没有隐式编码的位。

byte[] _mass = Encoding.GetEncoding("ASCII").GetBytes("VER\r\n");
_serialPort.Write(_mass, 0, _mass.Length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM