我正在写一个nema 0183聚合器。我有几个com端口上的传入数据,它们将被合并并在单个端口上输出。我之前的问题得到了一个使用多线程来实现这一点的建议。我很难正确地执行它。我没有在print语句所在的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", 4800, false);
RS232Control sounder = new RS232Control("COM39", 4800, 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库实现runnable和runnable。
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, int portbaud, 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, null, 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, null, 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, null, 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, null, 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, null, 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, null, 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, null, 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);
}
}
任何关于代码的问题,我都可以回答。dateupdate(line)返回特定类型的nema 0183语句的更新版本和正确的日期。代码没有问题。请看我前面的问题:javajssc从多个com端口读取数据,同时在一个端口上写出所有数据
我的第一个小目标是让它从一个com端口读取并在另一个com端口上输出。
暂无答案!
目前还没有任何答案,快来回答吧!