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 +4 -4
- data/lib/ccutrer/serial_port/linux.rb +87 -73
- data/lib/ccutrer/serial_port/osx.rb +79 -65
- data/lib/ccutrer/serial_port/posix.rb +22 -7
- data/lib/ccutrer/serial_port/termios.rb +10 -3
- data/lib/ccutrer/serial_port/version.rb +3 -1
- data/lib/ccutrer/serial_port.rb +134 -101
- data/lib/ccutrer-serialport.rb +13 -13
- metadata +6 -48
- data/.gitignore +0 -2
- data/.rspec +0 -3
- data/.travis.yml +0 -11
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -35
- data/LICENSE +0 -7
- data/README.md +0 -38
- data/Rakefile +0 -2
- data/ccutrer-serialport.gemspec +0 -24
- data/spec/serialport_spec.rb +0 -177
- data/spec/spec_helper.rb +0 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 105008488a20c325d7a6d178da216ba39a597dbc42d741079e3f84d49a84dc71
|
|
4
|
+
data.tar.gz: 03b74a72e0758729047cfb7553ece560ff7e209073e2ea5419f0bc215326b7a7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
4
|
-
|
|
5
|
+
module CCutrer
|
|
6
|
+
class SerialPort < File
|
|
7
|
+
module Termios
|
|
8
|
+
NCCS = 32
|
|
5
9
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
# c_cc characters
|
|
30
|
+
VTIME = 5
|
|
31
|
+
VMIN = 6
|
|
20
32
|
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
# c_iflag bits
|
|
34
|
+
IGNPAR = 0o000004
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
DATA_BITS = {
|
|
45
|
+
5 => 0o000000,
|
|
46
|
+
6 => 0o000020,
|
|
47
|
+
7 => 0o000040,
|
|
48
|
+
8 => 0o000060
|
|
49
|
+
}.freeze
|
|
38
50
|
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
85
|
+
PARITY = {
|
|
86
|
+
none: 0o000000,
|
|
87
|
+
even: PARENB,
|
|
88
|
+
odd: PARENB | PARODD
|
|
89
|
+
}.freeze
|
|
78
90
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
91
|
+
STOP_BITS = {
|
|
92
|
+
1 => 0o000000,
|
|
93
|
+
2 => CSTOPB
|
|
94
|
+
}.freeze
|
|
83
95
|
|
|
84
|
-
|
|
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
|
|
4
|
-
|
|
5
|
+
module CCutrer
|
|
6
|
+
class SerialPort < File
|
|
7
|
+
module Termios
|
|
8
|
+
NCCS = 20
|
|
5
9
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
# c_cc characters
|
|
30
|
+
VMIN = 16
|
|
31
|
+
VTIME = 17
|
|
20
32
|
|
|
21
|
-
|
|
22
|
-
|
|
33
|
+
# c_iflag bits
|
|
34
|
+
IGNPAR = 0x00000004
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
DATA_BITS = {
|
|
45
|
+
5 => 0x00000000,
|
|
46
|
+
6 => 0x00000100,
|
|
47
|
+
7 => 0x00000200,
|
|
48
|
+
8 => 0x00000300
|
|
49
|
+
}.freeze
|
|
38
50
|
|
|
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
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
PARITY = {
|
|
78
|
+
none: 0x00000000,
|
|
79
|
+
even: PARENB,
|
|
80
|
+
odd: PARENB | PARODD
|
|
81
|
+
}.freeze
|
|
70
82
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
83
|
+
STOP_BITS = {
|
|
84
|
+
1 => 0x00000000,
|
|
85
|
+
2 => CSTOPB
|
|
86
|
+
}.freeze
|
|
75
87
|
|
|
76
|
-
|
|
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
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
4
|
-
|
|
5
|
-
|
|
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
|
data/lib/ccutrer/serial_port.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
12
|
+
@termios = Termios::Termios.new
|
|
13
|
+
refresh
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
15
|
-
|
|
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
|
-
|
|
18
|
-
|
|
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
|
-
|
|
26
|
-
|
|
46
|
+
def getbyte
|
|
47
|
+
read(1)&.getbyte(0)
|
|
48
|
+
end
|
|
27
49
|
|
|
28
|
-
configure
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
end
|
|
65
|
+
def baud=(baud)
|
|
66
|
+
raise ArgumentError, "Invalid baud" unless Termios::BAUD_RATES.key?(baud)
|
|
53
67
|
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
err = Termios.cfsetibaud(@termios, baud)
|
|
69
|
+
raise SystemCallError, FFI.errno if err == -1
|
|
56
70
|
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
apply
|
|
75
|
+
refresh
|
|
76
|
+
end
|
|
77
|
+
else
|
|
78
|
+
def baud
|
|
79
|
+
Termios::BAUD_RATES.invert[Termios.cfgetispeed(@termios)]
|
|
80
|
+
end
|
|
69
81
|
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
def baud=(baud)
|
|
83
|
+
raise ArgumentError, "Invalid baud" unless Termios::BAUD_RATES.key?(baud)
|
|
72
84
|
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
end
|
|
88
|
+
err = Termios.cfsetospeed(@termios, Termios::BAUD_RATES[baud])
|
|
89
|
+
raise SystemCallError, FFI.errno if err == -1
|
|
83
90
|
|
|
84
|
-
|
|
85
|
-
|
|
91
|
+
apply
|
|
92
|
+
refresh
|
|
93
|
+
end
|
|
94
|
+
end
|
|
86
95
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
99
|
-
|
|
109
|
+
def parity
|
|
110
|
+
Termios::PARITY.invert[@termios[:c_cflag] & Termios::PARITY[:odd]]
|
|
111
|
+
end
|
|
100
112
|
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
126
|
+
def stop_bits=(stop_bits)
|
|
127
|
+
raise ArgumentError, "Invalid stop bits" unless Termios::STOP_BITS.key?(stop_bits)
|
|
113
128
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
data/lib/ccutrer-serialport.rb
CHANGED
|
@@ -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
|
|
4
|
-
require
|
|
5
|
+
require "rbconfig"
|
|
6
|
+
require "ffi"
|
|
5
7
|
|
|
6
8
|
module CCutrer
|
|
7
9
|
class SerialPort < File
|
|
8
|
-
ON_WINDOWS = RbConfig::CONFIG[
|
|
9
|
-
ON_LINUX = RbConfig::CONFIG[
|
|
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
|
|
16
|
+
require "ccutrer/serial_port/termios"
|
|
17
17
|
if ON_LINUX
|
|
18
|
-
require
|
|
18
|
+
require "ccutrer/serial_port/linux"
|
|
19
19
|
else
|
|
20
|
-
require
|
|
20
|
+
require "ccutrer/serial_port/osx"
|
|
21
21
|
end
|
|
22
|
-
require
|
|
22
|
+
require "ccutrer/serial_port/posix"
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
require
|
|
27
|
-
require
|
|
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.
|
|
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:
|
|
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
|
-
|
|
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: '
|
|
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.
|
|
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
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
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
data/ccutrer-serialport.gemspec
DELETED
|
@@ -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
|
data/spec/serialport_spec.rb
DELETED
|
@@ -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