I saw in the
The example was made to only read holding registers.
The datatype is a conversion of returned holding register.
For each type, you should change the transationID like 1,2,3,4 (now you used always 1), when the MBresponseEvent is triggered, in the "case" section you know which request has the number and you can do the corresponding conversion.
In the original example, in the MBresponseEvent, the conversion of the values is done in the conversion functions:
View attachment 147642
I saw in the example the functionality to read Coils also, depending on the bytes assign it. and a function to read, so as you said, this only works for reading Holding Registers, i just did a chenge in the SelectionBos to do exactly as a holdingRegisters does, read a COIL and so on.
[CODE lang="b4x" title="Read COILs"]Public Sub ReadCoils (transactionID As Short,unit As Short, startAddress As Short, numInputs As Short) As Boolean
''error when request is send
If numInputs > 2000 Then
CallException(transactionID,unit,fctReadCoil,excIllegalDataVal)
Return False
End If
Try
If IsConnected = True Then
'Send message to the network
Astream.Write(CreateReadHeader(transactionID,unit,startAddress,numInputs,fctReadCoil))
'To track the send messages en their timeout
TrackTimeout(transactionID)
Return True
Else
CallException(transactionID,unit,fctReadCoil,excExceptionNotConnected)
Return False
End If
Catch
Log(LastException)
End Try
End Sub[/CODE]
[CODE lang="b4x" title="ReadCoil"]
''Constants for access
Private const fctReadCoil As Byte = 1
Private const fctReadDiscreteInputs As Byte= 2
Private const fctReadHoldingRegister As Byte = 3
Private const fctReadInputRegister As Byte = 4
Private const fctWriteSingleCoil As Byte = 5
Private const fctWriteSingleRegister As Byte = 6
Private const fctWriteMultipleCoils As Byte = 15
Private const fctWriteMultipleRegister As Byte = 16
Private const fctReadWriteMultipleRegister As Byte = 23 'not supported in this class
'''''----------------------------------------------------[/CODE]