I want to get started using modbusClient in C# and visual studios to read the registers from a Seeed Studio TPH sensor that I linked below. I downloaded the manufacturers recommended application QModBus and followed the examples I found in the data sheet to set the COM port to COM 3, baud rate to 9600, data bits to 8, stop bits to 1, and parity to none.
Then I read from slave ID 1, function code 0x03 to read holding registers, start address 0 and number of coils 1 and I get a response of the temperature. This is all done in QModBus application.
Now I want to translate that to my C# code and display the temperature on a windows form. So far, I can connect to the COM port (COM3) and I think I am setting the initial parameters correctly.
I am not really sure how to set up the ModBus Request section and actually read the data from the register.
I think, in the method that I have called “buttonSendTest_Click” I want to set the Request data and read from the register, but I am unsure how exactly to set that up.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class WeatherStation2 : Form
{
LPModBus modbusClient;
private bool rloadConnected;
public WeatherStation2()
{
InitializeComponent();
modbusClient = new LPModBus();
modbusClient.SetBaudRate(9600);
modbusClient.SetCoilData(1);
modbusClient.SetParity(System.IO.Ports.Parity.None);
modbusClient.SetStopBits(System.IO.Ports.StopBits.One);
modbusClient.SetUnitId(1);
rloadConnected = false;
}
// Connect to the COM port
private void buttonConnectCOM_Click(object sender, EventArgs e)
{
if (!rloadConnected)
{
string prt = Helpers.SafeGetComboSelectedText(comboBoxCOMport);
if (prt != "")
{
try
{
if (modbusClient.OpenModbusPort(prt))
{
Helpers.SafeSetLabeltext(labelStatus, "Connected");
rloadConnected = true;
Helpers.SafeSetButtontext(buttonConnectCOM, "Disconnect");
}
}
catch // (Exception ex)
{
rloadConnected = false;
Helpers.SafeSetButtontext(buttonConnectCOM, "Connect");
Helpers.SafeSetLabeltext(labelStatus, "Open Port Error");
}
}
else
{
Helpers.SafeSetLabeltext(labelStatus, "Select comm port to continue");
rloadConnected = false;
Helpers.SafeSetButtontext(buttonConnectCOM, "Connect");
}
}
else
{
Helpers.SafeSetButtontext(buttonConnectCOM, "Connect");
modbusClient.ClosePort();
rloadConnected = false;
Helpers.SafeSetLabeltext(labelStatus, "Disconnected");
}
}
//Here is where I want to read from the register and display it in a message box
private void buttonSendTest_Click(object sender, EventArgs e)
{
modbusClient.SendReadHoldingRegisters();
MessageBox.Show(modbusClient.ReadPortBytes(null, null).ToString());
}
// This method populates the COM ports combo box with all the detected COM ports.
private void comboBoxCOMport_Click(object sender, EventArgs e)
{
string[] PortNames = System.IO.Ports.SerialPort.GetPortNames();
Helpers.SafetSetComboboxItmsClear(comboBoxCOMport);
Helpers.SafeSetComboboxItems(comboBoxCOMport, PortNames);
}
}
}
https://www.seeedstudio.com/RS485-Air-Temperature-Humidity-and-Pressure-Sensor-p-5801.html[
QModBus Application Screenshot](https://i.sstatic.net/3aLqFLlD.png)
Ive tried writing the following code and reading from the holding register, but all I get is “System.Byte[]”. I am not sure what functions I need to call to read the register or whether I need to implement a time.sleep(), or any other useful tricks for the ModBus protocol.
Jason Hu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.