How To Get Started with ModBus in C# and Visual Studios

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.

New contributor

Jason Hu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật