使用C#,nmodbus4,实现了读取单个从站的数据,使用2个timer读取2个从站时,程序卡死。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用C#,nmodbus4,实现了读取单个从站的数据,使用2个timer读取2个从站时,程序卡死。相关的知识,希望对你有一定的参考价值。
如何使用nmodbus4轮询读取多个从站数据?
public partial class Form1 : Form
public Form1()
InitializeComponent();
SerialPort port = new SerialPort("COM5");
byte slaveId = 6;
ushort startAddress = 0;
ushort numRegisters = 6;
byte slaveId1 = 3;
ushort startAddress1 = 0;
ushort numRegisters1 = 1;
bool done1 = true;
bool done2 = true;
private void Form1_Load_1(object sender, EventArgs e)
// configure serial port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
private void timer1_Tick(object sender, EventArgs e)
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
bool[] a = master.ReadInputs(slaveId, startAddress, numRegisters);
if (a[0] == false)
textBox2.BackColor = Color.Red;
else textBox2.BackColor = Color.Black;
for (int i = 0; i < numRegisters; i++)
textBox1.Text = textBox1.Text + " " + a[i].ToString();
private void timer2_Tick(object sender, EventArgs e)
IModbusSerialMaster master1 = ModbusSerialMaster.CreateRtu(port);
ushort[] registers = master1.ReadInputRegisters(slaveId1, startAddress1, numRegisters1);//04指令
if ( registers[0] == 1)
textBox4.BackColor = Color.Red;
else textBox4.BackColor = Color.Black;
for (int i = 0; i < numRegisters1; i++)
textBox3.Text = registers[i].ToString("X04");
以上是关于使用C#,nmodbus4,实现了读取单个从站的数据,使用2个timer读取2个从站时,程序卡死。的主要内容,如果未能解决你的问题,请参考以下文章
基于XMC4800 Ethercat从站的工厂自动化解决方案