1
1
package org .codeoverflow .chatoverflow .requirement .service .serial
2
2
3
- import java .io .PrintStream
3
+ import java .io .{ InputStream , PrintStream }
4
4
5
5
import com .fazecast .jSerialComm .{SerialPort , SerialPortInvalidPortException }
6
6
import org .codeoverflow .chatoverflow .WithLogger
@@ -9,55 +9,46 @@ import org.codeoverflow.chatoverflow.connector.Connector
9
9
/**
10
10
* The serial connector allows to communicate with a device connected to the pcs serial port (like an Arduino)
11
11
*
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
13
13
*/
14
14
class SerialConnector (override val sourceIdentifier : String ) extends Connector (sourceIdentifier) with WithLogger {
15
15
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" )
17
18
18
19
private var serialPort : Option [SerialPort ] = None
19
20
private var out : Option [PrintStream ] = None
21
+ private var in : Option [InputStream ] = None
20
22
private val serialPortInputListener = new SerialPortInputListener
21
23
22
24
/**
23
- * Sets the baud rate of the com port to a new value
24
- *
25
- * @param baudRate the new baud rate
26
25
* @throws java.lang.IllegalStateException if the serial port is not available yet
26
+ * @return print stream that outputs to the port
27
27
*/
28
28
@ 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
43
32
}
44
33
45
34
/**
46
- *
47
35
* @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
49
37
*/
50
38
@ throws(classOf [IllegalStateException ])
51
- def getPrintStream : PrintStream = {
39
+ def getInputStream : InputStream = {
52
40
if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
53
- out .get
41
+ in .get
54
42
}
55
43
56
44
/**
57
45
* Adds a new input listener that receives all data
58
46
* @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
59
48
*/
49
+ @ throws(classOf [IllegalStateException ])
60
50
def addInputListener (listener : Array [Byte ] => Unit ): Unit = {
51
+ if (serialPort.isEmpty) throw new IllegalStateException (" Serial port is not available yet" )
61
52
serialPortInputListener.addDataAvailableListener(_ => {
62
53
val buffer = new Array [Byte ](serialPort.get.bytesAvailable())
63
54
serialPort.get.readBytes(buffer, buffer.length) // FIXME DOES IT CRASH?
@@ -69,12 +60,24 @@ class SerialConnector(override val sourceIdentifier: String) extends Connector(s
69
60
* Opens a connection with the serial port
70
61
*/
71
62
override def start (): Boolean = {
63
+ // TODO Test if connector is working this way or if it requires an actor
72
64
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... "
74
74
if (serialPort.get.openPort(1000 )) {
75
+ Thread .sleep(1500 )// Sleep to wait for
75
76
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)
77
79
serialPort.get.addDataListener(serialPortInputListener)
80
+ logger info " Opened serial port!"
78
81
true
79
82
} else {
80
83
logger error s " Could not open serial port $sourceIdentifier"
0 commit comments