From 77db2136559ccef7d84cf6c0fd0166a970224680 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 26 Feb 2013 17:14:53 +0100 Subject: restructure scala directory in view of providing seperate projects for serial implementations --- .../scala/com/github/jodersky/ace/SafeSerial.scala | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 scala/ace/src/main/scala/com/github/jodersky/ace/SafeSerial.scala (limited to 'scala/ace/src/main/scala/com/github/jodersky/ace/SafeSerial.scala') diff --git a/scala/ace/src/main/scala/com/github/jodersky/ace/SafeSerial.scala b/scala/ace/src/main/scala/com/github/jodersky/ace/SafeSerial.scala new file mode 100644 index 0000000..d75d6f5 --- /dev/null +++ b/scala/ace/src/main/scala/com/github/jodersky/ace/SafeSerial.scala @@ -0,0 +1,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)) +} \ No newline at end of file -- cgit v1.2.3