在弹簧启动应用程序中,我使用serialPuntit librairie读取串行端口。应用程序中的任何地方访问对象
线程通过onApplicationEvent启动。
工作正常。
@Component
public class ScanRead implements Runnable {
Thread thrd;
boolean suspended;
boolean stopped;
String port = "/dev/ttyUSB0";
final protected static char[] hexArray = "ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
@Override
public void run() {
try {
while (true) {
StringBuilder sb = new StringBuilder();
SerialComManager scm = new SerialComManager();
long handle = scm.openComPort(port, true, false, true);
byte[] dataRead = {};
scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B9600, 0);
scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
long context = scm.createBlockingIOContext();
for (int x = 0; x < 8; x++) {
dataRead = scm.readBytesBlocking(handle, 1, context);
if (dataRead != null) {
sb.append(bytesToHex(dataRead));
}
}
System.out.println(sb.toString());
scm.unblockBlockingIOOperation(context);
scm.destroyBlockingIOContext(context);
scm.closeComPort(handle);
Thread.sleep(50);
synchronized (this) {
while (suspended) {
wait();
}
if (stopped) {
break;
}
}
}
} catch (InterruptedException exc) {
System.out.println(thrd.getName() + " interrupted.");
} catch (IOException ex) {
Logger.getLogger(ScanRead.class.getName()).log(Level.SEVERE, null, ex);
}
}
synchronized void stop() {
stopped = true;
suspended = false;
notify();
}
synchronized void suspend() {
suspended = true;
}
synchronized void resume() {
suspended = false;
notify();
}
}
@Component
public class ScanApplicationStartup implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
ScanRead sc = new ScanRead();
sc.run();
}
}
唯一的缺点,不能使用其他SerialComManager在另一个地方的应用程序在同一端口上阅读。
我搜索一种方法来初始化SerialComManager并在应用程序中的任何地方使用它。
任何建议。
创建一个bean添加getter方法并返回这个对象,bean默认情况下是prototype(每次都返回相同的实例),你可以在你的spring项目的任何地方使用它。 – amitmah