It is often useful to control the memory layout of fields in a data structure to match an interface control definition, or to interface with hardware. Define a data structure matching the RS-232 Plug Definition. Use the 9-pin definition for brevity.

Pin Settings for Plug
(Reverse order for socket.)
__________________________________________
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
_________________
1  2  3  4  5
6  7  8  9
25 pin                        9 pin
1 - PG   Protective ground
2 - TD   Transmitted data     3
3 - RD   Received data        2
4 - RTS  Request to send      7
5 - CTS  Clear to send        8
6 - DSR  Data set ready       6
7 - SG   Signal ground        5
8 - CD   Carrier detect       1
9 - + voltage (testing)
10 - - voltage (testing)
11 -
12 - SCD  Secondary CD
13 - SCS  Secondary CTS
14 - STD  Secondary TD
15 - TC   Transmit clock
16 - SRD  Secondary RD
17 - RC   Receiver clock
18 -
19 - SRS  Secondary RTS            
20 - DTR  Data terminal ready      4
21 - SQD  Signal quality detector
22 - RI   Ring indicator           9
23 - DRS  Data rate select
24 - XTC  External clock
25 -

object Rs232Pins9 extends App {
 
  val (off: Boolean, on: Boolean) = (false, true)
  val plug = new Rs232Pins9(carrierDetect = on, receivedData = on) // set first two pins, say
 
  def toOnOff(b: Boolean) = if (b) "on" else "off"
 
  class Rs232Pins9(
                    var carrierDetect: Boolean = off,
                    var receivedData: Boolean = off,
                    var transmittedData: Boolean = off,
                    var dataTerminalReady: Boolean = off,
                    var signalGround: Boolean = off,
                    var dataSetReady: Boolean = off,
                    var requestToSend: Boolean = off,
                    var clearToSend: Boolean = off,
                    var ringIndicator: Boolean = off
                  ) {
    def setPin(n: Int, v: Boolean) {
      (n) match {
        case 1 => carrierDetect = v
        case 2 => receivedData = v
        case 3 => transmittedData = v
        case 4 => dataTerminalReady = v
        case 5 => signalGround = v
        case 6 => dataSetReady = v
        case 7 => requestToSend = v
        case 8 => clearToSend = v
        case 9 => ringIndicator = v
      }
    }
  }
 
  // println(toOnOff(plug.component2()))                          // print value of pin 2 by number
  plug.transmittedData = on // set pin 3 by name
  plug.setPin(4, on) // set pin 4 by number
  // println(toOnOff(plug.component3()))                          // print value of pin 3 by number
  println(toOnOff(plug.dataTerminalReady)) // print value of pin 4 by name
  println(toOnOff(plug.ringIndicator)) // print value of pin 9 by name
}