Advertisement
edartuz

scala_serial_base

Mar 23rd, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.72 KB | None | 0 0
  1. package ul.commx
  2.  
  3. import java.io.{InputStream, OutputStream}
  4. import collection.mutable.{ArrayBuffer}
  5.  
  6. class SerialBase(name:String) {
  7.     /// closes port
  8.     def close = {}
  9.    
  10.     /// get/set RX timeout (milliseconds)
  11.     def timeoutRX:Int = 0
  12.     def timeoutRX_=(timeout:Int) = {}
  13.    
  14.     /// set port configuration
  15.     def init(baud:Int, bits:Int, stops:Double, parity:String): SerialBase = this
  16.     /// configure from string (in format: "baud=9600;bits=8;stops=2;parity=n;dtr=0;rts=1"
  17.     def initStr(s:String) = {
  18.         import scala.language.postfixOps
  19.         initStrMap( s.toLowerCase.split(";").map( _.split("=")).map(c => (c(0), c(1))).toMap)
  20.     }
  21.     def initStrMap(m:Map[String,String]) = {
  22.         m.foreach( i => setStr(i._1, i._2) )
  23.     }
  24.    
  25.     /// set string parameter
  26.     def setStr(k:String, v:String) = {
  27.         try k match {
  28.             case "baud"   => baud = v.toInt
  29.             case "bits"   => bits = v.toInt
  30.             case "stops"  => stops = v.toDouble
  31.             case "parity" => parity = v
  32.             case "dtr"    => DTR = List("true","1").contains(v.toLowerCase)
  33.             case "rts"    => RTS = List("true","1").contains(v.toLowerCase)
  34.         } catch { case _:Throwable => }
  35.     }
  36.     /// get string parameter
  37.     def getStr(k:String):String = {
  38.         k match {
  39.             case "baud"   => baud.toString
  40.             case "bits"   => bits.toString
  41.             case "stops"  => stops.toString
  42.             case "parity" => parity
  43.             case "dtr"    => DTR.toString
  44.             case "rts"    => RTS.toString
  45.             case "dsr"    => DSR.toString
  46.             case "cts"    => CTS.toString
  47.             case "dcd"    => DCD.toString
  48.             case "ri"     => RI.toString
  49.         }
  50.     }
  51.    
  52.     /// get/set baud rate
  53.     def baud:Int = 0
  54.     def baud_=(newBaud:Int) = {}
  55.    
  56.     /// get/set data bits
  57.     def bits:Int = 0
  58.     def bits_=(newBits:Int) = {}
  59.  
  60.     /// get/set stop bits
  61.     def stops:Double = 0
  62.     def stops_=(newStops:Double) = {}
  63.  
  64.     /// get/set parity
  65.     def parity:String = "n"
  66.     def parity_=(newParity:String) = {}
  67.    
  68.     /// get/set port signals
  69.     def DTR:Boolean = false
  70.     def DTR_=(state:Boolean) = {}
  71.     def RTS:Boolean = false
  72.     def RTS_=(state:Boolean) = {}
  73.     def DSR:Boolean = false
  74.     def CTS:Boolean = false
  75.     def DCD:Boolean = false
  76.     def RI:Boolean  = false
  77.  
  78.     /// returns number of available bytes in RX buffer
  79.     def availRX:Int = 0
  80.    
  81.     /// waits for n input bytes for t milliseconds
  82.     /// returns true if n bytes received, false if timeout occured
  83.     def availWait( n:Int, t:Int ):Boolean = {
  84.         var to = t
  85.         while ( availRX < n && to > 0 ) {
  86.             Thread.sleep(1)
  87.             to -= 1
  88.         }
  89.         availRX >= n
  90.     }
  91.    
  92.     /// waits for n input bytes for t * 0.1 milliseconds
  93.     /// returns Array of n received bytes, null if timeout occured
  94.     def readWait( n:Int, t:Int ): Seq[Byte] = {
  95.         if (availWait(n, t)) read(n) else Nil
  96.     }
  97.    
  98.     /// read n bytes from RX buffer to array (available number if n == 0)
  99.     def read(n:Int):Seq[Byte] = Nil
  100.     def read:Seq[Byte] = read(availRX)
  101.  
  102.     /// write bytes
  103.     def write(buf:Seq[Byte]):SerialBase = this
  104.     /// write string
  105.     def write(s:String, enc:String="ISO-8859-1"):SerialBase = write(s.getBytes(enc))
  106.    
  107.     /// flush RX/TX buffers
  108.     def flushRX:SerialBase = this
  109.     def flushTX:SerialBase = this
  110.     def flush:SerialBase = { flushRX; flushTX; this }
  111.  
  112.     def break( duration:Int ) = {}
  113. }
  114.  
  115. class SerialBaseMgr {
  116.     /// returns list of available ports
  117.     def ports:Seq[String] = Nil
  118.    
  119.     /// open port by name
  120.     def open(name:String):SerialBase = null
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement