简体   繁体   English

如何在.NET中实现RS485 2线双向通信?

[英]How can I implement RS485 2 wires bidirectional communication in .NET?

I'm trying to get bidirectional communication using RS485 2 wires and everything I've tried so far failed. 我正在尝试使用RS485 2线进行双向通信,到目前为止我尝试过的所有操作都失败了。

I can send data and have my peripheral react as expected (so the wiring is correct), but I never receive any reply. 我可以发送数据并使外围设备按预期进行反应(因此接线正确),但是我从未收到任何回复。


I'm using the .NET SerialPort , I've tried using the DataReceived event and also a loop in a Thread polling the port. 我正在使用.NET SerialPort ,我尝试使用DataReceived事件以及Thread轮询端口的循环。 I've even tried just blocking on read until enough data is received. 我什至尝试仅阻止读取,直到接收到足够的数据。


I have tried several hardware options: 我尝试了几种硬件选项:

  • PCI RS232 Card with a Sena RS232-RS485 converter 带Sena RS232-RS485转换器的PCI RS232卡
  • PCI RS232 Card with a Moxa RS232-RS485 converter 带Moxa RS232-RS485转换器的PCI RS232卡
  • PCI RS485 Card PCI RS485卡

I have played with driver settings: - FIFO Interrupt Trigger levels - Receiver FIFO Flow Control Thresholds - RS485 Buffer enable (Normal, Active High, Active Low) 我玩过驱动程序设置:-FIFO中断触发级别-接收器FIFO流控制阈值-RS485缓冲区启用(正常,高电平有效,低电平有效)


Following various leads (like Can't receive serial data in .net 2.0, using rs232 to rs485 converter ), I've tried setting DtrEnable to true, or false, or switching it. 遵循各种线索(例如, 无法使用rs232到rs485转换器在.net 2.0中接收串行数据 ),我尝试将DtrEnable设置为true或false,或对其进行切换。

I've also tried switching RtsEnable when sending and receiving (following http://en.wikipedia.org/wiki/RS-232#RTS.2FCTS_handshaking ). 我还尝试过在发送和接收时切换RtsEnable (遵循http://en.wikipedia.org/wiki/RS-232#RTS.2FCTS_handshaking )。


I don't see anything else to try right now without resorting to different wiring. 我现在看不到其他方法可以尝试不采用其他接线方式。 What could be wrong? 有什么事吗


As requested, some code (it's only a snapshot after many attempts): 根据要求,提供一些代码(经过多次尝试仅是快照):

Open: 打开:

_serialPort = new SerialPort(comboBoxSerialPort.Text, 9600, Parity.None, 8, StopBits.One)
{
    WriteTimeout = 500,
    ReadTimeout = 500,
    Handshake = Handshake.None
};

_serialPort.Open();
_serialPort.DtrEnable = true;
_serialPort.RtsEnable = true;

Send: 发送:

_serialPort.RtsEnable = false;
_serialPort.Write(data, 0, data.Length);
_serialPort.RtsEnable = true;

Thread.Sleep(1);
_dataSent.Set();

Reader thread: 读者线程:

var port = form1._serialPort;
byte[] buffer = new byte[128];
int read = 0;
do
{
    Array.Clear(buffer, 0, buffer.Length);
    read = 0;

    try
    {
        form1._dataSent.WaitOne();

        //if (port.BytesToRead > 0)
        read = port.Read(buffer, 0, buffer.Length);
    }
    catch (TimeoutException)
    {
    }
    catch (Exception ex)
    {
        form1.Invoke(form1.AddErrorMethod, ex.ToString());
        continue;
    }

    if (read > 0)
    {
        form1.Invoke(form1.AddOutputMethod, ByteListToString(buffer));
    }

    Thread.Sleep(20);
}
while (_continue);

Note: data packets are 10 bytes long, in both directions. 注意:双向的数据包长度为10个字节。

I've fixed the issue on one hardware configuration (the RS485 card) by: 我已通过以下方法解决了一种硬件配置(RS485卡)的问题:

  • fixing the wiring: although it's called '2 wires', 4 pins/wires from the card need to be used, joined, 2 by tranport wire. 固定布线:尽管它被称为“ 2根电线”,但需要使用卡上的4根针脚/电线,通过传输线将其2根连接在一起。
  • using DtrEnable to indicate sending/receiving 使用DtrEnable指示发送/接收
  • waiting before enabling receive mode after a send. 在发送后启用接收模式之前等待。

My send code now looks like this: 我的发送代码现在如下所示:

// Enable send mode
SerialPort.DtrEnable = false;
SerialPort.Write(data, 0, data.Length);

// Wait a little, then enable response mode.
Thread.Sleep(15);
SerialPort.DtrEnable = true;

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

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