Module: UART
- Defined in:
- lib/uart.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
- .data_bits(t, val) ⇒ Object
-
.open(filename, speed = 9600, mode = '8N1') ⇒ Object
Open a UART as a file.
- .parity(t, val) ⇒ Object
- .read_timeout(t, val) ⇒ Object
- .reading(t) ⇒ Object
- .speed(t, speed) ⇒ Object
- .stop_bits(t, val) ⇒ Object
Class Method Details
.data_bits(t, val) ⇒ Object
67 68 69 70 71 |
# File 'lib/uart.rb', line 67 def data_bits t, val t.cflag &= ~Termios::CSIZE # clear previous values t.cflag |= Termios.const_get("CS#{val}") # Set the data bits t end |
.open(filename, speed = 9600, mode = '8N1') ⇒ Object
Open a UART as a file.
For example, we have a serial connection on ‘/dev/tty.usbserial-00000000’. It’s speed is 9600, 8 data bits, 1 stop bit, and no parity:
serial = UART.open '/dev/tty.usbserial-00000000', 9600, '8N1'
serial.write 'whatever'
A speed of 9600, 8 data bits, no parity, and 1 stop bit is default, so we can reduce the code to this:
serial = UART.open '/dev/tty.usbserial-00000000'
serial.write 'whatever'
Finally, we can give a block and the file will be automatically closed for us like this:
UART.open '/dev/tty.usbserial-00000000' do |serial|
serial.write 'whatever'
end
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/uart.rb', line 28 def open filename, speed = 9600, mode = '8N1' f = File.open filename, File::RDWR|Fcntl::O_NOCTTY|Fcntl::O_NDELAY f.binmode f.sync = true # enable blocking reads, otherwise read timeout won't work f.fcntl Fcntl::F_SETFL, f.fcntl(Fcntl::F_GETFL, 0) & ~Fcntl::O_NONBLOCK t = Termios.tcgetattr f t.iflag = 0 t.oflag = 0 t.lflag = 0 if mode =~ /^(\d)(\w)(\d)$/ t = data_bits t, $1.to_i t = stop_bits t, $3.to_i t = parity t, { 'N' => :none, 'E' => :even, 'O' => :odd }[$2] t = speed t, speed t = read_timeout t, 5 t = reading t else raise ArgumentError, "Invalid format for mode" end Termios.tcsetattr f, Termios::TCSANOW, t Termios.tcflush f, Termios::TCIOFLUSH if block_given? begin yield f ensure f.close end else f end end |
.parity(t, val) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/uart.rb', line 85 def parity t, val case val when :none t.cflag &= ~Termios::PARENB when :even t.cflag |= Termios::PARENB # Enable parity t.cflag &= ~Termios::PARODD # Make it not odd when :odd t.cflag |= Termios::PARENB # Enable parity t.cflag |= Termios::PARODD # Make it odd else raise end t end |
.read_timeout(t, val) ⇒ Object
109 110 111 112 113 |
# File 'lib/uart.rb', line 109 def read_timeout t, val t.cc[Termios::VTIME] = val t.cc[Termios::VMIN] = 0 t end |
.reading(t) ⇒ Object
116 117 118 119 |
# File 'lib/uart.rb', line 116 def reading t t.cflag |= Termios::CLOCAL | Termios::CREAD t end |
.speed(t, speed) ⇒ Object
102 103 104 105 106 |
# File 'lib/uart.rb', line 102 def speed t, speed t.ispeed = Termios.const_get("B#{speed}") t.ospeed = Termios.const_get("B#{speed}") t end |
.stop_bits(t, val) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/uart.rb', line 74 def stop_bits t, val case val when 1 then t.cflag &= ~Termios::CSTOPB when 2 then t.cflag |= Termios::CSTOPB else raise end t end |