Commits

Anonymous committed 7875556

adding separate write coil message

Comments (0)

Files changed (3)

org.openscada.protocol.modbus/src/org/openscada/protocol/modbus/codec/ModbusProtocol.java

 import org.openscada.protocol.modbus.message.ReadResponse;
 import org.openscada.protocol.modbus.message.WriteDataRequest;
 import org.openscada.protocol.modbus.message.WriteMultiDataResponse;
+import org.openscada.protocol.modbus.message.WriteSingleCoilRequest;
 import org.openscada.protocol.modbus.message.WriteSingleDataResponse;
 
 public class ModbusProtocol
             data.putUnsigned ( writeMessage.getData ().length );
             data.put ( writeMessage.getData () );
         }
+        else if ( message instanceof WriteSingleCoilRequest )
+        {
+            final WriteSingleCoilRequest writeMessage = (WriteSingleCoilRequest)message;
+            data.put ( writeMessage.getFunctionCode () );
+            data.putUnsignedShort ( writeMessage.getAddress () );
+            data.put ( writeMessage.getValue () ? (byte)0xFF : (byte)0x00 );
+            data.put ( (byte)0x00 );
+        }
         else
         {
             throw new IllegalStateException ( String.format ( "Unsupported message type: %s", message.getClass () ) );

org.openscada.protocol.modbus/src/org/openscada/protocol/modbus/message/WriteDataRequest.java

         this ( unitIdentifier, data.length == 2 ? (byte)0x06 : (byte)0x10, startAddress, data );
     }
 
-    public static WriteDataRequest createWriteCoil ( final byte unitIdentifier, final int startAddress, final boolean value )
-    {
-        final byte[] data = new byte[] { value ? (byte)0xFF : (byte)0x00, (byte)0x00 };
-        return new WriteDataRequest ( unitIdentifier, startAddress, data );
-    }
-
     public int getStartAddress ()
     {
         return this.startAddress;

org.openscada.protocol.modbus/src/org/openscada/protocol/modbus/message/WriteSingleCoilRequest.java

+package org.openscada.protocol.modbus.message;
+
+public class WriteSingleCoilRequest extends BaseMessage
+{
+
+    private final int address;
+
+    private final boolean value;
+
+    public WriteSingleCoilRequest ( final byte unitIdentifier, final int address, final boolean value )
+    {
+        super ( unitIdentifier, (byte)0x05 );
+        this.address = address;
+        this.value = value;
+    }
+
+    public int getAddress ()
+    {
+        return this.address;
+    }
+
+    public boolean getValue ()
+    {
+        return this.value;
+    }
+
+}