称重终端中Java串口的读取
我在某个地方陷入了从COM端口分配读取输入和分配字节缓冲区类的困境。即使地磅上有一些重量,我也总是得到0的输出 以下行可以使用一些不需要的ascii代码打印重量: 字符串结果=新字符串(readbuffer,0,numBytes) 我正在使用字节缓冲区类将这个ascii值转换为有用的输出。但这只给出了0作为输出称重终端中Java串口的读取,java,serial-port,Java,Serial Port,我在某个地方陷入了从COM端口分配读取输入和分配字节缓冲区类的困境。即使地磅上有一些重量,我也总是得到0的输出 以下行可以使用一些不需要的ascii代码打印重量: 字符串结果=新字符串(readbuffer,0,numBytes) 我正在使用字节缓冲区类将这个ascii值转换为有用的输出。但这只给出了0作为输出 import java.io.*; import java.util.Enumeration; import java.nio.*; import javax.comm.CommPor
import java.io.*;
import java.util.Enumeration;
import java.nio.*;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import com.sap.conn.jco.*;
public class ReadingPorts implements SerialPortEventListener,Runnable {
static CommPortIdentifier portId;
static Enumeration portList;
static SerialPort port;
static InputStream inputStream;
static Thread readThread;
static byte readbuffer[];
public static void main(String[] args) {
// TODO Auto-generated method stub
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
if (!portId.isCurrentlyOwned()) {
ReadingPorts rp = new ReadingPorts();
} else {
System.out.println("This port is already used by some other program");
}
}
}
}
}
public ReadingPorts() {
try {
port = (SerialPort) portId.open("Custom", 500);
inputStream = port.getInputStream();
System.out.println("** Connected To COM3 **");
port.addEventListener(this);
port.notifyOnDataAvailable(true);
port.setSerialPortParams(2400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
port.enableReceiveTimeout(500);
System.out.println("................................");
readThread = new Thread(this);
readThread.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
readbuffer = new byte[500];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readbuffer);
String result = new String(readbuffer,0,numBytes);
}
ByteBuffer buffer = ByteBuffer.wrap(readbuffer); //wrap received byte buffer
buffer.order(ByteOrder.LITTLE_ENDIAN);//set correct endiannes
int version = buffer.getInt();// read first 4 bytes
int length = buffer.getInt(); // read next 4 bytes
byte[] rowMsg = new byte[length];// length = bytes to read
buffer.get(rowMsg); // copies 'length' bytes in rowMsg
String msg = new String(rowMsg); // convert to String
System.out.println("Version "+version+" message received");
System.out.println("Length: "+length);
System.out.println("text: "+msg);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void run() {
try {
System.out.println("In Run");
Thread.sleep(2000);
} catch (Exception ex) {
ex.printStackTrace();;
}
}
}