- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 jssc 库通过串行端口与设备进行通信。在标准 java SerialComm 库中有两个方法 getInputStream() 和 getOutputStream()。
为什么我需要这个?我想按照this实现Xmodem示例和 xmodem 构造函数需要两个参数:
public Xmodem(InputStream inputStream, OutputStream outputStream)
{
this.inputStream = inputStream;
this.outputStream = outputStream;
}
Xmodem xmodem = new Xmodem(serialPort.getInputStream(),serialPort.getOutputStream());
在 jssc 中没有这样的方法,但我想知道是否有其他方法?
最佳答案
一种可能性是提供一个自定义的PortInputStream
类,该类扩展InputStream
并实现JSSC的SerialPortEventListener
接口(interface)。该类从串行端口接收数据并将其存储在缓冲区中。它还提供了一个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
并写入串行接口(interface):
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);
关于java - jssc getInputStream() getOutputstream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42341530/
我的 Controller 中有这个方法。 Whick 基本上呈现 pdf 文件。 def getUserInvoiceImage (GetInvoiceDataCommand form) {
我用谷歌搜索错误消息 getOutputStream() has already been called for this response很多人说这是因为 ,但在我的代码中,没有空格或换行符。我在
我用谷歌搜索错误消息 getOutputStream() has already been called for this response很多人说这是因为 ,但在我的代码中,没有空格或换行符。我在
这个问题已经有答案了: java.lang.IllegalStateException: getOutputStream() has already been called for this resp
我正在使用 Spring 开发一个项目,我遇到了这个问题,我用谷歌搜索了错误消息,我确实找到了解决方案,甚至所有关于这个问题的 SO 帖子也是如此 java.lang.IllegalStateExce
你好,当我尝试在程序的连接方法中使用这两行代码时,出现错误“找不到符号方法 getOutputStream()” 我不知道我做错了什么,这是代码 socket = new ServerSocket(6
我用谷歌搜索错误信息 getOutputStream() has already been called for this response很多人说是因为 ,但在我的代码中,没有空格或换行符。我在li
我正在编写一个 java 代码来将 POST 请求发送到 URL,但它卡在粗体指示的行上。我在 Android 模拟器上运行这个 URL url = new URL("htt HttpURLConne
来自此链接“Writing to the OutputStream of Java Process/ProcessBuilder as pipe”并进行研究,我没有找到指定从 getOutputStr
我正在使用 jssc 库通过串行端口与设备进行通信。在标准 java SerialComm 库中有两个方法 getInputStream() 和 getOutputStream()。 为什么我需要这个
我正在尝试构建将数据发布到node.js服务器的java应用程序。 在 Node.js 中处理帖子的代码如下: app.post('/login.html', function (req, res)
我有一个想要测试的servlet。我想测试 Mockito 的 doGet。在来自 srvlet 的主代码信息中,通过 PrintWriter 转到页面/json。在我的测试中,我想要一个包含测试信息
我正在为我们的 Android 设备创建一个应用程序。本节的目的是将用户名和密码(当前仅分配为字符串)发布到 Web 服务并接收登录 token 。当运行代码时,在 getOutputStream()
我正在尝试从 MySQL-DB 下载 pdf(我将其作为 Blob 获取,到目前为止一切正常。但是当我尝试让 ServletOutputstream 将其发送到客户端时,程序崩溃。 AFAIK 当该方
所以我从 GUI 启动 Bukkit (Minecraft) 服务器。 ProcessBuilder builder = new ProcessBuilder(); builder.redirectE
我在 Windows 机器上使用 gzip 实用程序。我压缩了一个文件并作为 blob 存储在数据库中。当我想使用 gzip 实用程序解压缩此文件时,我将此字节流写入 process.getOutpu
我知道还有许多其他帖子处理同样的错误,但所有这些帖子要么是关于 JSP/GSP 页面的,要么是出于任何其他原因,对我的情况不是很有帮助。我将 Spring MVC 与 Thymeleaf 一起使用。以
一个问题 以为例 DataOutputStream output= new DataOutputStream(clientSocket.getOutputStream()) ; 或 DataInput
我正在使用 Spring @RestController 并以 Json 格式发送响应。这工作正常,但我需要向记录器发送响应,所以我已经实现了 Spring 的 HandlerInterceptor
Socket socket = sslSocketFactory.createSocket(); socket.connect(new InetSocketAddress(hostname, port
我是一名优秀的程序员,十分优秀!