Donate Share

Java Modbus Library

Tracker: Bugs

5 several bugs with fixes - ID: 625113
Last Update: Comment added ( wimpi )

hello,

as i was using your modbus library i found several bugs
in your implementation. several are about
unsigned/signed problems, e.g. you treated the
reference as a signed short. even if some of the
fields, like the length fields (word count or byte
count) cannot be above 2^15-1 i think as a matter of
conciseness these length fields should also be treated
as unsigned. in contrary for the data of a modbus frame
one should find one convention, either signed or
unsigned and stick to it. there was one case where data
was treated as unsigned.

below you will find some kind of pseudo diff of the
changes i did to the code.

finally i would like to thank you for your work you put
into the project and the documentation.

in file ModbusTCPTransport.java:
***
public ModbusRequest readRequest() throws
ModbusIOException {
//System.out.println("readRequest()");
try {
//Timeout? Infinite could be problematic
! int transactionID = m_Input.readUnsignedShort();
! int protocolID = m_Input.readUnsignedShort();
! int dataLength = m_Input.readUnsignedShort();
int unitID = m_Input.readUnsignedByte();
int functionCode = m_Input.readUnsignedByte();


***
public ModbusResponse readResponse() throws
ModbusIOException {
try {
! int transactionID = m_Input.readUnsignedShort();
//System.out.println("Read tid="+transactionID);
! int protocolID = m_Input.readUnsignedShort();
//System.out.println("Read pid="+protocolID);
! int dataLength = m_Input.readUnsignedShort();
//System.out.println("Read length="+dataLength);
int unitID = m_Input.readUnsignedByte();
//System.out.println("Read uid="+unitID);
int functionCode = m_Input.readUnsignedByte();


in file ModbusResponse.java:
***
response = new WriteCoilResponse();
break;
default:
! ExceptionResponse r = new ExceptionResponse();
! r.setExceptionCode(functionCode);
! response = r;
response.setHeadless(headless);
return response;
}


in all files implementing a modbus request and
implementing readData:
***
}//assembleData

public void readData(DataInputStream in) throws
IOException, EOFException {
! m_Reference = in.readUnsignedShort();
! m_BitCount = in.readUnsignedShort();
}//readData

}//class ReadCoilsRequest

in contrary in file ReadInputRegistersResponse.java
***

m_RegisterValues = new int[getWordCount()];
for (int k = 0; k < getWordCount(); k++) {
! m_RegisterValues[k] = in.readShort();
}

//update data length

in file ReadMultipleRegistersResponse.java you have a
misscount by one problem plus the readShort:
***
* @throws <tt>IndexOutOfBoundsException</tt> if
* the index is out of bounds.
*/
public int getRegisterValue(int index)
throws IndexOutOfBoundsException {

! if (index >= getWordCount()) {
throw new IndexOutOfBoundsException();
} else {
! return m_RegisterValues[index];
}
}//getRegisterValue

+ public int[] getRegisterValues() {
+ return m_RegisterValues;
+ }//getRegisterValues
+
public void setRegisterValue(int index, int value)
throws IndexOutOfBoundsException {
m_RegisterValues[index] = value;
***
m_RegisterValues = new int[getWordCount()];

for (int k = 0; k < getWordCount(); k++) {
! m_RegisterValues[k] = in.readShort();
}

//update data length

several problems in file WriteMultipleRegistersRequest.java
***
*/
public void setRegisterValues(int[] values) {
m_RegisterValues = values;
! setDataLength(5 + values.length * 2);
setChanged(true);
}//setRegisterValues

***
public int getRegisterValue(int index)
throws IndexOutOfBoundsException {

! if (index >= getWordCount()) {
throw new IndexOutOfBoundsException();
} else {
! return m_RegisterValues[index];
}
}//getRegisterValue

***
}//assembleData

public void readData(DataInputStream in) throws
IOException, EOFException {
! m_Reference = in.readUnsignedShort();
//read lengths
! int wc = in.readUnsignedShort();
int bc = in.readUnsignedByte();

//read values


Christian Schuhegger ( cs224 ) - 2002-10-18 09:25

5

Closed

Fixed

Dieter Wimberger

Messages

v1.0

Public


Comments ( 2 )




Date: 2002-10-30 20:25
Sender: wimpiProject AdminAccepting Donations

Logged In: YES
user_id=36782

Christian,

I have commited the various fixes to the HEAD on CVS. Probably you
want to give it a check.
Some fixes are a bit different to your proposal but more simple to catch
(added constructors with params to ExceptionResponse for example).

Thanks for your contribution.

Regards,
Dieter


Date: 2002-10-29 22:48
Sender: wimpiProject AdminAccepting Donations

Logged In: YES
user_id=36782

Hi Christian,

I am going to check out your report and follow up with fixes.

Thanks,
Dieter


Log in to comment.

Attached File

No Files Currently Attached

Changes ( 4 )

Field Old Value Date By
status_id Open 2002-10-30 20:25 wimpi
resolution_id None 2002-10-30 20:25 wimpi
close_date - 2002-10-30 20:25 wimpi
assigned_to nobody 2002-10-29 22:48 wimpi