在.NET中使用RS422传输数据

发布于 2024-08-19 00:09:07 字数 2073 浏览 2 评论 0原文

我正在尝试制作一个简单的应用程序来测试与另一台计算机的 RS422 通信。使用RS232接口该程序可以顺利运行,但使用RS422接口则无法运行,因为有一台计算机无法发送。为了使场景更复杂一些,我可以使用超级终端通过 RS422 进行通信。

这是代码:

public partial class MainForm : Form
{
    private SerialPort m_port;

    public MainForm()
    {
        InitializeComponent();
        m_list.Items.AddRange(SerialPort.GetPortNames());

        m_port = new SerialPort();
        m_port.BaudRate = 9600;
        m_port.DataBits = 8;
        m_port.Parity = Parity.None;
        m_port.StopBits = StopBits.One;
        m_port.Handshake = Handshake.None;
        m_port.Encoding = new ASCIIEncoding();

        m_port.ReceivedBytesThreshold = 1;
        m_port.DataReceived += DataReceivedEvent;
    }

    ~MainForm()
    {
        if (m_port != null)
            m_port.Close();
    }

    private void openClick(object sender, EventArgs e)
    {
        m_port.Close();
        m_port.PortName = (string)m_list.SelectedItem;

        try
        {
            m_port.Open();
            m_buttonSend.Enabled = true;
        }
        catch (UnauthorizedAccessException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void ButtonSendClick(object sender, EventArgs e)
    {
        m_port.WriteLine(m_testBox.Text);
    }

    private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
    {
        Invoke(new EventHandler(DoUpdate));
    }

    private void DoUpdate(object s, EventArgs e)
    {
        m_receivedText.Text += m_port.ReadLine();
    }
}

感谢任何有关此技术的帮助或经验。谢谢!

编辑

与超级终端的 Portmon 的跟踪之间有很多差异和 .NET 组件。其中一行引起了我的注意,因为它涉及到端口中断的等待掩码IOCTL_SERIAL_SET_WAIT_MASK

使用超级终端:

IOCTL_SERIAL_SET_WAIT_MASK  Serial0  SUCCESS  Mask: RLSD ERR

使用 .NET SerialPort 组件

IOCTL_SERIAL_SET_WAIT_MASK  Serial0  SUCCESS  Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING     

有人知道如何更改组件的掩码吗?这事越来越深了……

I'm trying to make a simple application to test the RS422 communications with another computer. Using the RS232 interfaces this program is working smoothly, but with the RS422 is not working, as there is one computer that can't send. To complex the scenario a little bit more, I can communicate through RS422 using a HyperTerminal.

Here is the code:

public partial class MainForm : Form
{
    private SerialPort m_port;

    public MainForm()
    {
        InitializeComponent();
        m_list.Items.AddRange(SerialPort.GetPortNames());

        m_port = new SerialPort();
        m_port.BaudRate = 9600;
        m_port.DataBits = 8;
        m_port.Parity = Parity.None;
        m_port.StopBits = StopBits.One;
        m_port.Handshake = Handshake.None;
        m_port.Encoding = new ASCIIEncoding();

        m_port.ReceivedBytesThreshold = 1;
        m_port.DataReceived += DataReceivedEvent;
    }

    ~MainForm()
    {
        if (m_port != null)
            m_port.Close();
    }

    private void openClick(object sender, EventArgs e)
    {
        m_port.Close();
        m_port.PortName = (string)m_list.SelectedItem;

        try
        {
            m_port.Open();
            m_buttonSend.Enabled = true;
        }
        catch (UnauthorizedAccessException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void ButtonSendClick(object sender, EventArgs e)
    {
        m_port.WriteLine(m_testBox.Text);
    }

    private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
    {
        Invoke(new EventHandler(DoUpdate));
    }

    private void DoUpdate(object s, EventArgs e)
    {
        m_receivedText.Text += m_port.ReadLine();
    }
}

Any help or experience with this technology is appreciated. Thanks!

EDIT

There is a lot of differences between the trace with Portmon of Hyperterminal and the .NET component. There is one of the lines that got my attention as it reefers to the wait mask of the port interruption IOCTL_SERIAL_SET_WAIT_MASK.

With HyperTerminal:

IOCTL_SERIAL_SET_WAIT_MASK  Serial0  SUCCESS  Mask: RLSD ERR

With the .NET SerialPort component

IOCTL_SERIAL_SET_WAIT_MASK  Serial0  SUCCESS  Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING     

Anybody knows how to change the mask from the component? This is getting deep...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-08-26 00:09:07

最后,初始化出现了问题,另外一个问题是阻塞 ReadLine 调用。必须启用 RTS 和 DTS。

这是代码

using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace ComPlay
{
    public partial class MainForm : Form
    {
            private SerialPort m_port;
            private byte [] m_buffer = new byte[10];

            public MainForm()
            {
                    InitializeComponent();
                    m_list.Items.AddRange(SerialPort.GetPortNames());
                    m_list.SelectedIndex = 0;

                    m_port = new SerialPort(SerialPort.GetPortNames()[0],9600,Parity.None,8,StopBits.One);   
                    m_port.Handshake = Handshake.None;
                    m_port.RtsEnable = true;
                    m_port.DtrEnable = true;

                    m_port.DataReceived += DataReceivedEvent;
                    m_port.PinChanged += PinChangedEvent;
            }

            ~MainForm()
            {
                    if (m_port != null)
                            m_port.Close();
            }

            private void openClick(object sender, EventArgs e)
            {
                    if (m_port.IsOpen)
                            m_port.Close();

                    m_port.PortName = (string)m_list.SelectedItem;

                    try
                    {
                            m_port.Open();
                            m_buttonSend.Enabled = true;
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                            MessageBox.Show(ex.Message);
                    }
            }

            private void ButtonSendClick(object sender, EventArgs e)
            {
                    byte [] r_bytes = Encoding.ASCII.GetBytes(m_testBox.Text);
                    m_port.Write(r_bytes,0,r_bytes.Length);
            }

            private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
            {
                    Invoke(new EventHandler(DoUpdate));
            }

            private void DoUpdate(object s, EventArgs e)
            {
                    m_port.Read(m_buffer, 0, m_buffer.Length);
                    m_receivedText.Text += Encoding.ASCII.GetString(m_buffer); 
            }

            private void PinChangedEvent(object sender, SerialPinChangedEventArgs args)
            {

            }
    }
}

要开始传输的重要几点是更改此

ioctl_serial_serial_set_set_handflow serial1成功奶昔:80000000替换:80000040 xonlimit:1024 xofflimit:1024

to此

ioct> ioctl_serial_serial_serial_set_serial_set_sser_set serial1成功摇摇:8000000000000040:800000000040。 XonLimit:1024 XoffLimit:1024

激活 RTS 和 DTR。

Finally there was a problem in the initialitation and another one with the blocking ReadLine call. RTS and DTS must be enabled.

Here is the code

using System;
using System.IO.Ports;
using System.Text;
using System.Windows.Forms;

namespace ComPlay
{
    public partial class MainForm : Form
    {
            private SerialPort m_port;
            private byte [] m_buffer = new byte[10];

            public MainForm()
            {
                    InitializeComponent();
                    m_list.Items.AddRange(SerialPort.GetPortNames());
                    m_list.SelectedIndex = 0;

                    m_port = new SerialPort(SerialPort.GetPortNames()[0],9600,Parity.None,8,StopBits.One);   
                    m_port.Handshake = Handshake.None;
                    m_port.RtsEnable = true;
                    m_port.DtrEnable = true;

                    m_port.DataReceived += DataReceivedEvent;
                    m_port.PinChanged += PinChangedEvent;
            }

            ~MainForm()
            {
                    if (m_port != null)
                            m_port.Close();
            }

            private void openClick(object sender, EventArgs e)
            {
                    if (m_port.IsOpen)
                            m_port.Close();

                    m_port.PortName = (string)m_list.SelectedItem;

                    try
                    {
                            m_port.Open();
                            m_buttonSend.Enabled = true;
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                            MessageBox.Show(ex.Message);
                    }
            }

            private void ButtonSendClick(object sender, EventArgs e)
            {
                    byte [] r_bytes = Encoding.ASCII.GetBytes(m_testBox.Text);
                    m_port.Write(r_bytes,0,r_bytes.Length);
            }

            private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
            {
                    Invoke(new EventHandler(DoUpdate));
            }

            private void DoUpdate(object s, EventArgs e)
            {
                    m_port.Read(m_buffer, 0, m_buffer.Length);
                    m_receivedText.Text += Encoding.ASCII.GetString(m_buffer); 
            }

            private void PinChangedEvent(object sender, SerialPinChangedEventArgs args)
            {

            }
    }
}

The important thing to begin transmitting was to change this

IOCTL_SERIAL_SET_HANDFLOW Serial1 SUCCESS Shake:80000000 Replace:80000040 XonLimit:1024 XoffLimit:1024

to this

IOCTL_SERIAL_SET_HANDFLOW Serial1 SUCCESS Shake:80000001 Replace:80000040 XonLimit:1024 XoffLimit:1024

activating RTS and DTR.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文