0% found this document useful (0 votes)
6 views4 pages

modbus

The document is a C# program for a Modbus multi-client application using Windows Forms. It establishes a serial connection to a Modbus device, allowing users to connect and disconnect, and periodically polls data from the device. The program includes methods for creating Modbus requests, sending and receiving data, and calculating CRC for error checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

modbus

The document is a C# program for a Modbus multi-client application using Windows Forms. It establishes a serial connection to a Modbus device, allowing users to connect and disconnect, and periodically polls data from the device. The program includes methods for creating Modbus requests, sending and receiving data, and calculating CRC for error checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.IO.Ports;
using System.Windows.Forms;

namespace ModbusMultiClient
{
public partial class Modbus : Form
{
private SerialPort serialPort = new SerialPort();
private Timer timersPoll = new Timer();

public Modbus()
{
InitializeComponent();
timersPoll.Interval = 1000;
timersPoll.Tick += TimersPoll_Tick;
}

private void BtnConnect_Click(object sender, EventArgs e)


{
if (BtnConnect.Text == "Connect")
{
try
{
serialPort.PortName = txtPort.Text;
serialPort.BaudRate = int.Parse(txtBad.Text);
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DataBits = 8;
serialPort.Open();

timersPoll.Start();
lblStatus.Text = "Connected";
BtnConnect.Text = "Disconnect";
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
else
{
timersPoll.Stop();
serialPort.Close();
lblStatus.Text = "Disconnected";
BtnConnect.Text = "Connect";
}
}

private void TimersPoll_Tick(object sender, EventArgs e)


{
if (serialPort.IsOpen)
{
byte slaveId = 1;
byte[] request = CreateModbusRequest(slaveId, 0, 3);
byte[] response = SendReceiveModbus(request);
if (response != null)
{
tReg1_1.Text = response[3].ToString();
tReg1_2.Text = response[5].ToString();
tReg1_3.Text = response[7].ToString();
}
}
}
private byte[] CreateModbusRequest(byte slaveId, ushort startAddress,
ushort numRegisters)
{
byte[] request = new byte[8];
request[0] = slaveId;
request[1] = 0x03; // Function code for reading holding registers
request[2] = (byte)(startAddress >> 8);
request[3] = (byte)(startAddress & 0xFF);
request[4] = (byte)(numRegisters >> 8);
request[5] = (byte)(numRegisters & 0xFF);
ushort crc = CalculateCRC(request, 6);
request[6] = (byte)(crc & 0xFF);
request[7] = (byte)(crc >> 8);
return request;
}

private byte[] SendReceiveModbus(byte[] request)


{
try
{
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
serialPort.Write(request, 0, request.Length);
System.Threading.Thread.Sleep(100);
int bytesToRead = serialPort.BytesToRead;
byte[] response = new byte[bytesToRead];
serialPort.Read(response, 0, bytesToRead);
return response;
}
catch (Exception ex)
{
MessageBox.Show("Communication error: " + ex.Message);
return null;
}
}

private ushort CalculateCRC(byte[] data, int length)


{
ushort crc = 0xFFFF;
for (int i = 0; i < length; i++)
{
crc ^= data[i];
for (int j = 0; j < 8; j++)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
{
crc >>= 1;
}
}
}
return crc;
}
}
}

You might also like