我有一个连接到mi PC到RS 232和一个usb到RS 232转换器的秤(如果有人知道的话,ATEN USB到串行桥)。
我只想从刻度中获得一刻,但我需要发送一个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
我做得不好?
编辑:
我的体重是OHAUS RV系列。
答案 0 :(得分:2)
大卫,那个量表有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,偶数部分。 您可以更改此设置,但使用默认比例配置时,这些是最好的。 (如果要使用其他人,则应更改比例配置)
要使用此协议,请使用此框:
因此,为了获得体重,您应该发送'$'字符