java - 无法发送消息,无法从我的串行端口连接设备中读取响应

标签 java serial-port

我有一个秤,连接到 mi PC 和 RS 232,并带有 USB 到 RS 232 转换器(ATEN USB 到串行桥,如果有人知道的话)。

我只想在某一时刻从秤上获取体重,但我需要发送一个 ASCII 'W'。

当主机通过发送大写W请求重量数据时,如果秤处于运动或无效状态,则秤将响应重量数据或状态字节。

摘自体重秤手册。

所以我执行了该代码:

import gnu.io.*;
import java.io.*;
import java.util.Enumeration;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;


public class PruebaMia implements SerialPortEventListener{

    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    String output = "W/r/t";


public static void main(String[] args) throws IOException, UnsupportedCommOperationException {
    PruebaMia pm = new PruebaMia();
    pm.FlashWriteMethod();
}

public void FlashWriteMethod() throws IOException, UnsupportedCommOperationException {
    portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM4")) {
                try {
                    serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);

                BufferedReader is = null;  // for demo purposes only. A stream would be more typical.
                PrintStream    os = null;
                try {
            serialPort.addEventListener(this);
            } catch (TooManyListenersException e) {
                System.out.println("Tooo many Listener exception");}

                try {
                    is = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                    } catch (IOException e) {
                    System.err.println("Can't open input stream: write-only");
                    is = null;
                }

                os = new PrintStream(serialPort.getOutputStream(), true);

                os.print("W");
                os.print("\r\n");


                // Read the response
                String response = is.readLine();
                OutputStream mOutputToPort = serialPort.getOutputStream();
                inputStream = serialPort.getInputStream();
                System.out.println(" Input Stream... " + inputStream);

                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(mOutputToPort));
                bw.write(output);
                bw.flush();

                inputStream = serialPort.getInputStream();
                System.out.println(" Input Stream... " + inputStream);
                }
             }
        }
}

 public void serialEvent(SerialPortEvent event){
        switch(event.getEventType()) {
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                outputBufferEmpty(event);
                break;

            case SerialPortEvent.DATA_AVAILABLE:
                dataAvailable(event);
                break;
        }
    }

    protected void outputBufferEmpty(SerialPortEvent event) {
    }

    protected void dataAvailable(SerialPortEvent event) {
        System.out.println("Data available event received");
//        try{
//            while (inputStream.available() > 0){
//                int numBytes = inputStream.read(readBuffer);}
//            
//        vBuffer += new String(readBuffer);
//        System.out.print(new String(readBuffer));
//        
//        }catch (IOException e){
//            System.out.println(e);}
//    }
    }

}

我认为我没有很好地将“W”发送到秤,因此它会引发异常:

线程“main”java.io.IOException中出现异常:底层输入流返回零字节

我做错了什么?

编辑:

我的体重秤是奥豪斯 RV 系列。

最佳答案

David,该秤有 4 种类型的协议(protocol)(原因是仅重量模式 (WO))。

我建议您使用 Elpsa 进行 WO 模式。来自手册:

西类牙竞争对手 Epelsa 开发了一种用于结帐秤 [ 仅重量秤 ] 和 POS [ 或 PC ] 之间通信的协议(protocol),该协议(protocol)已成为西类牙市场此类连接的标准,其中我们的希望我们的唯一体重秤能够兼容。

您可以使用 9600 波特率、8 个数据位、1 个停止位、偶数。 您可以更改此设置,但使用默认比例配置时,这些是最好的。 (如果您想使用其他的,您应该更改秤配置)

要使用此协议(protocol),请使用此框:

Scale box

因此,要接收重量,您应该发送“$”字符

关于java - 无法发送消息,无法从我的串行端口连接设备中读取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22632796/

相关文章:

java - JPanel 在没有 GridLayout 的情况下组织按钮/图像/等

java - 重构模式匹配序列

java - Jersey 中属性名称定义冲突,使用 Jackson 2.9.6

Arduino返回的PHP串口数据

c# - 串行端口和 BSOD

java - 打印树中所有节点的方法

Javax 内容无法访问

python - 带有pyserial(python串口)的二进制数据

linux - Socat Raspberry Pi 串口转 TCP/IP 问题

Python监控串口(RS-232)握手信号