Advertisement
edartuz

scala_serial_pjc

Mar 23rd, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.95 KB | None | 0 0
  1. package ul.commx
  2.  
  3. import purejavacomm.{CommPort, CommPortIdentifier, SerialPort}
  4. import collection.JavaConversions._
  5.  
  6. class SerialPJC(name:String) extends SerialBase(name) {
  7.     val port = CommPortIdentifier.getPortIdentifier(name).open("SerialPJC", 100).asInstanceOf[SerialPort]
  8.     port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE)
  9.     val portInStream = port.getInputStream
  10.     val portOutStream = port.getOutputStream
  11.    
  12.     override def close = port.close
  13.    
  14.     override def baud = port.getBaudRate
  15.     override def baud_=(newBaud:Int) = port.setSerialPortParams(newBaud, port.getDataBits, port.getStopBits, port.getParity)
  16.    
  17.     override def bits:Int = port.getDataBits match {
  18.         case SerialPort.DATABITS_5 => 5
  19.         case SerialPort.DATABITS_6 => 6
  20.         case SerialPort.DATABITS_7 => 7
  21.         case SerialPort.DATABITS_8 => 8
  22.     }
  23.     override def bits_=(b:Int) = port.setSerialPortParams( port.getBaudRate, b match {
  24.         case 5 => SerialPort.DATABITS_5
  25.         case 6 => SerialPort.DATABITS_6
  26.         case 7 => SerialPort.DATABITS_7
  27.         case 8 => SerialPort.DATABITS_8
  28.     }, port.getStopBits, port.getParity)
  29.  
  30.     override def stops:Double = port.getStopBits match {
  31.         case SerialPort.STOPBITS_1   => 1.0
  32.         case SerialPort.STOPBITS_1_5 => 1.5
  33.         case SerialPort.STOPBITS_2   => 2.0
  34.     }
  35.     override def stops_=(s:Double) = port.setSerialPortParams( port.getBaudRate, port.getDataBits, s match {
  36.         case 1.0 => SerialPort.STOPBITS_1
  37.         case 1.5 => SerialPort.STOPBITS_1_5
  38.         case 2.0 => SerialPort.STOPBITS_2
  39.     }, port.getParity)
  40.  
  41.     override def parity:String = port.getParity match {
  42.         case SerialPort.PARITY_NONE  => "n"
  43.         case SerialPort.PARITY_ODD   => "o"
  44.         case SerialPort.PARITY_EVEN  => "e"
  45.         case SerialPort.PARITY_MARK  => "m"
  46.         case SerialPort.PARITY_SPACE => "s"
  47.     }
  48.     override def parity_=(p:String) = port.setSerialPortParams( port.getBaudRate, port.getDataBits, port.getStopBits, p match {
  49.         case "n" => SerialPort.PARITY_NONE
  50.         case "o" => SerialPort.PARITY_ODD
  51.         case "e" => SerialPort.PARITY_EVEN
  52.         case "m" => SerialPort.PARITY_MARK
  53.         case "s" => SerialPort.PARITY_SPACE
  54.     })
  55.    
  56.     override def DTR:Boolean = port.isDTR
  57.     override def DTR_=(state:Boolean) = port.setDTR(state)
  58.     override def RTS:Boolean = port.isRTS
  59.     override def RTS_=(state:Boolean) = port.setRTS(state)
  60.     override def DSR:Boolean = port.isDSR
  61.     override def CTS:Boolean = port.isCTS
  62.     override def DCD:Boolean = port.isCD
  63.     override def RI:Boolean  = port.isRI
  64.  
  65.     override def availRX:Int = portInStream.available
  66.    
  67.     override def read(n:Int):Seq[Byte] = {
  68.         for (i <- 0 until (if (n == 0) availRX else math.min(n, availRX)))
  69.             yield (portInStream.read & 0xFF).toByte
  70.     }
  71.  
  72.     override def write(buf:Seq[Byte]):SerialBase = {
  73.         portOutStream.write(buf.toArray)
  74.         this
  75.     }
  76.    
  77.     override def flushRX:SerialBase = {
  78.         portInStream.skip(availRX)
  79.         this
  80.     }
  81.     override def flushTX:SerialBase = {
  82.         portOutStream.flush
  83.         this
  84.     }
  85.  
  86.     override def break( duration:Int ) = port.sendBreak(duration)
  87. }
  88.  
  89. object SerialPJC extends SerialBaseMgr {
  90.    
  91.     override def ports:Seq[String] = {
  92.         val ps = new collection.mutable.ArrayBuffer[String];
  93.         for (p <- enumerationAsScalaIterator(CommPortIdentifier.getPortIdentifiers)) {
  94.             val cpi = p.asInstanceOf[CommPortIdentifier];
  95.             if ((cpi.getPortType == CommPortIdentifier.PORT_SERIAL) &&
  96.                 (!cpi.isCurrentlyOwned) &&
  97.                 (List("COM","ttyS","ttyUSB","ttyACM","ttyBT","rfcomm","cu.usbserial","ttyd").exists(
  98.                     s => cpi.getName.startsWith(s)
  99.                 ))
  100.             ) ps += cpi.getName
  101.         }
  102.         ps.sorted
  103.     }
  104.    
  105.     override def open(name:String):SerialBase = new SerialPJC(name)
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement