kiss_serial.rb 2.13 KB
Newer Older
1
require 'serialport'
2
require 'kiss/kiss_abstract'
3

4
5
module Kiss
    class KissSerial < KissAbstract
6
7
8
9
10
11
12
13
14
15
16
17
18

        DEFAULT_READ_BYTES = 1000
        SERIAL_READ_TIMEOUT = -1

        protected
        def initialize(com_port,
                       baud=38400,
                       parity=SerialPort::NONE,
                       stop_bits=1,
                       byte_size=8,
                       read_bytes=DEFAULT_READ_BYTES,
                       strip_df_start=true)
            super(strip_df_start)
19

20
21
22
23
24
25
26
27
28
29
30
31
32
            @com_port = com_port
            @baud = baud
            @parity = parity
            @stop_bits = stop_bits
            @byte_size = byte_size
            @read_bytes = read_bytes
            @serial = nil
            @exit_kiss = false
        end

        protected
        def read_interface
            read_data = @serial.read(@read_bytes)
33
            if read_data
34
                return read_data.chars.map { |c| c.ord }
35
36
37
            else
                return nil
            end
38
39
40
41
42
43
44
45
46
        end

        protected
        def write_interface(data)
            @serial.write(data)
        end

        public
        def connect(mode_init=nil, **kwargs)
47
48
            super

49
50
51
52
53
54
55
56
57
58
59
60
            @serial = SerialPort.new(@com_port, @baud, @byte_size, @stop_bits, @parity)
            @serial.read_timeout = SERIAL_READ_TIMEOUT

            if mode_init
                @serial.write(mode_init)
                @exit_kiss = true
            else
                @exit_kiss = false
            end

            # Previous verious defaulted to Xastir-friendly configs. Unfortunately
            # those don't work with Bluetooth TNCs, so we're reverting to None.
61
62
63
64
            if kwargs
                kwargs.each do |name,value|
                    write_setting(name, value)
                end
65
66
67
68
69
70
            end
        end

        public
        def close
            super
71

72
73
74
75
            if @exit_kiss
                write_interface(MODE_END)
            end

76
            if @serial == nil or @serial.closed?
77
78
79
80
81
82
83
                raise 'Attempting to close before the class has been started.'
            else
                @serial.close
            end
        end
    end
end