Skip to content

Commit

Permalink
kurbatov#41: +JSerialCommTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertSandor committed Sep 1, 2020
1 parent ad5735d commit cd9cb59
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<artifactId>jssc</artifactId>
<version>${jssc.version}</version>
</dependency>
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.6.2</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/org/firmata4j/transport/JSerialCommTransport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Oleg Kurbatov (o.v.kurbatov@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.firmata4j.transport;

import java.io.IOException;

import org.firmata4j.Parser;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortDataListener;
import com.fazecast.jSerialComm.SerialPortEvent;

/**
* Implementation of {@link TransportInterface} based on {@link SerialPort}.
*
* @author Norbert Sándor
*/
public class JSerialCommTransport implements TransportInterface
{
private final SerialPort serialPort;

private Parser parser;

public JSerialCommTransport(String portDescriptor)
{
serialPort = SerialPort.getCommPort(portDescriptor);
}

@Override
public void start() throws IOException
{
if (!serialPort.isOpen())
{
if (serialPort.openPort())
{
serialPort.setComPortParameters(jssc.SerialPort.BAUDRATE_57600, jssc.SerialPort.DATABITS_8,
SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
serialPort.addDataListener(new SerialPortDataListener()
{
@Override
public void serialEvent(SerialPortEvent event)
{
if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_RECEIVED)
{
parser.parse(event.getReceivedData());
}
}

@Override
public int getListeningEvents()
{
return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
}
});
} else
{
throw new IOException("Cannot start firmata device: port=" + serialPort);
}
}
}

@Override
public void stop() throws IOException
{
if (serialPort.isOpen() && !serialPort.closePort())
{
throw new IOException("Cannot properly stop firmata device: port=" + serialPort);
}
}

@Override
public void write(byte[] bytes) throws IOException
{
serialPort.writeBytes(bytes, bytes.length);
}

@Override
public void setParser(Parser parser)
{
this.parser = parser;
}
}

0 comments on commit cd9cb59

Please sign in to comment.