问题描述
我正在编写 NEMA 0183 聚合器。我在多个 COM 端口上有传入数据,它们将被合并并在单个端口上输出。我之前的问题收到了使用多线程来实现这一点的建议。我无法正确实施它。我没有在打印语句所在的 Main 中接收任何数据。
package nema0183_aggregator;
import java.util.arraydeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.ArrayList;
/**
* This class contains the main method. It acts as a connector between the
* varIoUs read and single write classes.
*
* @author Ian Van Schaick
*/
public class Nema0183_aggregator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
arraydeque<String> gpsSentences = new arraydeque<String>();
arraydeque<String> aisSentences = new arraydeque<String>();
arraydeque<String> headingSentences = new arraydeque<String>();
arraydeque<String> sounderSentences = new arraydeque<String>();
arraydeque<String> outputSentences = new arraydeque<String>();
RS232Control gps = new RS232Control("COM32",4800,true);
RS232Control ais = new RS232Control("COM35",34800,false);
RS232Control heading = new RS232Control("COM37",false);
RS232Control sounder = new RS232Control("COM39",false);
RS232Control output = new RS232Control("COM41",115200,false);
gps.run();
// gps.start();
while (true) {
System.out.println(gps.getLine());
// gpsSentences.add(gps.getLine());
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Nema0183_aggregator.class.getName()).log(Level.SEVERE,null,ex);
}
} //End of while
}
}
RS232Control.java 实现可运行并使用 JSCC 库。
package nema0183_aggregator;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import jssc.*; // Java Simple Serial Connector,the library that contains the serial methods
import static nema0183_aggregator.RS232Control.serialPort;
/**
*
* @author Ian Van Schaick
*/
public class RS232Control implements Runnable {
static SerialPort serialPort;
String portName;
static long portOpen;
StringBuilder message;
Boolean receivingMessage;
SerialPortReader reader;
volatile String readLine;
Boolean ackNowledge;
int baud;
boolean gpsData;
NEMADateUpdate gpsUpdate;
String linesep;
/**
*
* @param portNum
* @param portbaud
* @param gps
*/
public RS232Control(String portNum,int portbaud,boolean gps) {
gpsData = gps;
if (gpsData == true) {
gpsUpdate = new NEMADateUpdate ();
}
portName = portNum;
baud = portbaud;
serialPort = new SerialPort(portName);
message = new StringBuilder();
receivingMessage = false;
reader = new SerialPortReader();
readLine = "";
ackNowledge = false;
linesep = System.getProperty("line.separator");
openP();
}
/**
* Finds the serial port to be used,in Windows type COM1,for example In
* Linux,type /dev/pts/3 for example. The custom USB-RS232 device,using a
* MCP2200,is on /dev/ttyACM0/
* All serial ports may not be listed.
*
* @return The serial port name in String format,used to open and close the
* port
*/
protected ArrayList<String> findPort() {
// System.out.println("List of COM ports:");
String[] portNames = SerialPortList.getPortNames();
ArrayList<String> portList = new ArrayList<>();
for (String portName1 : portNames) {
// System.out.println(portName1);
portList.add(portName1);
}
// System.out.println(SerialPort.BAUdratE_9600 + "");
//
// System.out.println(SerialPort.BAUdratE_4800 + "");
//
// System.out.println(SerialPort.BAUdratE_38400 + "");
//
// System.out.println(SerialPort.BAUdratE_115200 + "");
//
// System.out.println("What COM port are you using?");
// System.out.println("Please type it in how it appears above.");
// Scanner sc = new Scanner(system.in);
// String port = "";
// if (sc.hasNext()) {
// port = sc.next();
// } else {
//
// }
return portList;
}
/**
* Checks if the serial port is connected
* @return Returns true if any of the serial ports found using getPortNames()
* matches the portName global variable (what ever the user types in when
* findPort() is called).
*/
protected boolean serialConnected () {
boolean connected = false;
String[] portNames = SerialPortList.getPortNames();
for (String portName1 : portNames) {
if (portName1.equals(portName) ) {
connected = true;
// System.out.println("Connected successfully to serial port: " + portName);
}
else {
// System.out.println("Can not connect to serial port: " + portName);
}
}
return connected;
}
protected void changePort (String portNum,boolean gps) {
close();
gpsData = gps;
if (gpsData == true) {
gpsUpdate = new NEMADateUpdate ();
}
portName = portNum;
baud = portbaud;
serialPort = new SerialPort(portName);
message = new StringBuilder();
receivingMessage = false;
reader = new SerialPortReader();
readLine = "";
ackNowledge = false;
linesep = System.getProperty("line.separator");
openP();
}
/**
* Opens a COM port at the specified settings (baudrate 8N1)
* Can throw an error opening the port
*/
private void openP() {
try {
serialPort.openPort();
serialPort.setParams(baud,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
int mask = SerialPort.MASK_RXCHAR;
serialPort.setEventsMask(mask);
serialPort.addEventListener(reader);
serialPort.setRTS(false);
serialPort.setDTR(false);
ackNowledge = true;
} catch (SerialPortException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
System.out.println("There is an error opening port т: " + ex);
}
}
/**
* Closes the serial port,can throw a SerialPortException error.
*
* @return
*/
private boolean close() {
boolean success = false;
try {
serialPort.closePort();
success = true;
} catch (SerialPortException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
}
return success;
}
/**
* Opens the serial port. Tries to read a string from the serial port.
* Closes the serial port.
*
* @return Returns the byte array read from the serial port.
*/
protected byte [] testRead() {
byte [] readArray = null;
ArrayList <byte []> readList = new ArrayList <byte []> ();
for (int i = 0; i < 100; i++) {
byte [] tempArray = null;
try {
readList.add(serialPort.readBytes(10) );
} catch (SerialPortException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
}
}
String line = new String (readArray);
System.out.println(line);
// if (gpsData == true) {
// readArray = gpsUpdate.dateUpdate(readArray);
// }
return readArray;
}
/**
* Opens the serial port. Tries to read a string from the serial port.
* Closes the serial port.
*
* @return Returns the byte array read from the serial port.
*/
protected String testRead2() {
String line = "";
ArrayList <String> readList = new ArrayList <String> ();
boolean lineFin = false;
for (int i = 0; i < 100 && (!lineFin); i++) {
try {
// byte [] tempArray = null;
// readList.add(serialPort.readBytes(8) );
line = line + serialPort.readString(20);
} catch (SerialPortException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
}
if (line.endsWith(linesep)) {
lineFin = true;
}
if (gpsData == true) {
line = gpsUpdate.dateUpdate(line);
}
}
readLine = line;
System.out.println("tesstRead2: " + readLine);
return line;
}
/**
* Writes the String message to the serial port
*
* @param message The string to write to the serial port
* @return Returns true if the write was successful
*/
protected boolean testWrite(String message) {
boolean success = false;
try {
// openP();
if ( (!message.isBlank() ) && message.startsWith("$") ) {
success = serialPort.writeString(message);
}
// serialPort.closePort();
} catch (SerialPortException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
}
return success;
}
/**
* Writes the String message to the serial port
*
* @param message The string to write to the serial port
* @return Returns true if the write was successful
*/
protected boolean testWrite(byte [] message) {
boolean success = false;
try {
// openP();
serialPort.writeBytes(message);
success = true;
// serialPort.closePort();
} catch (SerialPortException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
}
return success;
}
protected synchronized String getLine () {
// System.out.println("GetLine: " + readLine);
return readLine;
}
@Override
public void run() {
while (true) {
testRead2();
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(RS232Control.class.getName()).log(Level.SEVERE,ex);
}
}
}
// class getCurLine extends Thread {
// @Override
// public void run() {
// String line = readLine;
// System.out.println("GetLineClass: " + readLine);
// }
//}
} // End of RS232Control class
/**
* In this class must implement the method serialEvent,through it we learn
* about events that happened to our port. But we will not report on all events
* but only those that we put in the mask. In this case the arrival of the data
* and change the status lines CTS and DSR
*/
class SerialPortReader implements SerialPortEventListener {
/**
* Reads the data bit by bit from the serial port Can throw a
* SerialPortException error
*
* @param event
*/
@Override
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR() && event.getEventValue() == 10) {
try {
// String line = serialPort.readString(event.getEventValue());
byte buffer[] = serialPort.readBytes(10);
// ackNowledgeStr + ackNowledgeStr +
// System.out.println("serialEvent: " + line);
// if (line.contains((char) 0x6 + "")) {
// ackNowledge2 = true;
// System.out.println("AckNowledged");
//
// } else {
// ackNowledge2 = false;
// }
// System.out.println("Received response: " + readLine);
} catch (SerialPortException ex) {
System.out.println("Error in receiving string from COM-port: " + ex);
}
}
}
/**
* Prints out the message read from the serial port
*
* @param message
*/
protected void processMessage(String message) {
// System.out.println(message);
}
}
关于代码的任何问题,我应该能够回答。 gpsUpdate.dateUpdate(line) 返回具有正确日期的特定类型 NEMA 0183 句子的更新版本。它的代码没有问题。在此处查看我之前的问题:Java jssc read from multiple COM ports,simultaneously,write all data out on one port
我的第一个小目标是让它从一个 COM 端口读取并在另一个端口上输出。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)