Net.wimpi.modbus.net.SerialConnection - Class that implements a serial connection which can be used for master and slave implementations. Jamod PROS CONS. Client/server structure of Modbus, where the client acts as the Master and the server. ASCII for serial lines communication; Modbus TCP for.
PermalinkGamemon universal usb converter drivers. Openhab / jamod. This is a fork of Java Modbus Library (jamod). Serial: ASCII, RTU (Master only), BIN; IP: TCP, UDP. Openhab / jamod. This is a fork of Java Modbus Library (jamod). Serial: ASCII, RTU (Master only), BIN; IP: TCP, UDP. I'm using jamod (ModBus api) with a Beaglebone Black which runs on Android. I successfully used it for ModBus TCP Slave app. It worked like a charm. Now I'm trying to use it for Modbus serial slave.
Join GitHub today
![Jamod Jamod](https://www.openmuc.org/openmuc/user-guide/images/webui-dataplotter.png)
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up Find file Copy path
Modbus Tcp Master
Cannot retrieve contributors at this time
Serial Master
/*** |
* 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. |
* |
* Original implementation by jamod development team. |
* This file modified by Charles Hache <[email protected]> |
***/ |
packagenet.wimpi.modbus.util; |
importnet.wimpi.modbus.Modbus; |
importjava.util.Properties; |
importjssc.SerialPort; |
/** |
* Helper class wrapping all serial port communication parameters. |
* |
* @author Dieter Wimberger |
* @author John Charlton |
* @author Charles Hache |
* @version @version@ (@date@) |
*/ |
publicclassSerialParameters { |
// instance attributes |
privateString m_PortName; |
privateint m_BaudRate; |
privateint m_FlowControlIn; |
privateint m_FlowControlOut; |
privateint m_Databits; |
privateint m_Stopbits; |
privateint m_Parity; |
privateString m_Encoding; |
privateboolean m_Echo; |
privateint m_ReceiveTimeout; |
/** |
* Constructs a new <tt>SerialParameters</tt> instance with default values. |
*/ |
publicSerialParameters() { |
m_PortName =''; |
m_BaudRate =9600; |
m_FlowControlIn =SerialPort.FLOWCONTROL_NONE; |
m_FlowControlOut =SerialPort.FLOWCONTROL_NONE; |
m_Databits =SerialPort.DATABITS_8; |
m_Stopbits =SerialPort.STOPBITS_1; |
m_Parity =SerialPort.PARITY_NONE; |
m_Encoding =Modbus.DEFAULT_SERIAL_ENCODING; |
m_ReceiveTimeout =500; // 0.5 secs |
m_Echo =false; |
}// constructor |
/** |
* Constructs a new <tt>SerialParameters<tt> instance with |
* given parameters. |
* |
* @param portName |
* The name of the port. |
* @param baudRate |
* The baud rate. |
* @param flowControlIn |
* Type of flow control for receiving. |
* @param flowControlOut |
* Type of flow control for sending. |
* @param databits |
* The number of data bits. |
* @param stopbits |
* The number of stop bits. |
* @param parity |
* The type of parity. |
* @param echo |
* Flag for setting the RS485 echo mode. |
* @param timeout |
* Timeout for serial reads, in milliseconds. |
*/ |
publicSerialParameters(StringportName, intbaudRate, intflowControlIn, |
intflowControlOut, intdatabits, intstopbits, intparity, |
booleanecho, inttimeout) { |
m_PortName = portName; |
m_BaudRate = baudRate; |
m_FlowControlIn = flowControlIn; |
m_FlowControlOut = flowControlOut; |
m_Databits = databits; |
m_Stopbits = stopbits; |
m_Parity = parity; |
m_Echo = echo; |
m_ReceiveTimeout = timeout; |
}// constructor |
/** |
* Constructs a new <tt>SerialParameters</tt> instance with parameters |
* obtained from a <tt>Properties</tt> instance. |
* |
* @param props |
* a <tt>Properties</tt> instance. |
* @param prefix |
* a prefix for the properties keys if embedded into other |
* properties. |
*/ |
publicSerialParameters(Propertiesprops, Stringprefix) { |
if (prefix null) { |
prefix =''; |
} |
setPortName(props.getProperty(prefix +'portName', '')); |
setBaudRate(props.getProperty(prefix +'baudRate', ''+9600)); |
setFlowControlIn(props.getProperty(prefix +'flowControlIn', '' |
+SerialPort.FLOWCONTROL_NONE)); |
setFlowControlOut(props.getProperty(prefix +'flowControlOut', '' |
+SerialPort.FLOWCONTROL_NONE)); |
setParity(props.getProperty(prefix +'parity', '' |
+SerialPort.PARITY_NONE)); |
setDatabits(props.getProperty(prefix +'databits', '' |
+SerialPort.DATABITS_8)); |
setStopbits(props.getProperty(prefix +'stopbits', '' |
+SerialPort.STOPBITS_1)); |
setEncoding(props.getProperty(prefix +'encoding', |
Modbus.DEFAULT_SERIAL_ENCODING)); |
setEcho('true'.equals(props.getProperty(prefix +'echo'))); |
setReceiveTimeout(props.getProperty(prefix +'timeout', ''+500)); |
}// constructor |
/** |
* Sets the port name. |
* |
* @param name |
* the new port name. |
*/ |
publicvoidsetPortName(Stringname) { |
m_PortName = name; |
}// setPortName |
/** |
* Returns the port name. |
* |
* @return the port name. |
*/ |
publicStringgetPortName() { |
return m_PortName; |
}// getPortName |
/** |
* Sets the baud rate. |
* |
* @param rate |
* the new baud rate. |
*/ |
publicvoidsetBaudRate(intrate) { |
m_BaudRate = rate; |
}// setBaudRate |
/** |
* Sets the baud rate. |
* |
* @param rate |
* the new baud rate. |
*/ |
publicvoidsetBaudRate(Stringrate) { |
m_BaudRate =Integer.parseInt(rate); |
}// setBaudRate |
/** |
* Return the baud rate as <tt>int</tt>. |
* |
* @return the baud rate as <tt>int</tt>. |
*/ |
publicintgetBaudRate() { |
return m_BaudRate; |
}// getBaudRate |
/** |
* Returns the baud rate as a <tt>String</tt>. |
* |
* @return the baud rate as <tt>String</tt>. |
*/ |
publicStringgetBaudRateString() { |
returnInteger.toString(m_BaudRate); |
}// getBaudRateString |
/** |
* Sets the type of flow control for the input as given by the passed in |
* <tt>int</tt>. |
* |
* @param flowcontrol |
* the new flow control type. |
*/ |
publicvoidsetFlowControlIn(intflowcontrol) { |
m_FlowControlIn = flowcontrol; |
}// setFlowControl |
/** |
* Sets the type of flow control for the input as given by the passed in |
* <tt>String</tt>. |
* |
* @param flowcontrol |
* the flow control for reading type. |
*/ |
publicvoidsetFlowControlIn(Stringflowcontrol) { |
m_FlowControlIn = stringToFlow(flowcontrol); |
}// setFlowControlIn |
/** |
* Returns the input flow control type as <tt>int</tt>. |
* |
* @return the input flow control type as <tt>int</tt>. |
*/ |
publicintgetFlowControlIn() { |
return m_FlowControlIn; |
}// getFlowControlIn |
/** |
* Returns the input flow control type as <tt>String</tt>. |
* |
* @return the input flow control type as <tt>String</tt>. |
*/ |
publicStringgetFlowControlInString() { |
return flowToString(m_FlowControlIn); |
}// getFlowControlIn |
/** |
* Sets the output flow control type as given by the passed in <tt>int</tt>. |
* |
* @param flowControlOut |
* new output flow control type as <tt>int</tt>. |
*/ |
publicvoidsetFlowControlOut(intflowControlOut) { |
m_FlowControlOut = flowControlOut; |
}// setFlowControlOut |
/** |
* Sets the output flow control type as given by the passed in |
* <tt>String</tt>. |
* |
* @param flowControlOut |
* the new output flow control type as <tt>String</tt>. |
*/ |
publicvoidsetFlowControlOut(StringflowControlOut) { |
m_FlowControlOut = stringToFlow(flowControlOut); |
}// setFlowControlOut |
/** |
* Returns the output flow control type as <tt>int</tt>. |
* |
* @return the output flow control type as <tt>int</tt>. |
*/ |
publicintgetFlowControlOut() { |
return m_FlowControlOut; |
}// getFlowControlOut |
/** |
* Returns the output flow control type as <tt>String</tt>. |
* |
* @return the output flow control type as <tt>String</tt>. |
*/ |
publicStringgetFlowControlOutString() { |
return flowToString(m_FlowControlOut); |
}// getFlowControlOutString |
/** |
* Sets the number of data bits. |
* |
* @param databits |
* the new number of data bits. |
*/ |
publicvoidsetDatabits(intdatabits) { |
m_Databits = databits; |
}// setDatabits |
/** |
* Sets the number of data bits from the given <tt>String</tt>. |
* |
* @param databits |
* the new number of data bits as <tt>String</tt>. |
*/ |
publicvoidsetDatabits(Stringdatabits) { |
if (databits.equals('5')) { |
m_Databits =SerialPort.DATABITS_5; |
} |
if (databits.equals('6')) { |
m_Databits =SerialPort.DATABITS_6; |
} |
if (databits.equals('7')) { |
m_Databits =SerialPort.DATABITS_7; |
} |
if (databits.equals('8')) { |
m_Databits =SerialPort.DATABITS_8; |
} |
}// setDatabits |
/** |
* Returns the number of data bits as <tt>int</tt>. |
* |
* @return the number of data bits as <tt>int</tt>. |
*/ |
publicintgetDatabits() { |
return m_Databits; |
}// getDatabits |
/** |
* Returns the number of data bits as <tt>String</tt>. |
* |
* @return the number of data bits as <tt>String</tt>. |
*/ |
publicStringgetDatabitsString() { |
switch (m_Databits) { |
caseSerialPort.DATABITS_5: |
return'5'; |
caseSerialPort.DATABITS_6: |
return'6'; |
caseSerialPort.DATABITS_7: |
return'7'; |
caseSerialPort.DATABITS_8: |
return'8'; |
default: |
return'8'; |
} |
}// getDataBits |
/** |
* Sets the number of stop bits. |
* |
* @param stopbits |
* the new number of stop bits setting. |
*/ |
publicvoidsetStopbits(intstopbits) { |
m_Stopbits = stopbits; |
}// setStopbits |
/** |
* Sets the number of stop bits from the given <tt>String</tt>. |
* |
* @param stopbits |
* the number of stop bits as <tt>String</tt>. |
*/ |
publicvoidsetStopbits(Stringstopbits) { |
if (stopbits.equals('1')) { |
m_Stopbits =SerialPort.STOPBITS_1; |
} |
if (stopbits.equals('1.5')) { |
m_Stopbits =SerialPort.STOPBITS_1_5; |
} |
if (stopbits.equals('2')) { |
m_Stopbits =SerialPort.STOPBITS_2; |
} |
}// setStopbits |
/** |
* Returns the number of stop bits as <tt>int</tt>. |
* |
* @return the number of stop bits as <tt>int</tt>. |
*/ |
publicintgetStopbits() { |
return m_Stopbits; |
}// getStopbits |
/** |
* Returns the number of stop bits as <tt>String</tt>. |
* |
* @return the number of stop bits as <tt>String</tt>. |
*/ |
publicStringgetStopbitsString() { |
switch (m_Stopbits) { |
caseSerialPort.STOPBITS_1: |
return'1'; |
caseSerialPort.STOPBITS_1_5: |
return'1.5'; |
caseSerialPort.STOPBITS_2: |
return'2'; |
default: |
return'1'; |
} |
}// getStopbitsString |
/** |
* Sets the parity schema. |
* |
* @param parity |
* the new parity schema as <tt>int</tt>. |
*/ |
publicvoidsetParity(intparity) { |
m_Parity = parity; |
}// setParity |
/** |
* Sets the parity schema from the given <tt>String</tt>. |
* |
* @param parity |
* the new parity schema as <tt>String</tt>. |
*/ |
publicvoidsetParity(Stringparity) { |
parity = parity.toLowerCase(); |
if (parity.equals('none')) { |
m_Parity =SerialPort.PARITY_NONE; |
} |
if (parity.equals('even')) { |
m_Parity =SerialPort.PARITY_EVEN; |
} |
if (parity.equals('odd')) { |
m_Parity =SerialPort.PARITY_ODD; |
} |
}// setParity |
/** |
* Returns the parity schema as <tt>int</tt>. |
* |
* @return the parity schema as <tt>int</tt>. |
*/ |
publicintgetParity() { |
return m_Parity; |
}// getParity |
/** |
* Returns the parity schema as <tt>String</tt>. |
* |
* @return the parity schema as <tt>String</tt>. |
*/ |
publicStringgetParityString() { |
switch (m_Parity) { |
caseSerialPort.PARITY_NONE: |
return'none'; |
caseSerialPort.PARITY_EVEN: |
return'even'; |
caseSerialPort.PARITY_ODD: |
return'odd'; |
default: |
return'none'; |
} |
}// getParityString |
/** |
* Sets the encoding to be used. |
* |
* @param enc |
* the encoding as string. |
* @see Modbus#SERIAL_ENCODING_ASCII |
* @see Modbus#SERIAL_ENCODING_RTU |
* @see Modbus#SERIAL_ENCODING_BIN |
*/ |
publicvoidsetEncoding(Stringenc) { |
enc = enc.toLowerCase(); |
if (enc.equals(Modbus.SERIAL_ENCODING_ASCII) |
|| enc.equals(Modbus.SERIAL_ENCODING_RTU) |
|| enc.equals(Modbus.SERIAL_ENCODING_BIN)) { |
m_Encoding = enc; |
} else { |
m_Encoding =Modbus.DEFAULT_SERIAL_ENCODING; |
} |
}// setEncoding |
/** |
* Returns the encoding to be used. |
* |
* @return the encoding as string. |
* @see Modbus#SERIAL_ENCODING_ASCII |
* @see Modbus#SERIAL_ENCODING_RTU |
* @see Modbus#SERIAL_ENCODING_BIN |
*/ |
publicStringgetEncoding() { |
return m_Encoding; |
}// getEncoding |
/** |
* Get the Echo value. |
* |
* @return the Echo value. |
*/ |
publicbooleanisEcho() { |
return m_Echo; |
}// getEcho |
/** |
* Set the Echo value. |
* |
* @param newEcho |
* The new Echo value. |
*/ |
publicvoidsetEcho(booleannewEcho) { |
m_Echo = newEcho; |
}// setEcho |
/** |
* Returns the receive timeout for serial communication. |
* |
* @return the timeout in milliseconds. |
*/ |
publicintgetReceiveTimeout() { |
return m_ReceiveTimeout; |
}// getReceiveTimeout |
/** |
* Sets the receive timeout for serial communication. |
* |
* @param receiveTimeout |
* the receiveTimeout in milliseconds. |
*/ |
publicvoidsetReceiveTimeout(intreceiveTimeout) { |
m_ReceiveTimeout = receiveTimeout; |
}// setReceiveTimeout |
/** |
* Sets the receive timeout for the serial communication parsing the given |
* String using <tt>Integer.parseInt(String)</tt>. |
* |
* @param str |
* the timeout as String. |
*/ |
publicvoidsetReceiveTimeout(Stringstr) { |
m_ReceiveTimeout =Integer.parseInt(str); |
}// setReceiveTimeout |
/** |
* Converts a <tt>String</tt> describing a flow control type to the |
* <tt>int</tt> which is defined in SerialPort. |
* |
* @param flowcontrol |
* the <tt>String</tt> describing the flow control type. |
* @return the <tt>int</tt> describing the flow control type. |
*/ |
privateintstringToFlow(Stringflowcontrol) { |
flowcontrol = flowcontrol.toLowerCase(); |
if (flowcontrol.equals('none')) { |
returnSerialPort.FLOWCONTROL_NONE; |
} |
if (flowcontrol.equals('xon/xoff out')) { |
returnSerialPort.FLOWCONTROL_XONXOFF_OUT; |
} |
if (flowcontrol.equals('xon/xoff in')) { |
returnSerialPort.FLOWCONTROL_XONXOFF_IN; |
} |
if (flowcontrol.equals('rts/cts in')) { |
returnSerialPort.FLOWCONTROL_RTSCTS_IN; |
} |
if (flowcontrol.equals('rts/cts out')) { |
returnSerialPort.FLOWCONTROL_RTSCTS_OUT; |
} |
returnSerialPort.FLOWCONTROL_NONE; |
}// stringToFlow |
/** |
* Converts an <tt>int</tt> describing a flow control type to a String |
* describing a flow control type. |
* |
* @param flowcontrol |
* the <tt>int</tt> describing the flow control type. |
* @return the <tt>String</tt> describing the flow control type. |
*/ |
privateStringflowToString(intflowcontrol) { |
switch (flowcontrol) { |
caseSerialPort.FLOWCONTROL_NONE: |
return'none'; |
caseSerialPort.FLOWCONTROL_XONXOFF_OUT: |
return'xon/xoff out'; |
caseSerialPort.FLOWCONTROL_XONXOFF_IN: |
return'xon/xoff in'; |
caseSerialPort.FLOWCONTROL_RTSCTS_IN: |
return'rts/cts in'; |
caseSerialPort.FLOWCONTROL_RTSCTS_OUT: |
return'rts/cts out'; |
default: |
return'none'; |
} |
}// flowToString |
/** |
* Populates the settings from an <tt>Proper</tt> that reads from a |
* properties file or contains a set of properties. |
* |
* @param in |
* the <tt>InputStream</tt> to read from. |
* |
* private void loadFrom(InputStream in) throws IOException { |
* Properties props = new Properties(); props.load(in); |
* setPortName(props.getProperty('portName')); |
* setBaudRate(props.getProperty('baudRate')); |
* setFlowControlIn(props.getProperty('flowControlIn')); |
* setFlowControlOut(props.getProperty('flowControlOut')); |
* setParity(props.getProperty('parity')); |
* setDatabits(props.getProperty('databits')); |
* setStopbits(props.getProperty('stopbits')); |
* setEncoding(props.getProperty('encoding')); setEcho(new |
* Boolean(props.getProperty('echo')).booleanValue()); |
* }//loadFrom |
*/ |
}// class SerialParameters |
Virtual Dj Serial Master Key
Copy lines Copy permalink