Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ul.commx
- import java.io.{InputStream, OutputStream}
- import collection.mutable.{ArrayBuffer}
- class SerialBase(name:String) {
- /// closes port
- def close = {}
- /// get/set RX timeout (milliseconds)
- def timeoutRX:Int = 0
- def timeoutRX_=(timeout:Int) = {}
- /// set port configuration
- def init(baud:Int, bits:Int, stops:Double, parity:String): SerialBase = this
- /// configure from string (in format: "baud=9600;bits=8;stops=2;parity=n;dtr=0;rts=1"
- def initStr(s:String) = {
- import scala.language.postfixOps
- initStrMap( s.toLowerCase.split(";").map( _.split("=")).map(c => (c(0), c(1))).toMap)
- }
- def initStrMap(m:Map[String,String]) = {
- m.foreach( i => setStr(i._1, i._2) )
- }
- /// set string parameter
- def setStr(k:String, v:String) = {
- try k match {
- case "baud" => baud = v.toInt
- case "bits" => bits = v.toInt
- case "stops" => stops = v.toDouble
- case "parity" => parity = v
- case "dtr" => DTR = List("true","1").contains(v.toLowerCase)
- case "rts" => RTS = List("true","1").contains(v.toLowerCase)
- } catch { case _:Throwable => }
- }
- /// get string parameter
- def getStr(k:String):String = {
- k match {
- case "baud" => baud.toString
- case "bits" => bits.toString
- case "stops" => stops.toString
- case "parity" => parity
- case "dtr" => DTR.toString
- case "rts" => RTS.toString
- case "dsr" => DSR.toString
- case "cts" => CTS.toString
- case "dcd" => DCD.toString
- case "ri" => RI.toString
- }
- }
- /// get/set baud rate
- def baud:Int = 0
- def baud_=(newBaud:Int) = {}
- /// get/set data bits
- def bits:Int = 0
- def bits_=(newBits:Int) = {}
- /// get/set stop bits
- def stops:Double = 0
- def stops_=(newStops:Double) = {}
- /// get/set parity
- def parity:String = "n"
- def parity_=(newParity:String) = {}
- /// get/set port signals
- def DTR:Boolean = false
- def DTR_=(state:Boolean) = {}
- def RTS:Boolean = false
- def RTS_=(state:Boolean) = {}
- def DSR:Boolean = false
- def CTS:Boolean = false
- def DCD:Boolean = false
- def RI:Boolean = false
- /// returns number of available bytes in RX buffer
- def availRX:Int = 0
- /// waits for n input bytes for t milliseconds
- /// returns true if n bytes received, false if timeout occured
- def availWait( n:Int, t:Int ):Boolean = {
- var to = t
- while ( availRX < n && to > 0 ) {
- Thread.sleep(1)
- to -= 1
- }
- availRX >= n
- }
- /// waits for n input bytes for t * 0.1 milliseconds
- /// returns Array of n received bytes, null if timeout occured
- def readWait( n:Int, t:Int ): Seq[Byte] = {
- if (availWait(n, t)) read(n) else Nil
- }
- /// read n bytes from RX buffer to array (available number if n == 0)
- def read(n:Int):Seq[Byte] = Nil
- def read:Seq[Byte] = read(availRX)
- /// write bytes
- def write(buf:Seq[Byte]):SerialBase = this
- /// write string
- def write(s:String, enc:String="ISO-8859-1"):SerialBase = write(s.getBytes(enc))
- /// flush RX/TX buffers
- def flushRX:SerialBase = this
- def flushTX:SerialBase = this
- def flush:SerialBase = { flushRX; flushTX; this }
- def break( duration:Int ) = {}
- }
- class SerialBaseMgr {
- /// returns list of available ports
- def ports:Seq[String] = Nil
- /// open port by name
- def open(name:String):SerialBase = null
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement