blob: d75d6f55187d079e43935363d750cd5e2194b09d (
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
|
package com.github.jodersky.ace
import com.github.jodersky.ace.protocol._
import jssc.SerialPort
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Try
class SafeSerial(port: SerialPort) { self =>
val physical = new PhysicalLayer(port)
val link = new LinkLayer
val transport = new TransportLayer
val application = new ReactiveLayer[Message, String] {
def receive(message: Message) = Console.println(message.data.map(_.toChar).mkString(""))
def write(s: String) = writeToLower(Message(s.map(_.toChar.toInt))).map(x => s)
}
def send(s: String) = application.write(s)
def close() = Try(port.closePort())
physical connect link connect transport connect application
physical.begin()
}
object SafeSerial {
def open(port: String, rate: Int) = Try {
val serialPort = new SerialPort(port);
serialPort.openPort()
serialPort.setParams(rate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE)
serialPort
} map (port => new SafeSerial(port))
}
|