Pardon_me
Pardon_me

Reputation: 735

How to use NModbus and C# with PLC to to turn outputs on and off

Hi all I'm using the NModbus library, and was previously running on VS2012 and then VS2010, but tried running it on VS2008 and it seems to be running better.

Here is my output when I run the code in the dos prompt

Modbus.IO.ModbusSerialTransport Write -     TX: 1,16,7,208,0,3,6,0,1,0,5,0,15,149,152
Modbus.IO.ModbusRTUTransport ReadResponse - RX: 1,16,7,208,0,3,128,133

Now I do get a flickering on my PLC unit for the RX and TX when I run this script, but I do not see my outputs on my PLC going high. Here is the associated piece of code from the library

{
        using (SerialPort port = new SerialPort("COM1"))
        {
            // configure serial port
            port.BaudRate = 38400;
            port.DataBits = 8;
            port.Parity = Parity.Odd;
            port.StopBits = StopBits.One;
            port.Open();

            // create modbus master
            IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);

            byte slaveId = 1;
            ushort startAddress = 2000;
            //ushort[] registers = new ushort[] { 1, 2, 3 };
            ushort[] registers = new ushort[] { 01, 05, 15 };
            // write three registers
            master.WriteMultipleRegisters(slaveId, startAddress, registers);
        }
    }

Can someone help me in understanding the TX and RX, and also perhaps help to point me in the direction to pinpoint where I can look to find how to set outputs high from the C# script?

Upvotes: 1

Views: 7206

Answers (1)

Ronaldo
Ronaldo

Reputation: 635

It is easier to understand the TX and RX data if you convert the bytes to its hexadecimal representation.

For TX data, we have:

01 10 07 D0 00 03 06 00 01 00 05 00 0F 95 98

01 is the Modbus slave address

10 is the Modbus function code for the Write Multiple Registers function

07 D0 is the starting address (2000)

00 03 is the number of registers being written to (3)

06 is the number of bytes containing register data (each register holds 2 bytes, as you're writing to 3 registers, we have 6 bytes being transferred)

00 01 is the value being written to register 2000

00 05 is the value being written to register 2001

00 0F is the value being written to register 2002

95 98 is the calculated CRC for this frame

For RX data we have:

01 10 07 D0 00 03 80 85

The response for the Write Mulitple Registers function is just a response with the same slave address, function code, starting address and the number of registers from the request, plus the calculated CRC for the response frame (80 85).

You can get the Modbus specifications from http://www.modbus.org/specs.php. You can find the format for all request and response frames in the Modbus Application Protocol Specification.

You need to check your PLC documentation to see how the outputs are mapped to registers. As they're on/off outputs, they're probably mapped to coils, in this case you should use the Write Multiple Coils function.

Upvotes: 3

Related Questions