Hi,
I want to read 3 Modbus registers from a level measuring device.
What I do now is to list all the available com ports and then after selecting the correct one I start reading and displaying the 3 registers by using BasicLabel controls.
It works but this is not how I want it to work.
Is it possible to establish Modbus communication and then start reading Modbus data?
So I use the following code:
private void MainForm_Load(object sender, EventArgs e)
{
//show list of valid com ports
foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
}
private void button1_Click(object sender, EventArgs e)
{
modbusRTUCom1.PortName = comboBox1.SelectedItem.ToString();
}
private void modbusRTUCom1_ConnectionEstablished(object sender, EventArgs e)
{
button1.Text = "Connected";
Timer timerReadPLC = new Timer();
timerReadPLC.Interval = 500;
timerReadPLC.Tick += new EventHandler(timerReadPLC_Tick);
timerReadPLC.Start();
}
void timerReadPLC_Tick(object sender, EventArgs e)
{
litersMod.Text = modbusRTUCom1.Read("30001").ToString();
kilosMod.Text = modbusRTUCom1.Read("30003").ToString();
int x = Int32.Parse(modbusRTUCom1.Read("30002")) / 10;
levelMod.Text = x.ToString();
}
Is it a proper way to make a program?
Also I don't really undestand what modbusRTUCom1.Subscribe and modbusRTUCom1.BeginRead methods do.