本文整理汇总了Java中gnu.io.SerialPort类的典型用法代码示例。如果您正苦于以下问题:Java SerialPort类的具体用法?Java SerialPort怎么用?Java SerialPort使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SerialPort类属于gnu.io包,在下文中一共展示了SerialPort类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initialize
import gnu.io.SerialPort; //导入依赖的package包/类
public void initialize() {
System.setProperty("gnu.io.rxtx.SerialPorts", getComPortName());
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
if(currPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (currPortId.getName().equals(txtComPortName.getText())) {
System.out.println(txtComPortName.getText());
portId = currPortId;
break;
}
}
}
if (portId == null) {
JOptionPane.showMessageDialog(null," Portuna bağlı cihaz yok!","Hata",JOptionPane.ERROR_MESSAGE);
System.out.println("Porta bağlı cihaz yok!");
return;
}
System.out.println(portId);
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
serialPort.addEventListener((SerialPortEventListener) this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
示例2: SerialComm
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Constructor
*
* @param portName
* The name of the serial port
* @param listeners
* The listeners for incoming telegrams
* @throws Exception
*/
public SerialComm(String portName, ArrayList<EnoceanListener> listeners)
throws Exception {
this.listeners = listeners;
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
throw new Exception("Port is currently in use");
}
CommPort commPort = portIdentifier
.open(this.getClass().getName(), 2000); // timeout 2 s.
if (!(commPort instanceof SerialPort)) {
throw new Exception("Only serial port is supported");
}
port = commPort;
SerialPort serialPort = (SerialPort) commPort;
// 57600 bit/s, 8 bits, stop bit length 1, no parity bit
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
input = serialPort.getInputStream();
output = serialPort.getOutputStream();
}
示例3: setDatabits
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Sets the number of data bits.
*
* @param databits the new number of data bits.
*/
public void setDatabits(int databits) throws IllegalArgumentException {
if (!SerialParameterValidator.isDataBitsValid(databits)) {
throw new IllegalArgumentException("Databit '" + databits + "' invalid");
}
switch (databits) {
case 5:
m_Databits = SerialPort.DATABITS_5;
break;
case 6:
m_Databits = SerialPort.DATABITS_6;
break;
case 7:
m_Databits = SerialPort.DATABITS_7;
break;
case 8:
m_Databits = SerialPort.DATABITS_8;
break;
default:
m_Databits = SerialPort.DATABITS_8;
break;
}
}
示例4: setStopbits
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Sets the number of stop bits.
*
* @param stopbits the new number of stop bits setting.
*/
public void setStopbits(double stopbits) throws IllegalArgumentException {
if (!SerialParameterValidator.isStopbitsValid(stopbits)) {
throw new IllegalArgumentException("stopbit value '" +
stopbits + "' not valid");
}
if (stopbits == 1) {
m_Stopbits = SerialPort.STOPBITS_1;
} else if (stopbits == 1.5) {
m_Stopbits = SerialPort.STOPBITS_1_5;
} else if (stopbits == 2) {
m_Stopbits = SerialPort.STOPBITS_2;
} else {
m_Stopbits = SerialPort.STOPBITS_1;
}
}
示例5: setParity
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Sets the parity schema from the given
* <tt>String</tt>.
*
* @param parity the new parity schema as <tt>String</tt>.
*/
public void setParity(String parity) throws IllegalArgumentException {
parity = parity.toLowerCase();
int intParity = SerialPort.PARITY_NONE;
if (parity.equals("none") || parity.equals("n")) {
intParity = SerialPort.PARITY_NONE;
} else if (parity.equals("even") || parity.equals("e")) {
intParity = SerialPort.PARITY_EVEN;
} else if (parity.equals("odd") || parity.equals("o")) {
intParity = SerialPort.PARITY_ODD;
} else {
throw new IllegalArgumentException(
"unknown parity string '" + parity + "'");
}
setParity(intParity);
}
示例6: connect
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
public void connect() throws EpsonProjectorException {
try {
logger.debug("Open connection to serial port '{}'", serialPortName);
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.disableReceiveTimeout();
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
out.flush();
if (in.markSupported()) {
in.reset();
}
// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event loop
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
throw new EpsonProjectorException(e);
}
}
示例7: removeListener
import gnu.io.SerialPort; //导入依赖的package包/类
private void removeListener(SerialPort port) {
port.notifyOnRingIndicator( false );
port.notifyOnParityError( false );
port.notifyOnOverrunError( false );
port.notifyOnOutputEmpty( false );
port.notifyOnFramingError( false );
port.notifyOnDSR( false );
port.notifyOnDataAvailable( false );
port.notifyOnCTS( false );
port.notifyOnCarrierDetect( false );
port.notifyOnBreakInterrupt( false );
port.removeEventListener();
}
示例8: stop
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* <p>stop</p>
*/
public void stop() {
for( Pipe pipe : m_loggingPorts.values() )
{
pipe.stop();
}
m_loggingPorts.clear();
System.out.print( "Closing " + m_openPorts.size() + " open comm ports... ");
for( SerialPort port : m_openPorts.values() )
{
removeListener( port );
port.close();
}
m_openPorts.clear();
System.out.println("done.");
}
示例9: setDatabits
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Sets the number of data bits.
*
* @param databits the new number of data bits.
*/
public void setDatabits(int databits) throws IllegalArgumentException {
if (!SerialParameterValidator.isDataBitsValid(databits)) {
throw new IllegalArgumentException("Databit '" + databits + "' invalid");
}
switch (databits) {
case 5:
m_Databits = SerialPort.DATABITS_5;
break;
case 6:
m_Databits = SerialPort.DATABITS_6;
break;
case 7:
m_Databits = SerialPort.DATABITS_7;
break;
case 8:
m_Databits = SerialPort.DATABITS_8;
break;
default:
m_Databits = SerialPort.DATABITS_8;
break;
}
}
示例10: setStopbits
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Sets the number of stop bits.
*
* @param stopbits the new number of stop bits setting.
*/
public void setStopbits(double stopbits) throws IllegalArgumentException {
if (!SerialParameterValidator.isStopbitsValid(stopbits)) {
throw new IllegalArgumentException("stopbit value '" + stopbits + "' not valid");
}
if (stopbits == 1) {
m_Stopbits = SerialPort.STOPBITS_1;
} else if (stopbits == 1.5) {
m_Stopbits = SerialPort.STOPBITS_1_5;
} else if (stopbits == 2) {
m_Stopbits = SerialPort.STOPBITS_2;
} else {
m_Stopbits = SerialPort.STOPBITS_1;
}
}
示例11: setParity
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* Sets the parity schema from the given
* <tt>String</tt>.
*
* @param parity the new parity schema as <tt>String</tt>.
*/
public void setParity(String parity) throws IllegalArgumentException {
parity = parity.toLowerCase();
int intParity = SerialPort.PARITY_NONE;
if (parity.equals("none") || parity.equals("n")) {
intParity = SerialPort.PARITY_NONE;
} else if (parity.equals("even") || parity.equals("e")) {
intParity = SerialPort.PARITY_EVEN;
} else if (parity.equals("odd") || parity.equals("o")) {
intParity = SerialPort.PARITY_ODD;
} else {
throw new IllegalArgumentException("unknown parity string '" + parity + "'");
}
setParity(intParity);
}
示例12: create
import gnu.io.SerialPort; //导入依赖的package包/类
@Override
public CULConfig create(String deviceType, String deviceAddress, CULMode mode, Dictionary<String, ?> config)
throws ConfigurationException {
int baudRate = 9600;
final String configuredBaudRate = (String) config.get(KEY_BAUDRATE);
Integer tmpBaudRate = baudrateFromConfig(configuredBaudRate);
if (tmpBaudRate != null) {
baudRate = tmpBaudRate;
logger.info("Update config, {} = {}", KEY_BAUDRATE, baudRate);
}
int parityMode = SerialPort.PARITY_EVEN;
final String configuredParity = (String) config.get(KEY_PARITY);
Integer parsedParityNumber = parityFromConfig(configuredParity);
if (parsedParityNumber != null) {
parityMode = parsedParityNumber;
logger.info("Update config, {} = {} ({})", KEY_PARITY, convertParityModeToString(parityMode), parityMode);
}
return new CULSerialConfig(deviceType, deviceAddress, mode, baudRate, parityMode);
}
示例13: closePort
import gnu.io.SerialPort; //导入依赖的package包/类
/**
* 关闭串口
*
* @param serialport
* 待关闭的串口对象
*/
public static void closePort(SerialPort serialPort) {
if (serialPort != null) {
serialPort.close();
serialPort = null;
}
}
示例14: connect
import gnu.io.SerialPort; //导入依赖的package包/类
void connect(String destination) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
if (portIdentifier.isCurrentlyOwned()) {
log.warning("Error: Port for Dynamixel-Communication is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialReader = new SerialReader(in);
serialPort.addEventListener(serialReader);
serialPort.notifyOnDataAvailable(true);
connected = true;
log.info("Connected to Dynamixel!");
} else {
log.warning("Error: Cannot connect to Dynamixel!");
}
}
}
示例15: connect
import gnu.io.SerialPort; //导入依赖的package包/类
void connect(String destination) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(destination);
if (portIdentifier.isCurrentlyOwned()) {
log.warning("Error: Port for Pan-Tilt-Communication is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialPort.addEventListener(new SerialReader(in));
serialPort.notifyOnDataAvailable(true);
connected = true;
log.info("Connected to Pan-Tilt-Unit!");
} else {
log.warning("Error: Cannot connect to Pan-Tilt-Unit!");
}
}
}