串行C ++/CLI字节传输问题 [英] Serial Port C++/CLI problems on bytes transmition

查看:62
本文介绍了串行C ++/CLI字节传输问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有问题.
我已经将串行通信设置为4800bps,8位1停止位非奇偶校验(已经在C ++/Cli程序和外部电路中设置了此参数).好吧,我正在尝试将字节数组(25个字节)发送到我的外部电路(这是接收器,已设置为接收25个字节以执行特定操作),当我使用以下串行端口代码部分时( C ++/CLI)通过测试Windows超级终端上的通信(我期望的结果),我在电路(接收器)中得到的结果与我得到的结果截然不同.

I am having a problem with my program.
I had set my Serial communication to a 4800bps, 8 bits 1 stop bit non parity (already set this parameter in the C++/Cli program and in the external circuit). Well I am trying to send an array of bytes (25 bytes) to my external Circuit (wich is the receptor .This has been set for receiving 25 bytes to perform an specific action), well when I used the following serial port code part (C++/CLI) I obtained very different results in my circuit(receptor) from the results I had by testing the communication on the HyperTerminal (this results are the ones that I expected) from Windows.

private: System::Void button6_Click_1(System::Object^ sender, System::EventArgs^ e)
{
   array<Byte>^ TxBfr={0xFF,0xFF,0xFF,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0xFF,0x00,0xFF,0x03};
    serialPort1->Write(TxBrf,0,25);
    
};



该代码确实发送信息(字节数),但似乎已对其进行了修改,因为我的外部电路执行的操作与其应执行的操作不同.

我是否需要对数组进行任何转换?可能是问题所在吗?

感谢您阅读任何反馈?

Bryan



This code DOES send the information (arry of bytes) but seems like its been modified, because my external circuit performs a different action to the one its supposed to do.

Do I have to do any convertion to my array? Might that be the problem?

Thanks for reading any feedback?

Bryan

推荐答案

文档中 [ ^ ]方法是有关该函数使用的编码的说明.
In the documentation of the Write[^] method is a remark about the encoding used by the function.
默认情况下,SerialPort使用ASCIIEncoding对字符进行编码. ASCIIEncoding将大于127的所有字符编码为(char)63或?".要支持该范围内的其他字符,请将Encoding设置为UTF8Encoding,UTF32Encoding或UnicodeEncoding.
By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater then 127 as (char)63 or ''?''. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.


因此,编码使您的字节变得混乱,如前所述,大于127的任何字节都将被char(63)''?''替换.

尝试使用其他编码.


So the encoding is messing up your bytes, as stated any byte larger than 127 is replaced by char(63) ''?''.

Try using a different encoding.


这篇关于串行C ++/CLI字节传输问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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