How to Send Data to Com Port Using Java

Sending a string to a COM port

Take a look at this link.

It will give more insight in how to send data via COM port

How to send data to COM PORT using JAVA?

this page links to some of the possible answers

Java Serial Communication on Windows

Is there Java library or framework for accessing Serial ports?

Reading serial port in Java

and two other answers which are basically equal as the reading serial ports answer.

private TwoWaySerialComm twoWaySerCom;

public AlarmGenerator() {

initComponents();

twoWaySerCom = new TwoWaySerialComm();

try {

twoWaySerCom.connect("COM1");
}
catch (Exception e) {

e.printStackTrace();
}
}

I noticed you have multiple

 public static void Main(string[] args)

in your project those are project entries and one should be sufficient.

How to send data to COM PORT using JAVA?

This question has been asked and answered many times:

Read file from serial port using Java

Reading serial port in Java

Reading file from serial port in Java

Is there Java library or framework for accessing Serial ports?

Java Serial Communication on Windows

to reference a few.

Personally I recommend SerialPort from http://serialio.com - it's not free, but it's well worth the developer (no royalties) licensing fee for any commercial project. Sadly, it is no longer royalty free to deploy, and SerialIO.com seems to have remade themselves as a hardware seller; I had to search for information on SerialPort.

From personal experience, I strongly recommend against the Sun, IBM and RxTx implementations, all of which were unstable in 24/7 use. Refer to my answers on some of the aforementioned questions for details. To be perfectly fair, RxTx may have come a long way since I tried it, though the Sun and IBM implementations were essentially abandoned, even back then.

A newer free option that looks promising and may be worth trying is jSSC (Java Simple Serial Connector), as suggested by @Jodes comment.

Sending data by serial port

This issue was related to the fact that the serial port that my application was trying to access was of type RS232 and this type of serial port will only allow on thread to access the port and there by I could not see the logs along with out to the display.
Please note this is not a solution it is a reason

Send data received on serial port to JavaFX controller

It's not really clear what you're asking: you already have all the pieces.

I'm not familiar with jSerialComm (in particular how it manages threading), but your JavaFX application will look like this:

public class MyApp extends Application {
private SerialPort comPort;

public static void main(String[] args) {
Application.launch(args);
}

@Override
public void init() {
comPort = SerialPort.getCommPorts()[0];

comPort.setBaudRate(1200);
comPort.openPort();
}

@Override
public void start(Stage stage) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/fxml/file"));
Parent root = loader.load();
MyControllerClass controller = loader.getController();

SerialPortMessageListener listener = new SerialPortMessageListener() {
@Override
public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_RECEIVED; }

@Override
public byte[] getMessageDelimiter() { return new byte[] { (byte)0x03}; }

@Override
public boolean delimiterIndicatesEndOfMessage() { return true; }

@Override
public void serialEvent(SerialPortEvent event) {
byte[] delimitedMessage = event.getReceivedData();
Platform.runLater(() -> controller.processData(new String(delimitedMessage)));
}
}

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}

@Override
public void stop() {
// shut down comPort
}
}

and

public class MyControllerClass {
@FXML
private TextArea textArea;

public void processData(String data) {
textArea.appendText(data);
}

// other controller stuff
}


Related Topics



Leave a reply



Submit