[英]Unable to send message, read response from my serial port attached device in java
我有一个连接到 mi PC 和 RS 232 的秤,还有一个 USB 到 RS 232 转换器(如果有人知道的话,ATEN USB 到串行桥接器)。
我只想立即从比例中获得 wheight,但我需要发送一个 ASCII 'W'。
When the host requests weight data by sending an uppercase W, the scale will respond with the weight data or a status byte if the scale is in motion or an invalid state.
来自秤的手册。
所以我做了那个代码:
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”发送到秤,所以它抛出了一个异常:
Exception in thread "main" java.io.IOException: Underlying input stream returned zero bytes
我在做什么坏事?
编辑:
我的秤是奥豪斯 RV 系列。
大卫,那个秤有 4 种类型的协议(原因是仅重量模式(WO))。
我建议您将 Elpsa 用于 WO 模式。 从手册:
The spanish competitor Epelsa has developed a protocol for the communication between checkout scales [ only-weight scales ] and POS [ or PC ] which has become kind of standard in the spanish market for this type of connections, thereof our interest that our only-weight scales become compatible.
您可以使用 9600 波特、8 个数据位、1 个停止位、偶数位。 您可以更改此设置,但使用默认比例配置这些是最好的。 (如果你想使用其他人,你应该改变规模配置)
要使用此协议,请使用此框:
所以要接收重量,你应该发送一个 '$' 字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.