Java jssc getInputStream()getOutputstream()
我正在使用jssc库通过串口与设备通信。 在标准java SerialComm库中,有两种方法getInputStream()和getOutputStream() 为什么我需要这个?我想根据示例实现Xmodem,Xmodem构造函数需要两个参数:Java jssc getInputStream()getOutputstream(),java,serial-port,jssc,xmodem,Java,Serial Port,Jssc,Xmodem,我正在使用jssc库通过串口与设备通信。 在标准java SerialComm库中,有两种方法getInputStream()和getOutputStream() 为什么我需要这个?我想根据示例实现Xmodem,Xmodem构造函数需要两个参数: public Xmodem(InputStream inputStream, OutputStream outputStream) { this.inputStream = inputStream; this.outputStre
public Xmodem(InputStream inputStream, OutputStream outputStream)
{
this.inputStream = inputStream;
this.outputStream = outputStream;
}
Xmodem xmodem = new Xmodem(serialPort.getInputStream(),serialPort.getOutputStream());
在jssc中没有这样的方法,但我想知道是否有其他方法?一种可能性是提供一个自定义的
PortInputStream
类,该类扩展InputStream
并实现jsscSerialPortEventListener
接口。此类从串行端口接收数据并将其存储在缓冲区中。它还提供了从缓冲区获取数据的read()
方法
private class PortInputStream extends InputStream implements SerialPortEventListener {
CircularBuffer buffer = new CircularBuffer(); //backed by a byte[]
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
// exception handling omitted
buffer.write(serialPort.readBytes(event.getEventValue()));
}
}
@Override
public int read() throws IOException {
int data = -1;
try {
data = buffer.read();
} catch (InterruptedException e) {
// exception handling
}
return data;
}
@Override
public int available() throws IOException {
return buffer.getLength();
}
类似地,您可以提供一个自定义的PortOutputStream
类,该类扩展OutputStream
并写入串行接口:
private class PortOutputStream extends OutputStream {
SerialPort serialPort = null;
public PortOutputStream(SerialPort port) {
serialPort = port;
}
@Override
public void write(int data) throws IOException,SerialPortException {
if (serialPort != null && serialPort.isOpened()) {
serialPort.writeByte((byte)(data & 0xFF));
} else {
// exception handling
}
// you may also override write(byte[], int, int)
}
在实例化Xmodem
对象之前,必须创建两个流:
// omitted serialPort initialization
InputStream pin = new PortInputStream();
serialPort.addEventListener(pin, SerialPort.MASK_RXCHAR);
OutPutStream pon = new PortOutputStream(serialPort);
Xmodem xmodem = new Xmodem(pin,pon);
一种可能性是提供一个自定义的
PortInputStream
类,该类扩展InputStream
并实现JSSCsSerialPortEventListener
接口。此类从串行端口接收数据并将其存储在缓冲区中。它还提供了从缓冲区获取数据的read()
方法
private class PortInputStream extends InputStream implements SerialPortEventListener {
CircularBuffer buffer = new CircularBuffer(); //backed by a byte[]
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() > 0) {
// exception handling omitted
buffer.write(serialPort.readBytes(event.getEventValue()));
}
}
@Override
public int read() throws IOException {
int data = -1;
try {
data = buffer.read();
} catch (InterruptedException e) {
// exception handling
}
return data;
}
@Override
public int available() throws IOException {
return buffer.getLength();
}
类似地,您可以提供一个自定义的PortOutputStream
类,该类扩展OutputStream
并写入串行接口:
private class PortOutputStream extends OutputStream {
SerialPort serialPort = null;
public PortOutputStream(SerialPort port) {
serialPort = port;
}
@Override
public void write(int data) throws IOException,SerialPortException {
if (serialPort != null && serialPort.isOpened()) {
serialPort.writeByte((byte)(data & 0xFF));
} else {
// exception handling
}
// you may also override write(byte[], int, int)
}
在实例化Xmodem
对象之前,必须创建两个流:
// omitted serialPort initialization
InputStream pin = new PortInputStream();
serialPort.addEventListener(pin, SerialPort.MASK_RXCHAR);
OutPutStream pon = new PortOutputStream(serialPort);
Xmodem xmodem = new Xmodem(pin,pon);