blob: 3bfd650ea6809335b2933a008e425c831361f97a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.github.jodersky.ace.jssc
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import com.github.jodersky.ace.serial._
import com.github.jodersky.ace.serial.Serial
import jssc.SerialPortEvent
import jssc.SerialPort
import jssc.SerialPortEventListener
import java.io.IOException
object JSSCSerialProvider extends SerialProvider {
def open(port: String, baudRate: Int) = new Serial {
val serialPort = new SerialPort(port);
serialPort.openPort()
serialPort.setParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE)
val listener = new SerialPortEventListener {
override def serialEvent(event: SerialPortEvent) = {
if (event.isRXCHAR()) {
val bytes = serialPort.readBytes
if (bytes != null) notifyHigher(bytes.map(_ & 0xff))
}
}
}
def send(data: Seq[Int]) = future {
serialPort.writeBytes(data.toArray.map(_.toByte))
} map { success =>
if (success) data
else throw new IOException("Could not write to serial port.")
}
def close() = serialPort.closePort()
def begin() = {
val mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
serialPort.setEventsMask(mask)
serialPort.addEventListener(listener)
}
}
}
|