ccutrer-serialport 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69c3b68becbcef8f755d29da38ddd0d1532efabb76daa1f4397b6fbc713f0893
4
- data.tar.gz: ff3d20273bd129636c01836f5e29a5a4ac82ac16ae9840c63e6fa79f16537184
3
+ metadata.gz: 105008488a20c325d7a6d178da216ba39a597dbc42d741079e3f84d49a84dc71
4
+ data.tar.gz: 03b74a72e0758729047cfb7553ece560ff7e209073e2ea5419f0bc215326b7a7
5
5
  SHA512:
6
- metadata.gz: 2f30d416a29cd1bab53072caeff2bfd2ade151ba1d0f687bae4b3217493a0dc63979def24e4fca3d68528af7570abf0df85bb9d9542b4ec0261fb8b4edf92ba5
7
- data.tar.gz: 3da04dcfa3ad985c233f6a8a7732b0b4abed9e5a41cba72804753bdab38a9b07cf3a5ba66f1b401f9f0356e3eea7e8fabd8a02d800ae06a1234098089caf7af2
6
+ metadata.gz: ca738d205a88eee990ae890ed3ebade20d59fbd7482a39b43938ab8ccbe5c4b39332ec740fe3b7b36c73f8cee5ac6bd4dd880cb5823c65f8cecd6a0de63c98b4
7
+ data.tar.gz: 5176af69f40896d1dddae480d88054955064eef462bbb5743e3a5a263dc363d2849f9f77ab683d5c25878e4e73dac2c368840631a934ecdd878f2e8afb842a42
@@ -1,85 +1,99 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2016 The Hybrid Group, 2020-2021 Cody Cutrer
2
4
 
3
- module CCutrer::SerialPort::Termios
4
- NCCS = 32
5
+ module CCutrer
6
+ class SerialPort < File
7
+ module Termios
8
+ NCCS = 32
5
9
 
6
- class Termios < FFI::Struct
7
- layout :c_iflag, :uint,
8
- :c_oflag, :uint,
9
- :c_cflag, :uint,
10
- :c_lflag, :uint,
11
- :c_line, :uchar,
12
- :c_cc, [ :uchar, NCCS ],
13
- :c_ispeed, :uint,
14
- :c_ospeed, :uint
15
- end
10
+ class Termios < FFI::Struct
11
+ layout :c_iflag,
12
+ :uint,
13
+ :c_oflag,
14
+ :uint,
15
+ :c_cflag,
16
+ :uint,
17
+ :c_lflag,
18
+ :uint,
19
+ :c_line,
20
+ :uchar,
21
+ :c_cc,
22
+ [:uchar, NCCS],
23
+ :c_ispeed,
24
+ :uint,
25
+ :c_ospeed,
26
+ :uint
27
+ end
16
28
 
17
- # c_cc characters
18
- VTIME = 5
19
- VMIN = 6
29
+ # c_cc characters
30
+ VTIME = 5
31
+ VMIN = 6
20
32
 
21
- # c_iflag bits
22
- IGNPAR = 0000004
33
+ # c_iflag bits
34
+ IGNPAR = 0o000004
23
35
 
24
- # c_cflag bits
25
- CSIZE = 0000060
26
- CSTOPB = 0000100
27
- CREAD = 0000200
28
- PARENB = 0000400
29
- PARODD = 0001000
30
- CLOCAL = 0004000
36
+ # c_cflag bits
37
+ CSIZE = 0o000060
38
+ CSTOPB = 0o000100
39
+ CREAD = 0o000200
40
+ PARENB = 0o000400
41
+ PARODD = 0o001000
42
+ CLOCAL = 0o004000
31
43
 
32
- DATA_BITS = {
33
- 5 => 0000000,
34
- 6 => 0000020,
35
- 7 => 0000040,
36
- 8 => 0000060
37
- }
44
+ DATA_BITS = {
45
+ 5 => 0o000000,
46
+ 6 => 0o000020,
47
+ 7 => 0o000040,
48
+ 8 => 0o000060
49
+ }.freeze
38
50
 
39
- BAUD_RATES = {
40
- 0 => 0000000,
41
- 50 => 0000001,
42
- 75 => 0000002,
43
- 110 => 0000003,
44
- 134 => 0000004,
45
- 150 => 0000005,
46
- 200 => 0000006,
47
- 300 => 0000007,
48
- 600 => 0000010,
49
- 1200 => 0000011,
50
- 1800 => 0000012,
51
- 2400 => 0000013,
52
- 4800 => 0000014,
53
- 9600 => 0000015,
54
- 19200 => 0000016,
55
- 38400 => 0000017,
56
- 57600 => 0010001,
57
- 115200 => 0010002,
58
- 230400 => 0010003,
59
- 460800 => 0010004,
60
- 500000 => 0010005,
61
- 576000 => 0010006,
62
- 921600 => 0010007,
63
- 1000000 => 0010010,
64
- 1152000 => 0010011,
65
- 1500000 => 0010012,
66
- 2000000 => 0010013,
67
- 2500000 => 0010014,
68
- 3000000 => 0010015,
69
- 3500000 => 0010016,
70
- 4000000 => 0010017
71
- }
51
+ BAUD_RATES = {
52
+ 0 => 0o000000,
53
+ 50 => 0o000001,
54
+ 75 => 0o000002,
55
+ 110 => 0o000003,
56
+ 134 => 0o000004,
57
+ 150 => 0o000005,
58
+ 200 => 0o000006,
59
+ 300 => 0o000007,
60
+ 600 => 0o000010,
61
+ 1200 => 0o000011,
62
+ 1800 => 0o000012,
63
+ 2400 => 0o000013,
64
+ 4800 => 0o000014,
65
+ 9600 => 0o000015,
66
+ 19_200 => 0o000016,
67
+ 38_400 => 0o000017,
68
+ 57_600 => 0o010001,
69
+ 115_200 => 0o010002,
70
+ 230_400 => 0o010003,
71
+ 460_800 => 0o010004,
72
+ 500_000 => 0o010005,
73
+ 576_000 => 0o010006,
74
+ 921_600 => 0o010007,
75
+ 1_000_000 => 0o010010,
76
+ 1_152_000 => 0o010011,
77
+ 1_500_000 => 0o010012,
78
+ 2_000_000 => 0o010013,
79
+ 2_500_000 => 0o010014,
80
+ 3_000_000 => 0o010015,
81
+ 3_500_000 => 0o010016,
82
+ 4_000_000 => 0o010017
83
+ }.freeze
72
84
 
73
- PARITY = {
74
- none: 0000000,
75
- even: PARENB,
76
- odd: PARENB | PARODD,
77
- }
85
+ PARITY = {
86
+ none: 0o000000,
87
+ even: PARENB,
88
+ odd: PARENB | PARODD
89
+ }.freeze
78
90
 
79
- STOP_BITS = {
80
- 1 => 0000000,
81
- 2 => CSTOPB
82
- }
91
+ STOP_BITS = {
92
+ 1 => 0o000000,
93
+ 2 => CSTOPB
94
+ }.freeze
83
95
 
84
- TCSANOW = 0
96
+ TCSANOW = 0
97
+ end
98
+ end
85
99
  end
@@ -1,77 +1,91 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2016 The Hybrid Group, 2020-2021 Cody Cutrer
2
4
 
3
- module CCutrer::SerialPort::Termios
4
- NCCS = 20
5
+ module CCutrer
6
+ class SerialPort < File
7
+ module Termios
8
+ NCCS = 20
5
9
 
6
- class Termios < FFI::Struct
7
- layout :c_iflag, :ulong,
8
- :c_oflag, :ulong,
9
- :c_cflag, :ulong,
10
- :c_lflag, :ulong,
11
- :c_line, :uchar,
12
- :c_cc, [ :uchar, NCCS ],
13
- :c_ispeed, :ulong,
14
- :c_ospeed, :ulong
15
- end
10
+ class Termios < FFI::Struct
11
+ layout :c_iflag,
12
+ :ulong,
13
+ :c_oflag,
14
+ :ulong,
15
+ :c_cflag,
16
+ :ulong,
17
+ :c_lflag,
18
+ :ulong,
19
+ :c_line,
20
+ :uchar,
21
+ :c_cc,
22
+ [:uchar, NCCS],
23
+ :c_ispeed,
24
+ :ulong,
25
+ :c_ospeed,
26
+ :ulong
27
+ end
16
28
 
17
- # c_cc characters
18
- VMIN = 16
19
- VTIME = 17
29
+ # c_cc characters
30
+ VMIN = 16
31
+ VTIME = 17
20
32
 
21
- # c_iflag bits
22
- IGNPAR = 0x00000004
33
+ # c_iflag bits
34
+ IGNPAR = 0x00000004
23
35
 
24
- # c_cflag bits
25
- CSIZE = 0x00000700
26
- CSTOPB = 0x00000400
27
- CREAD = 0x00000800
28
- PARENB = 0x00001000
29
- PARODD = 0x00002000
30
- CLOCAL = 0x00008000
36
+ # c_cflag bits
37
+ CSIZE = 0x00000300
38
+ CSTOPB = 0x00000400
39
+ CREAD = 0x00000800
40
+ PARENB = 0x00001000
41
+ PARODD = 0x00002000
42
+ CLOCAL = 0x00008000
31
43
 
32
- DATA_BITS = {
33
- 5 => 0x00000000,
34
- 6 => 0x00000100,
35
- 7 => 0x00000200,
36
- 8 => 0x00000300
37
- }
44
+ DATA_BITS = {
45
+ 5 => 0x00000000,
46
+ 6 => 0x00000100,
47
+ 7 => 0x00000200,
48
+ 8 => 0x00000300
49
+ }.freeze
38
50
 
39
- BAUD_RATES = {
40
- 0 => 0,
41
- 50 => 50,
42
- 75 => 75,
43
- 110 => 110,
44
- 134 => 134,
45
- 150 => 150,
46
- 200 => 200,
47
- 300 => 300,
48
- 600 => 600,
49
- 1200 => 1200,
50
- 1800 => 1800,
51
- 2400 => 2400,
52
- 4800 => 4800,
53
- 9600 => 9600,
54
- 19200 => 19200,
55
- 38400 => 38400,
56
- 7200 => 7200,
57
- 14400 => 14400,
58
- 28800 => 28800,
59
- 57600 => 57600,
60
- 76800 => 76800,
61
- 115200 => 115200,
62
- 230400 => 230400
63
- }
51
+ BAUD_RATES = {
52
+ 0 => 0,
53
+ 50 => 50,
54
+ 75 => 75,
55
+ 110 => 110,
56
+ 134 => 134,
57
+ 150 => 150,
58
+ 200 => 200,
59
+ 300 => 300,
60
+ 600 => 600,
61
+ 1200 => 1200,
62
+ 1800 => 1800,
63
+ 2400 => 2400,
64
+ 4800 => 4800,
65
+ 7200 => 7200,
66
+ 9600 => 9600,
67
+ 19_200 => 19_200,
68
+ 38_400 => 38_400,
69
+ 14_400 => 14_400,
70
+ 28_800 => 28_800,
71
+ 57_600 => 57_600,
72
+ 76_800 => 76_800,
73
+ 115_200 => 115_200,
74
+ 230_400 => 230_400
75
+ }.freeze
64
76
 
65
- PARITY = {
66
- :none => 0x00000000,
67
- :even => PARENB,
68
- :odd => PARENB | PARODD,
69
- }
77
+ PARITY = {
78
+ none: 0x00000000,
79
+ even: PARENB,
80
+ odd: PARENB | PARODD
81
+ }.freeze
70
82
 
71
- STOP_BITS = {
72
- 1 => 0x00000000,
73
- 2 => CSTOPB
74
- }
83
+ STOP_BITS = {
84
+ 1 => 0x00000000,
85
+ 2 => CSTOPB
86
+ }.freeze
75
87
 
76
- TCSANOW = 0
88
+ TCSANOW = 0
89
+ end
90
+ end
77
91
  end
@@ -1,10 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2016 The Hybrid Group, 2020-2021 Cody Cutrer
2
4
 
3
- module CCutrer::SerialPort::Termios
4
- attach_function :cfgetispeed, [ Termios ], :uint, blocking: true
5
- attach_function :cfgetospeed, [ Termios ], :uint, blocking: true
6
- attach_function :cfsetispeed, [ Termios, :uint ], :int, blocking: true
7
- attach_function :cfsetospeed, [ Termios, :uint ], :int, blocking: true
8
- attach_function :tcsetattr, [ :int, :int, Termios ], :int, blocking: true
9
- attach_function :tcgetattr, [ :int, Termios ], :int, blocking: true
5
+ module CCutrer
6
+ class SerialPort < File
7
+ module Termios
8
+ attach_function :cfgetispeed, [Termios], :uint
9
+ attach_function :cfgetospeed, [Termios], :uint
10
+ attach_function :cfsetispeed, [Termios, :uint], :int
11
+ attach_function :cfsetospeed, [Termios, :uint], :int
12
+ attach_function :tcsetattr, [:int, :int, Termios], :int
13
+ attach_function :tcgetattr, [:int, Termios], :int
14
+
15
+ begin
16
+ attach_function :cfsetibaud, [Termios, :uint], :int
17
+ attach_function :cfsetobaud, [Termios, :uint], :int
18
+ attach_function :cfgetibaud, [Termios], :uint
19
+ attach_function :cfgetobaud, [Termios], :uint
20
+ rescue FFI::NotFoundError
21
+ # ignore; will fall back to hardcoded constants
22
+ end
23
+ end
24
+ end
10
25
  end
@@ -1,6 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2016 The Hybrid Group, 2020-2021 Cody Cutrer
2
4
 
3
- module CCutrer::SerialPort::Termios
4
- extend FFI::Library
5
- ffi_lib FFI::Library::LIBC
5
+ module CCutrer
6
+ class SerialPort < File
7
+ module Termios
8
+ extend FFI::Library
9
+
10
+ ffi_lib FFI::Library::LIBC
11
+ end
12
+ end
6
13
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2018 The Hybrid Group and Friends
2
4
 
3
5
  module CCutrer
4
6
  class SerialPort < File
5
- VERSION = "1.1.0"
7
+ VERSION = "1.2.0"
6
8
  end
7
9
  end
@@ -1,133 +1,166 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2016 The Hybrid Group, 2020 Cody Cutrer
2
4
 
3
- require 'fcntl'
5
+ module CCutrer
6
+ class SerialPort < File
7
+ def initialize(address, baud: nil, data_bits: nil, parity: nil, stop_bits: nil)
8
+ super(address, IO::RDWR | IO::NOCTTY | IO::NONBLOCK)
4
9
 
5
- class CCutrer::SerialPort < File
6
- def initialize(address, baud: nil, data_bits: nil, parity: nil, stop_bits: nil)
7
- super(address, IO::RDWR | IO::NOCTTY | IO::NONBLOCK)
10
+ raise IOError, "Not a serial port" unless tty?
8
11
 
9
- raise IOError, "Not a serial port" unless tty?
12
+ @termios = Termios::Termios.new
13
+ refresh
10
14
 
11
- fl = fcntl(Fcntl::F_GETFL, 0)
12
- fcntl(Fcntl::F_SETFL, ~Fcntl::O_NONBLOCK & fl)
15
+ # base config
16
+ @termios[:c_iflag] = Termios::IGNPAR
17
+ @termios[:c_oflag] = 0
18
+ @termios[:c_cflag] =
19
+ Termios::CREAD |
20
+ Termios::CLOCAL
21
+ @termios[:c_lflag] = 0
22
+
23
+ @termios[:c_cc][Termios::VTIME] = 0
24
+ @termios[:c_cc][Termios::VMIN] = 0
25
+
26
+ configure do
27
+ self.baud = baud if baud
28
+ self.data_bits = data_bits if data_bits
29
+ self.parity = parity if parity
30
+ self.stop_bits = stop_bits if stop_bits
31
+ end
32
+ raise Errno::EINVAL if baud && baud != self.baud
33
+ raise Errno::EINVAL if data_bits && data_bits != self.data_bits
34
+ raise Errno::EINVAL if parity && parity != self.parity
35
+ raise Errno::EINVAL if stop_bits && stop_bits != self.stop_bits
36
+ end
13
37
 
14
- @termios = Termios::Termios.new
15
- refresh
38
+ # Read up to +length+ bytes that are currently available without waiting.
39
+ def read(length, outbuf = nil)
40
+ result = read_nonblock(length, outbuf, exception: false)
41
+ return result unless result == :wait_readable
16
42
 
17
- # base config
18
- @termios[:c_iflag] = Termios::IGNPAR
19
- @termios[:c_oflag] = 0
20
- @termios[:c_cflag] =
21
- Termios::CREAD |
22
- Termios::CLOCAL
23
- @termios[:c_lflag] = 0
43
+ nil
44
+ end
24
45
 
25
- @termios[:c_cc][Termios::VTIME] = 0
26
- @termios[:c_cc][Termios::VMIN] = 0
46
+ def getbyte
47
+ read(1)&.getbyte(0)
48
+ end
27
49
 
28
- configure do
29
- self.baud = baud if baud
30
- self.data_bits = data_bits if data_bits
31
- self.parity = parity if parity
32
- self.stop_bits = stop_bits if stop_bits
50
+ def configure
51
+ @configuring = true
52
+ yield
53
+ @configuring = false
54
+ apply
55
+ refresh
56
+ ensure
57
+ @configuring = false
33
58
  end
34
- raise Errno::EINVAL if baud && baud != self.baud
35
- raise Errno::EINVAL if data_bits && data_bits != self.data_bits
36
- raise Errno::EINVAL if parity && parity != self.parity
37
- raise Errno::EINVAL if stop_bits && stop_bits != self.stop_bits
38
- end
39
59
 
40
- def configure
41
- @configuring = true
42
- yield
43
- @configuring = false
44
- apply
45
- refresh
46
- ensure
47
- @configuring = false
48
- end
60
+ if Termios.respond_to?(:cfgetibaud)
61
+ def baud
62
+ Termios.cfgetibaud(@termios)
63
+ end
49
64
 
50
- def baud
51
- Termios::BAUD_RATES.invert[Termios.cfgetispeed(@termios)]
52
- end
65
+ def baud=(baud)
66
+ raise ArgumentError, "Invalid baud" unless Termios::BAUD_RATES.key?(baud)
53
67
 
54
- def baud=(baud)
55
- raise ArgumentError, "Invalid baud" unless Termios::BAUD_RATES.key?(baud)
68
+ err = Termios.cfsetibaud(@termios, baud)
69
+ raise SystemCallError, FFI.errno if err == -1
56
70
 
57
- err = Termios.cfsetispeed(@termios, Termios::BAUD_RATES[baud])
58
- raise SystemCallError, FFI.errno if err == -1
59
- err = Termios.cfsetospeed(@termios, Termios::BAUD_RATES[baud])
60
- raise SystemCallError, FFI.errno if err == -1
61
- apply
62
- refresh
63
- self.baud
64
- end
71
+ err = Termios.cfsetobaud(@termios, baud)
72
+ raise SystemCallError, FFI.errno if err == -1
65
73
 
66
- def data_bits
67
- Termios::DATA_BITS.invert[@termios[:c_cflag] & Termios::CSIZE]
68
- end
74
+ apply
75
+ refresh
76
+ end
77
+ else
78
+ def baud
79
+ Termios::BAUD_RATES.invert[Termios.cfgetispeed(@termios)]
80
+ end
69
81
 
70
- def data_bits=(data_bits)
71
- raise ArgumentError, "Invalid data bits" unless Termios::DATA_BITS.key?(data_bits)
82
+ def baud=(baud)
83
+ raise ArgumentError, "Invalid baud" unless Termios::BAUD_RATES.key?(baud)
72
84
 
73
- @termios[:c_cflag] &= ~Termios::CSIZE
74
- @termios[:c_cflag] |= Termios::DATA_BITS[data_bits]
75
- apply
76
- refresh
77
- self.data_bits
78
- end
85
+ err = Termios.cfsetispeed(@termios, Termios::BAUD_RATES[baud])
86
+ raise SystemCallError, FFI.errno if err == -1
79
87
 
80
- def parity
81
- Termios::PARITY.invert[@termios[:c_cflag] & Termios::PARITY[:odd]]
82
- end
88
+ err = Termios.cfsetospeed(@termios, Termios::BAUD_RATES[baud])
89
+ raise SystemCallError, FFI.errno if err == -1
83
90
 
84
- def parity=(parity)
85
- raise ArgumentError, "Invalid parity" unless Termios::PARITY.key?(parity)
91
+ apply
92
+ refresh
93
+ end
94
+ end
86
95
 
87
- @termios[:c_cflag] &= ~Termios::PARITY[:odd]
88
- @termios[:c_cflag] |= Termios::PARITY[parity]
89
- apply
90
- refresh
91
- self.parity
92
- end
96
+ def data_bits
97
+ Termios::DATA_BITS.invert[@termios[:c_cflag] & Termios::CSIZE]
98
+ end
93
99
 
94
- def stop_bits
95
- (@termios[:c_cflag] & Termios::CSTOPB) != 0 ? 2 : 1
96
- end
100
+ def data_bits=(data_bits)
101
+ raise ArgumentError, "Invalid data bits" unless Termios::DATA_BITS.key?(data_bits)
102
+
103
+ @termios[:c_cflag] &= ~Termios::CSIZE
104
+ @termios[:c_cflag] |= Termios::DATA_BITS[data_bits]
105
+ apply
106
+ refresh
107
+ end
97
108
 
98
- def stop_bits=(stop_bits)
99
- raise ArgumentError, "Invalid stop bits" unless Termios::STOP_BITS.key?(stop_bits)
109
+ def parity
110
+ Termios::PARITY.invert[@termios[:c_cflag] & Termios::PARITY[:odd]]
111
+ end
100
112
 
101
- @termios[:c_cflag] &= ~Termios::CSTOPB
102
- @termios[:c_cflag] |= Termios::STOP_BITS[stop_bits]
103
- apply
104
- refresh
105
- self.stop_bits
106
- end
113
+ def parity=(parity)
114
+ raise ArgumentError, "Invalid parity" unless Termios::PARITY.key?(parity)
107
115
 
108
- def inspect
109
- "#<#{self.class.name}:#{path} #{baud} #{data_bits}#{parity.to_s[0].upcase}#{stop_bits}>"
110
- end
116
+ @termios[:c_cflag] &= ~Termios::PARITY[:odd]
117
+ @termios[:c_cflag] |= Termios::PARITY[parity]
118
+ apply
119
+ refresh
120
+ end
121
+
122
+ def stop_bits
123
+ @termios[:c_cflag].nobits?(Termios::CSTOPB) ? 1 : 2
124
+ end
111
125
 
112
- private
126
+ def stop_bits=(stop_bits)
127
+ raise ArgumentError, "Invalid stop bits" unless Termios::STOP_BITS.key?(stop_bits)
113
128
 
114
- def apply
115
- return if @configuring
116
- err = Termios.tcsetattr(fileno, Termios::TCSANOW, @termios)
117
- raise SystemCallError, FFI.errno if err == -1
118
- self
119
- rescue SystemCallError
120
- begin
129
+ @termios[:c_cflag] &= ~Termios::CSTOPB
130
+ @termios[:c_cflag] |= Termios::STOP_BITS[stop_bits]
131
+ apply
121
132
  refresh
133
+ end
134
+
135
+ def inspect
136
+ "#<#{self.class.name}:#{path} #{baud} #{data_bits}#{parity.to_s[0].upcase}#{stop_bits}>"
137
+ end
138
+
139
+ private
140
+
141
+ def apply
142
+ return if @configuring
143
+
144
+ err = Termios.tcsetattr(fileno, Termios::TCSANOW, @termios)
145
+ raise SystemCallError, FFI.errno if err == -1
146
+
147
+ self
122
148
  rescue SystemCallError
149
+ begin
150
+ refresh
151
+ rescue SystemCallError
152
+ # ignore errors during refresh after a failed apply
153
+ end
154
+ raise
123
155
  end
124
- raise
125
- end
126
156
 
127
- def refresh
128
- return if @configuring
129
- err = Termios.tcgetattr(fileno, @termios)
130
- raise SystemCallError, FFI.errno if err == -1
131
- self
157
+ def refresh
158
+ return if @configuring
159
+
160
+ err = Termios.tcgetattr(fileno, @termios)
161
+ raise SystemCallError, FFI.errno if err == -1
162
+
163
+ self
164
+ end
132
165
  end
133
166
  end
@@ -1,27 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2014-2016 The Hybrid Group, 2020 Cody Cutrer
2
4
 
3
- require 'rbconfig'
4
- require 'ffi'
5
+ require "rbconfig"
6
+ require "ffi"
5
7
 
6
8
  module CCutrer
7
9
  class SerialPort < File
8
- ON_WINDOWS = RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i
9
- ON_LINUX = RbConfig::CONFIG['host_os'] =~ /linux/i
10
+ ON_WINDOWS = RbConfig::CONFIG["host_os"] =~ /mswin|windows|mingw/i
11
+ ON_LINUX = RbConfig::CONFIG["host_os"] =~ /linux/i
10
12
 
11
- if ON_WINDOWS
12
- raise "rubyserial is not currently supported on windows"
13
- end
13
+ raise "rubyserial is not currently supported on windows" if ON_WINDOWS
14
14
 
15
15
  # order is important here
16
- require 'ccutrer/serial_port/termios'
16
+ require "ccutrer/serial_port/termios"
17
17
  if ON_LINUX
18
- require 'ccutrer/serial_port/linux'
18
+ require "ccutrer/serial_port/linux"
19
19
  else
20
- require 'ccutrer/serial_port/osx'
20
+ require "ccutrer/serial_port/osx"
21
21
  end
22
- require 'ccutrer/serial_port/posix'
22
+ require "ccutrer/serial_port/posix"
23
23
  end
24
24
  end
25
25
 
26
- require 'ccutrer/serial_port'
27
- require 'ccutrer/serial_port/version'
26
+ require "ccutrer/serial_port"
27
+ require "ccutrer/serial_port/version"
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccutrer-serialport
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-02-09 00:00:00.000000000 Z
10
+ date: 2026-07-28 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: ffi
@@ -30,50 +29,12 @@ dependencies:
30
29
  - - ">="
31
30
  - !ruby/object:Gem::Version
32
31
  version: 1.9.3
33
- - !ruby/object:Gem::Dependency
34
- name: rake
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '13.0'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '13.0'
47
- - !ruby/object:Gem::Dependency
48
- name: rspec
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: 3.0.0
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: 3.0.0
61
32
  description: Ruby only library that relies on FFI instead of an extension, and inherits
62
33
  from IO
63
- email:
64
34
  executables: []
65
35
  extensions: []
66
36
  extra_rdoc_files: []
67
37
  files:
68
- - ".gitignore"
69
- - ".rspec"
70
- - ".travis.yml"
71
- - Gemfile
72
- - Gemfile.lock
73
- - LICENSE
74
- - README.md
75
- - Rakefile
76
- - ccutrer-serialport.gemspec
77
38
  - lib/ccutrer-serialport.rb
78
39
  - lib/ccutrer/serial_port.rb
79
40
  - lib/ccutrer/serial_port/linux.rb
@@ -81,13 +42,11 @@ files:
81
42
  - lib/ccutrer/serial_port/posix.rb
82
43
  - lib/ccutrer/serial_port/termios.rb
83
44
  - lib/ccutrer/serial_port/version.rb
84
- - spec/serialport_spec.rb
85
- - spec/spec_helper.rb
86
45
  homepage: https://github.com/ccutrer/ccutrer-serialport
87
46
  licenses:
88
47
  - MIT
89
- metadata: {}
90
- post_install_message:
48
+ metadata:
49
+ rubygems_mfa_required: 'true'
91
50
  rdoc_options: []
92
51
  require_paths:
93
52
  - lib
@@ -95,15 +54,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
54
  requirements:
96
55
  - - ">="
97
56
  - !ruby/object:Gem::Version
98
- version: '2.0'
57
+ version: '3.1'
99
58
  required_rubygems_version: !ruby/object:Gem::Requirement
100
59
  requirements:
101
60
  - - ">="
102
61
  - !ruby/object:Gem::Version
103
62
  version: '0'
104
63
  requirements: []
105
- rubygems_version: 3.1.4
106
- signing_key:
64
+ rubygems_version: 3.6.2
107
65
  specification_version: 4
108
66
  summary: Linux/OS X RS-232 serial port communication
109
67
  test_files: []
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- /pkg
2
- /socat.log
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --warnings
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,11 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- addons:
4
- apt:
5
- packages:
6
- - socat
7
- rvm:
8
- - 2.7
9
- - 2.6
10
- script:
11
- - bundle exec rake spec
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gemspec
data/Gemfile.lock DELETED
@@ -1,35 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- ccutrer-serialport (1.1.0)
5
- ffi (~> 1.9, >= 1.9.3)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- diff-lcs (1.4.4)
11
- ffi (1.13.1)
12
- rake (13.0.1)
13
- rspec (3.0.0)
14
- rspec-core (~> 3.0.0)
15
- rspec-expectations (~> 3.0.0)
16
- rspec-mocks (~> 3.0.0)
17
- rspec-core (3.0.4)
18
- rspec-support (~> 3.0.0)
19
- rspec-expectations (3.0.4)
20
- diff-lcs (>= 1.2.0, < 2.0)
21
- rspec-support (~> 3.0.0)
22
- rspec-mocks (3.0.4)
23
- rspec-support (~> 3.0.0)
24
- rspec-support (3.0.4)
25
-
26
- PLATFORMS
27
- ruby
28
-
29
- DEPENDENCIES
30
- ccutrer-serialport!
31
- rake (~> 13.0)
32
- rspec (~> 3.0.0)
33
-
34
- BUNDLED WITH
35
- 2.1.4
data/LICENSE DELETED
@@ -1,7 +0,0 @@
1
- Copyright 2020 Cody Cutrer
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md DELETED
@@ -1,38 +0,0 @@
1
- # ccutrer-serialport
2
-
3
- SerialPort is a simple Ruby gem for interacting with serial ports.
4
-
5
- It does not require an extension thanks to using [FFI](https://github.com/ffi/ffi).
6
-
7
- SerialPort objects inherit from `File` (and thus `IO`), so are compatible with all
8
- IO methods, in particular `read_partial`, and asynchronous IO.
9
-
10
- It was inspired by the [rubyserial](https://github.com/hybridgroup/rubyserial) gem.
11
-
12
- ## Usage
13
-
14
- ```ruby
15
- require 'ccutrer-rubyserial'
16
- serialport = CCutrer::SerialPort.new '/dev/ttyACM0' # Defaults to 9600 baud, 8 data bits, and no parity
17
- serialport = CCutrer::SerialPort.new '/dev/ttyACM0', baud: 57600
18
- serialport = CCutrer::SerialPort.new '/dev/ttyACM0', baud: 19200, data_bits: 8, parity: :even
19
-
20
- serialport.read(4096)
21
- serialport.gets
22
-
23
- require 'io/wait'
24
- serialport.ready?
25
- serialport.wait_readable(1)
26
- ```
27
-
28
- ## Running the tests
29
-
30
- The test suite is written using rspec, just use the `rspec` command.
31
-
32
- ### Test dependencies
33
-
34
- To run the tests, you must also have `socat` installed.
35
-
36
- ## License
37
-
38
- MIT. See `LICENSE` for more details.
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
@@ -1,24 +0,0 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
- require "ccutrer/serial_port/version"
3
-
4
- Gem::Specification.new do |s|
5
- s.name = "ccutrer-serialport"
6
- s.version = CCutrer::SerialPort::VERSION
7
- s.summary = "Linux/OS X RS-232 serial port communication"
8
- s.description = "Ruby only library that relies on FFI instead of an extension, and inherits from IO"
9
- s.homepage = "https://github.com/ccutrer/ccutrer-serialport"
10
- s.authors = ["Cody Cutrer"]
11
- s.platform = Gem::Platform::RUBY
12
- s.license = 'MIT'
13
-
14
- s.required_ruby_version = '>= 2.0'
15
-
16
- s.files = `git ls-files`.split("\n")
17
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ["lib"]
19
-
20
- s.add_runtime_dependency 'ffi', '~> 1.9', '>= 1.9.3'
21
-
22
- s.add_development_dependency 'rake', '~> 13.0'
23
- s.add_development_dependency 'rspec', '~> 3.0.0'
24
- end
@@ -1,177 +0,0 @@
1
- require 'ccutrer-serialport'
2
-
3
- describe CCutrer::SerialPort do
4
- before do
5
- @ports = []
6
- File.delete('socat.log') if File.file?('socat.log')
7
-
8
- raise 'socat not found' unless (`socat -h` && $? == 0)
9
-
10
- Thread.new do
11
- system('socat -lf socat.log -d -d pty,raw,echo=0 pty,raw,echo=0')
12
- end
13
-
14
- @ptys = nil
15
-
16
- loop do
17
- if File.file? 'socat.log'
18
- @file = File.open('socat.log', "r")
19
- @fileread = @file.read
20
-
21
- unless @fileread.count("\n") < 3
22
- @ptys = @fileread.scan(/PTY is (.*)/)
23
- break
24
- end
25
- end
26
- end
27
-
28
- @ports = [@ptys[1][0], @ptys[0][0]]
29
-
30
- @sp2 = CCutrer::SerialPort.new(@ports[0])
31
- @sp = CCutrer::SerialPort.new(@ports[1])
32
- end
33
-
34
- after do
35
- @sp2.close
36
- @sp.close
37
- end
38
-
39
- it "should read and write" do
40
- @sp2.write('hello')
41
- # small delay so it can write to the other port.
42
- sleep 0.1
43
- check = @sp.read(5)
44
- expect(check).to eql('hello')
45
- end
46
-
47
- it "should convert ints to strings" do
48
- expect(@sp2.write(123)).to eql(3)
49
- sleep 0.1
50
- expect(@sp.read(3)).to eql('123')
51
- end
52
-
53
- it "write should return bytes written" do
54
- expect(@sp2.write('hello')).to eql(5)
55
- end
56
-
57
- it "reading nothing should be blank" do
58
- expect(@sp.read(5)).to be_nil
59
- end
60
-
61
- it "should give me nil on getbyte" do
62
- expect(@sp.getbyte).to be_nil
63
- end
64
-
65
- it 'should give me a zero byte from getbyte' do
66
- @sp2.write("\x00")
67
- sleep 0.1
68
- expect(@sp.getbyte).to eql(0)
69
- end
70
-
71
- it "should give me bytes" do
72
- @sp2.write('hello')
73
- # small delay so it can write to the other port.
74
- sleep 0.1
75
- check = @sp.getbyte
76
- expect([check].pack('C')).to eql('h')
77
- end
78
-
79
- describe "giving me lines" do
80
- it "should give me a line" do
81
- @sp.write("no yes \n hello")
82
- sleep 0.1
83
- expect(@sp2.gets).to eql("no yes \n")
84
- end
85
-
86
- it "should give me a line with block" do
87
- @sp.write("no yes \n hello")
88
- sleep 0.1
89
- result = ""
90
- @sp2.each_line do |line|
91
- result = line
92
- break if !result.empty?
93
- end
94
- expect(result).to eql("no yes \n")
95
- end
96
-
97
- it "should accept a sep param" do
98
- @sp.write('no yes END bleh')
99
- sleep 0.1
100
- expect(@sp2.gets('END')).to eql("no yes END")
101
- end
102
-
103
- it "should accept a limit param" do
104
- @sp.write("no yes \n hello")
105
- sleep 0.1
106
- expect(@sp2.gets(4)).to eql("no y")
107
- end
108
-
109
- it "should accept limit and sep params" do
110
- @sp.write("no yes END hello")
111
- sleep 0.1
112
- expect(@sp2.gets('END', 20)).to eql("no yes END")
113
- @sp2.read(1000)
114
- @sp.write("no yes END hello")
115
- sleep 0.1
116
- expect(@sp2.gets('END', 4)).to eql('no y')
117
- end
118
-
119
- it "should read a paragraph at a time" do
120
- @sp.write("Something \n Something else \n\n and other stuff")
121
- sleep 0.1
122
- expect(@sp2.gets('')).to eql("Something \n Something else \n\n")
123
- end
124
- end
125
-
126
- describe 'config' do
127
- it 'should accept EVEN parity' do
128
- @sp2.close
129
- @sp.close
130
- @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :even)
131
- @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :even)
132
- @sp.write("Hello!\n")
133
- sleep 0.1
134
- expect(@sp2.gets).to eql("Hello!\n")
135
- end
136
-
137
- it 'should accept ODD parity' do
138
- @sp2.close
139
- @sp.close
140
- @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :odd)
141
- @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :odd)
142
- @sp.write("Hello!\n")
143
- sleep 0.1
144
- expect(@sp2.gets).to eql("Hello!\n")
145
- end
146
-
147
- it 'should accept 1 stop bit' do
148
- @sp2.close
149
- @sp.close
150
- @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :none, stop_bits: 1)
151
- @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :none, stop_bits: 1)
152
- @sp.write("Hello!\n")
153
- sleep 0.1
154
- expect(@sp2.gets).to eql("Hello!\n")
155
- end
156
-
157
- it 'should accept 2 stop bits' do
158
- @sp2.close
159
- @sp.close
160
- @sp2 = CCutrer::SerialPort.new(@ports[0], baud: 19200, data_bits: 8, parity: :none, stop_bits: 2)
161
- @sp = CCutrer::SerialPort.new(@ports[1], baud: 19200, data_bits: 8, parity: :none, stop_bits: 2)
162
- @sp.write("Hello!\n")
163
- sleep 0.1
164
- expect(@sp2.gets).to eql("Hello!\n")
165
- end
166
-
167
- it 'should set baud rate, check #46 fixed' do
168
- @sp.close
169
- rate = 600
170
- @sp = CCutrer::SerialPort.new(@ports[1], baud: rate)
171
- fd = @sp.fileno
172
- termios = CCutrer::SerialPort::Posix::Termios.new
173
- CCutrer::SerialPort::Posix.tcgetattr(fd, termios)
174
- expect(termios[:c_ispeed]).to eql(CCutrer::SerialPort::Posix::BAUD_RATES[rate])
175
- end
176
- end
177
- end
data/spec/spec_helper.rb DELETED
@@ -1,2 +0,0 @@
1
- RSpec.configure do |config|
2
- end