Modbus Serial Master Jamodd
Github page for the Java Modbus Library (jamod) Project on Sourceforge.
Thank you for creating a useful tool. I wish I could copy the text in the Bus Monitor and ADU windows. It is my hope that you can freely copy and paste this content into a text editor.
RTU and TCP communicated with my device on the first or second try. Make sure your serial port configurations match.
Easy! Just downloaded and ran on windows 7 64 bit. Used COM4 fine at the first attempt.
No able to open comport other than default com port provided in application. I tried to give COM9, COM10, COM11 and it always ends up in error such as INFO 2017-06-03T20:41:51.322 .qModMastersrcmodbusadapter.cpp @ 39 'Connecting to Serial Port [COM10]..' ERROR 2017-06-03T20:41:51.322 .qModMastersrcmodbusadapter.cpp @ 50 Connection failed. Could not connect to serial port INFO 2017-06-03T20:41:51.322 .qModMastersrcrawdatamodel.cpp @ 17 Raw Data Model Line = 'Sys > 20:41:51:322 - Connecting to Serial Port [COM10]..Failed' , No of lines = 1 INFO 2017-06-03T20:42:30.593 .qModMastersrcmodbusadapter.cpp @ 39 'Connecting to Serial Port [COM10:]..' ERROR 2017-06-03T20:42:30.593 .qModMastersrcmodbusadapter.cpp @ 50 Connection failed. Could not connect to serial port INFO 2017-06-03T20:42:30.593 .qModMastersrcrawdatamodel.cpp @ 17 Raw Data Model Line = 'Sys > 20:42:30:593 - Connecting to Serial Port [COM10:]..Failed' , No of lines = 5
Join GitHub today
Modbus Serial Master Jamodd Online
Yaesu ft 736r repair. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Modbus Serial Master Jamodd Key
Sign up/*** |
* Copyright 2002-2010 jamod development team |
* |
* Licensed under the Apache License, Version 2.0 (the 'License'); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an 'AS IS' BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
***/ |
packagenet.wimpi.modbus.io; |
importnet.wimpi.modbus.Modbus; |
importnet.wimpi.modbus.ModbusIOException; |
importnet.wimpi.modbus.msg.ModbusMessage; |
importnet.wimpi.modbus.msg.ModbusRequest; |
importnet.wimpi.modbus.msg.ModbusResponse; |
importnet.wimpi.modbus.util.ModbusUtil; |
importjava.io.IOException; |
importjssc.SerialInputStream; |
importjssc.SerialOutputStream; |
/** |
* Class that implements the ModbusRTU transport flavor. |
* |
* @author John Charlton |
* @author Dieter Wimberger |
* |
* @version @version@ (@date@) |
*/ |
publicclassModbusRTUTransportextendsModbusSerialTransport { |
privateSerialInputStream m_InputStream; // wrap into filter input |
privateSerialOutputStream m_OutputStream; // wrap into filter output |
privatebyte[] m_InBuffer; |
privateBytesInputStream m_ByteIn; // to read message from |
privateBytesOutputStream m_ByteInOut; // to buffer message to |
privateBytesOutputStream m_ByteOut; // write frames |
privatebyte[] lastRequest =null; |
publicvoidwriteMessage(ModbusMessagemsg) throwsModbusIOException { |
try { |
int len; |
synchronized (m_ByteOut) { |
// first clear any input from the receive buffer to prepare |
// for the reply since RTU doesn't have message delimiters |
clearInput(); |
// write message to byte out |
m_ByteOut.reset(); |
msg.setHeadless(); |
msg.writeTo(m_ByteOut); |
len = m_ByteOut.size(); |
int[] crc =ModbusUtil.calculateCRC(m_ByteOut.getBuffer(), 0, |
len); |
m_ByteOut.writeByte(crc[0]); |
m_ByteOut.writeByte(crc[1]); |
// write message |
len = m_ByteOut.size(); |
byte buf[] = m_ByteOut.getBuffer(); |
m_OutputStream.write(buf, 0, len); // PDU + CRC |
m_OutputStream.flush(); |
if (Modbus.debug) |
System.out |
.println('Sent: '+ModbusUtil.toHex(buf, 0, len)); |
// clears out the echoed message |
// for RS485 |
if (m_Echo) { |
readEcho(len); |
} |
lastRequest =newbyte[len]; |
System.arraycopy(buf, 0, lastRequest, 0, len); |
} |
} catch (Exception ex) { |
thrownewModbusIOException('I/O failed to write'); |
} |
}// writeMessage |
// This is required for the slave that is not supported |
publicModbusRequestreadRequest() throwsModbusIOException { |
thrownewRuntimeException('Operation not supported.'); |
} // readRequest |
/** |
* Clear the input if characters are found in the input stream. |
* |
* @throws IOException |
*/ |
publicvoidclearInput() throwsIOException { |
if (m_InputStream.available() >0) { |
int len = m_InputStream.available(); |
byte buf[] =newbyte[len]; |
m_InputStream.read(buf, 0, len); |
if (Modbus.debug) |
System.out.println('Clear input: ' |
+ModbusUtil.toHex(buf, 0, len)); |
} |
}// cleanInput |
publicModbusResponsereadResponse() throwsModbusIOException { |
boolean done =false; |
ModbusResponse response =null; |
int dlength =0; |
try { |
do { |
// 1. read to function code, create request and read function |
// specific bytes |
synchronized (m_ByteIn) { |
int uid = m_InputStream.read(); |
if (uid !=-1) { |
int fc = m_InputStream.read(); |
m_ByteInOut.reset(); |
m_ByteInOut.writeByte(uid); |
m_ByteInOut.writeByte(fc); |
// create response to acquire length of message |
response =ModbusResponse.createModbusResponse(fc); |
response.setHeadless(); |
// With Modbus RTU, there is no end frame. Either we |
// assume |
// the message is complete as is or we must do function |
// specific processing to know the correct length. To |
// avoid |
// moving frame timing to the serial input functions, we |
// set the |
// timeout and to message specific parsing to read a |
// response. |
getResponse(fc, m_ByteInOut); |
dlength = m_ByteInOut.size() -2; // less the crc |
if (Modbus.debug) |
System.out.println('Response: ' |
+ModbusUtil.toHex(m_ByteInOut.getBuffer(), |
0, dlength +2)); |
m_ByteIn.reset(m_InBuffer, dlength); |
// check CRC |
int[] crc =ModbusUtil.calculateCRC(m_InBuffer, 0, |
dlength); // does not include CRC |
if (ModbusUtil.unsignedByteToInt(m_InBuffer[dlength]) != crc[0] |
ModbusUtil |
.unsignedByteToInt(m_InBuffer[dlength +1]) != crc[1]) { |
thrownewIOException( |
'CRC Error in received frame: ' |
+ dlength |
+' bytes: ' |
+ModbusUtil.toHex( |
m_ByteIn.getBuffer(), 0, |
dlength)); |
} |
} else { |
thrownewIOException('Error reading response'); |
} |
// read response |
m_ByteIn.reset(m_InBuffer, dlength); |
if (response !=null) { |
response.readFrom(m_ByteIn); |
} |
done =true; |
}// synchronized |
} while (!done); |
return response; |
} catch (Exception ex) { |
System.err |
.println('Last request: '+ModbusUtil.toHex(lastRequest)); |
System.err.println(ex.getMessage()); |
thrownewModbusIOException('I/O exception - failed to read'); |
} |
}// readResponse |
/** |
* Prepares the input and output streams of this <tt>ModbusRTUTransport</tt> |
* instance. |
* |
* @param in |
* the input stream to be read from. |
* @param out |
* the output stream to write to. |
* @throws IOException |
* if an IO error occurs. |
*/ |
publicvoidprepareStreams(SerialInputStreamin, SerialOutputStreamout) { |
m_InputStream = in; |
m_OutputStream = out; |
m_ByteOut =newBytesOutputStream(Modbus.MAX_MESSAGE_LENGTH); |
m_InBuffer =newbyte[Modbus.MAX_MESSAGE_LENGTH]; |
m_ByteIn =newBytesInputStream(m_InBuffer); |
m_ByteInOut =newBytesOutputStream(m_InBuffer); |
} // prepareStreams |
publicvoidclose() throwsIOException { |
m_InputStream.close(); |
m_OutputStream.close(); |
}// close |
privatevoidgetResponse(intfn, BytesOutputStreamout) throwsIOException { |
int bc =-1, bc2 =-1, bcw =-1; |
int inpBytes =0; |
byte inpBuf[] =newbyte[256]; |
try { |
switch (fn) { |
case0x01: |
case0x02: |
case0x03: |
case0x04: |
case0x0C: |
case0x11:// report slave ID version and run/stop state |
case0x14:// read log entry (60000 memory reference) |
case0x15:// write log entry (60000 memory reference) |
case0x17: |
// read the byte count; |
bc = m_InputStream.read(); |
out.write(bc); |
// now get the specified number of bytes and the 2 CRC bytes |
inpBytes = m_InputStream.blockingRead(inpBuf, 0, bc +2); |
out.write(inpBuf, 0, inpBytes); |
if (inpBytes != bc +2) { |
System.out.println('Error: looking for '+ (bc +2) |
+' bytes, received '+ inpBytes); |
} |
break; |
case0x05: |
case0x06: |
case0x0B: |
case0x0F: |
case0x10: |
// read status: only the CRC remains after address and function |
// code |
inpBytes = m_InputStream.blockingRead(inpBuf, 0, 6); |
out.write(inpBuf, 0, inpBytes); |
break; |
case0x07: |
case0x08: |
// read status: only the CRC remains after address and function |
// code |
inpBytes = m_InputStream.blockingRead(inpBuf, 0, 3); |
out.write(inpBuf, 0, inpBytes); |
break; |
case0x16: |
// eight bytes in addition to the address and function codes |
inpBytes = m_InputStream.blockingRead(inpBuf, 0, 8); |
out.write(inpBuf, 0, inpBytes); |
break; |
case0x18: |
// read the byte count word |
bc = m_InputStream.read(); |
out.write(bc); |
bc2 = m_InputStream.read(); |
out.write(bc2); |
bcw =ModbusUtil.makeWord(bc, bc2); |
// now get the specified number of bytes and the 2 CRC bytes |
inpBytes = m_InputStream.blockingRead(inpBuf, 0, bcw +2); |
out.write(inpBuf, 0, inpBytes); |
break; |
} |
} catch (IOException e) { |
thrownewIOException('getResponse serial port exception'); |
} |
}// getResponse |
@Override |
publicvoidflush() { |
try { |
m_InputStream.skip(m_InputStream.available()); |
} catch (IOException e) { |
// TODO: If flushing the buffer fails, what should we do? |
} |
} |
} // ModbusRTUTransport |
Copy lines Copy permalink