11package org .codeoverflow .chatoverflow .requirement .service .serial
22
3- import java .io .PrintStream
3+ import java .io .{ InputStream , PrintStream }
44
55import com .fazecast .jSerialComm .{SerialPort , SerialPortInvalidPortException }
66import org .codeoverflow .chatoverflow .WithLogger
@@ -9,55 +9,46 @@ import org.codeoverflow.chatoverflow.connector.Connector
99/**
1010 * The serial connector allows to communicate with a device connected to the pcs serial port (like an Arduino)
1111 *
12- * @param sourceIdentifier the port descriptor of the serial port to which the device is connected
12+ * @param sourceIdentifier r the unique source identifier to identify this connector
1313 */
1414class SerialConnector (override val sourceIdentifier : String ) extends Connector (sourceIdentifier) with WithLogger {
1515
16- override protected var requiredCredentialKeys : List [String ] = List ()
16+ override protected var optionalCredentialKeys : List [String ] = List (" baudRate" )
17+ override protected var requiredCredentialKeys : List [String ] = List (" port" )
1718
1819 private var serialPort : Option [SerialPort ] = None
1920 private var out : Option [PrintStream ] = None
21+ private var in : Option [InputStream ] = None
2022 private val serialPortInputListener = new SerialPortInputListener
2123
2224 /**
23- * Sets the baud rate of the com port to a new value
24- *
25- * @param baudRate the new baud rate
2625 * @throws java.lang.IllegalStateException if the serial port is not available yet
26+ * @return print stream that outputs to the port
2727 */
2828 @ throws(classOf [IllegalStateException ])
29- def setBaudRate (baudRate : Int ): Unit = {
30- if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
31- serialPort.get.setBaudRate(baudRate)
32- }
33-
34- /**
35- *
36- * @throws java.lang.IllegalStateException if the serial port is not available yet
37- * @return the baud rate of the com port
38- */
39- @ throws(classOf [IllegalStateException ])
40- def getBaudRate : Int = {
41- if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
42- serialPort.get.getBaudRate
29+ def getPrintStream : PrintStream = {
30+ if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
31+ out.get
4332 }
4433
4534 /**
46- *
4735 * @throws java.lang.IllegalStateException if the serial port is not available yet
48- * @return print stream that outputs to the port
36+ * @return a inputstream that receives all data from the port
4937 */
5038 @ throws(classOf [IllegalStateException ])
51- def getPrintStream : PrintStream = {
39+ def getInputStream : InputStream = {
5240 if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
53- out .get
41+ in .get
5442 }
5543
5644 /**
5745 * Adds a new input listener that receives all data
5846 * @param listener a listener that handles incoming data in a byte array
47+ * @throws java.lang.IllegalStateException if the serial port is not available yet
5948 */
49+ @ throws(classOf [IllegalStateException ])
6050 def addInputListener (listener : Array [Byte ] => Unit ): Unit = {
51+ if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
6152 serialPortInputListener.addDataAvailableListener(_ => {
6253 val buffer = new Array [Byte ](serialPort.get.bytesAvailable())
6354 serialPort.get.readBytes(buffer, buffer.length) // FIXME DOES IT CRASH?
@@ -69,12 +60,24 @@ class SerialConnector(override val sourceIdentifier: String) extends Connector(s
6960 * Opens a connection with the serial port
7061 */
7162 override def start (): Boolean = {
63+ // TODO Test if connector is working this way or if it requires an actor
7264 try {
73- serialPort = Some (SerialPort .getCommPort(sourceIdentifier))
65+ serialPort = Some (SerialPort .getCommPort(credentials.get.getValue(" port" ).get))
66+ credentials.get.getValue(" baudRate" ) match {
67+ case Some (baudRate) if baudRate.matches(" \\ s*\\ d+\\ s*" ) => serialPort.get.setBaudRate(baudRate.trim.toInt)
68+ case Some (ivalidBaudrate) =>
69+ logger error s " Invalid baud rate: $ivalidBaudrate"
70+ return false
71+ case None => // Do nothing
72+ }
73+ logger info s " Waiting for serial port to open... "
7474 if (serialPort.get.openPort(1000 )) {
75+ Thread .sleep(1500 )// Sleep to wait for
7576 serialPort.get.setComPortTimeouts(SerialPort .TIMEOUT_READ_SEMI_BLOCKING , 0 , 0 )
76- out = Some (new PrintStream (serialPort.get.getOutputStream))
77+ out = Some (new PrintStream (serialPort.get.getOutputStream, true , " US-ASCII" ))
78+ in = Some (serialPort.get.getInputStream)
7779 serialPort.get.addDataListener(serialPortInputListener)
80+ logger info " Opened serial port!"
7881 true
7982 } else {
8083 logger error s " Could not open serial port $sourceIdentifier"
0 commit comments