📄 serialparameters.java
字号:
//License/*** * Java Modbus Library (jamod) * Copyright (c) 2002-2004, jamod development team * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the author nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. ***/package net.wimpi.modbus.util;import net.wimpi.modbus.Modbus;import javax.comm.SerialPort;import java.util.Properties;/** * Helper class wrapping all serial port communication parameters. * Very similar to the javax.comm demos, however, not the same. * * @author Dieter Wimberger * @author John Charlton * @version 1.2rc1 (09/11/2004) */public class SerialParameters { //instance attributes private String m_PortName; private int m_BaudRate; private int m_FlowControlIn; private int m_FlowControlOut; private int m_Databits; private int m_Stopbits; private int m_Parity; private String m_Encoding; private boolean m_Echo; /** * Constructs a new <tt>SerialParameters</tt> instance with * default values. */ public SerialParameters() { 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_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. */ public SerialParameters(String portName, int baudRate, int flowControlIn, int flowControlOut, int databits, int stopbits, int parity, boolean echo) { m_PortName = portName; m_BaudRate = baudRate; m_FlowControlIn = flowControlIn; m_FlowControlOut = flowControlOut; m_Databits = databits; m_Stopbits = stopbits; m_Parity = parity; m_Echo = echo; }//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. */ public SerialParameters(Properties props, String prefix) { 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"))); }//constructor /** * Sets the port name. * * @param name the new port name. */ public void setPortName(String name) { m_PortName = name; }//setPortName /** * Returns the port name. * * @return the port name. */ public String getPortName() { return m_PortName; }//getPortName /** * Sets the baud rate. * * @param rate the new baud rate. */ public void setBaudRate(int rate) { m_BaudRate = rate; }//setBaudRate /** * Sets the baud rate. * * @param rate the new baud rate. */ public void setBaudRate(String rate) { m_BaudRate = Integer.parseInt(rate); }//setBaudRate /** * Return the baud rate as <tt>int</tt>. * * @return the baud rate as <tt>int</tt>. */ public int getBaudRate() { return m_BaudRate; }//getBaudRate /** * Returns the baud rate as a <tt>String</tt>. * * @return the baud rate as <tt>String</tt>. */ public String getBaudRateString() { return Integer.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. */ public void setFlowControlIn(int flowcontrol) { 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. */ public void setFlowControlIn(String flowcontrol) { 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>. */ public int getFlowControlIn() { return m_FlowControlIn; }//getFlowControlIn /** * Returns the input flow control type as <tt>String</tt>. * * @return the input flow control type as <tt>String</tt>. */ public String getFlowControlInString() { 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>. */ public void setFlowControlOut(int flowControlOut) { 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>. */ public void setFlowControlOut(String flowControlOut) { 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>. */ public int getFlowControlOut() { return m_FlowControlOut; }//getFlowControlOut /** * Returns the output flow control type as <tt>String</tt>. * * @return the output flow control type as <tt>String</tt>. */ public String getFlowControlOutString() { return flowToString(m_FlowControlOut); }//getFlowControlOutString /** * Sets the number of data bits. * * @param databits the new number of data bits. */ public void setDatabits(int databits) { 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>. */ public void setDatabits(String databits) { if (databits.equals("5")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -