在.NET中使用RS422传输数据
我正在尝试制作一个简单的应用程序来测试与另一台计算机的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,初始化出现了问题,另外一个问题是阻塞
ReadLine
调用。必须启用 RTS 和 DTS。这是代码
要开始传输的重要几点是更改此
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
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.