//step1. reference nmodbuspc.dll, and using the namespaces.
usingModbus.Device;//for modbus master
usingSystem.IO.Ports;//for serial port
usingModbusRTU_Master;
usingModbusDemo.windows;
usingSystem.IO;
usingSystem.Threading;
usingSystem.Runtime.InteropServices;
usingSystem.Diagnostics;
/*
//Modbus exception codes definition
* Code * Name * Meaning
01 ILLEGAL FUNCTION The function code received in the query is not an allowable action for the server.
02 ILLEGAL DATA ADDRESS The data addrdss received in the query is not an allowable address for the server.
03 ILLEGAL DATA VALUE A value contained in the query data field is not an allowable value for the server.
04 SLAVE DEVICE FAILURE An unrecoverable error occurred while the server attempting to perform the requested action.
05 ACKNOWLEDGE This response is returned to prevent a timeout error from occurring in the client (or master)
when the server (or slave) needs a long duration of time to process accepted request.
06 SLAVE DEVICE BUSY The server (or slave) is engaged in processing a long–duration program command , and the
client (or master) should retransmit the message later when the server (or slave) is free.
08 MEMORY PARITY ERROR The server (or slave) attempted to read record file, but detected a parity error in the memory.
0A GATEWAY PATH UNAVAILABLE The gateway is misconfigured or overloaded.
0B GATEWAY TARGET DEVICE FAILED TO RESPOND No response was obtained from the target device. Usually means that the device is not present on the network.
*/
namespaceModbusDemo
{
publicpartialclassmodbus_Master:UserControl
{
SerialPortserialPort=newSerialPort();//create a new SerialPort object with default settings.