我正在尝试使用realterm与硬件流量控制设置为“RS 485-rts”来通信控制板。如何在Windows串口实现它? [英] I am trying to communicate a control board using realterm with hardware flow control set as 'RS 485-rts'. How can I have it implemented in windows serial port ?

查看:548
本文介绍了我正在尝试使用realterm与硬件流量控制设置为“RS 485-rts”来通信控制板。如何在Windows串口实现它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码,



 port.Parity = Parity.None; 
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Encoding = System.Text.Encoding.ASCII;
port.Name =COM1;
port.BaudRate = 9600;
port.DataReceived + = Port_DataReceived;





在向Board发送命令时,

 port.RtsEnable = true; 
port.Write(System.Text.Encoding.ASCII.GetBytes(dataToSend),0,dataToSend.Length);
Thread.Sleep(15);
port.RtsEnable = false;





我尝试过:



我尝试过使用RealTerm,而且我一直得到正确的响应,我也从上面的代码得到了正确的答案,但有些时候它的返回垃圾不是从RealTerm发送时的情况。

解决方案

它被称为:防御性编程



Quote:

不保证每收到一个字节都会引发DataReceived事件。使用BytesToRead属性确定要在缓冲区中读取的数据量。


I'm using below codes,

port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Encoding = System.Text.Encoding.ASCII;
port.Name ="COM1";
port.BaudRate=9600;
port.DataReceived += Port_DataReceived;



And while sending commands to Board,

port.RtsEnable = true;
port.Write(System.Text.Encoding.ASCII.GetBytes(dataToSend), 0, dataToSend.Length);
Thread.Sleep(15);
port.RtsEnable = false;



What I have tried:

I have tried using RealTerm, and I'm getting correct response all the time, I'm also getting correct response from above codes but some of the time its returning garbage which is not a case while sending from RealTerm.

解决方案

It's called: "defensive programming"

Quote:

The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.


这篇关于我正在尝试使用realterm与硬件流量控制设置为“RS 485-rts”来通信控制板。如何在Windows串口实现它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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