Closed Lighfer closed 6 years ago
Hi Huang,
firmata4j uses jSSC (Java Simple Serial Connector) for working with serial ports. It supports COM ports.
The stack trace says that the port is not open. In order to open the port, device.start()
method should be invoked before first device.sendMessage()
invokation.
I would improve your sample the following way:
IODevice device = new FirmataDevice("COM6");
device.addEventListener(new IODeviceEventListener() {
public void onStart(IOEvent event) {
System.out.println("Device is ready");
}
public void onStop(IOEvent event) {
System.out.println("Device has been stopped");
}
public void onPinChange(IOEvent event) {
Pin pin = event.getPin();
System.out.println(
String.format(
"Pin %d got a value of %d",
pin.getIndex(),
pin.getValue())
);
}
public void onMessageReceive(IOEvent event, String message) {
System.out.println(message);
}
});
device.start(); // initiate communication to the device
device.ensureInitializationIsDone(); // wait for initialization is done
device.sendMessage("aldjaklsdjaklsdjaklsdjklasd");
Thread.sleep(1000); // allowing some time for the device to respond
device.stop();
Please, let me know if this solves the issue.
Sorry for delayed responce, Oleg
It works! Than you @kurbatov !
I want to use this framework to communicate two computer via serial port, does it support?
I try to run follow demo:
But i got this error: