-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_serial.rb
40 lines (32 loc) · 1.22 KB
/
test_serial.rb
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require './helpers.rb'
require './requests.rb'
require "serialport"
new_request = ::ArduinoGateway::RestfulRequest.new(-1, "GET /json\r\n\r\n ", ip:"usbserial-A6008kUs", port: 0)
arduino_connection = Thread.new(new_request) do |request|
Thread.current[:response] = ""
serial_list = `ls /dev/tty.*`.split("\n")
port_str = serial_list.find{ |n| n.include?("usbserial-A6008kUs") }
baud_rate, data_bits, stop_bits, parity = 4800, 8, 1, SerialPort::NONE
puts "Selected Serial Device: #{port_str} from full list: #{serial_list}"
serial_connection = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
serial_connection.puts "#{request.restful_request}\r\n\r"
puts "making serial request: #{request.restful_request}\r\n\r"
timed_out = false
# timer = ArduinoGateway::Helpers::Timer.get(3) do
# puts "setting timed_out to true"
# timed_out = true
# serial_connection.puts
# serial_connection.close
# end
response = ""
while !timed_out do
if response = serial_connection.gets
Thread.current[:response] = Thread.current[:response].to_s + response
puts response
end
end
puts Thread.current[:response]
end # arduino_connection thread
arduino_connection.join
loop do
end