-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComArduino2.rb
executable file
·160 lines (103 loc) · 2.97 KB
/
ComArduino2.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env ruby
# 19 July 2014
# this is designed to work with ... ArduinoPC2.ino ...
# this receives data prefixed by < and terminated by >
# this version uses JSSC - Java Simple Serial Connector - rather than RxTx
# RXTX seems to have disappeared from the web
# apparently all the necessary binaries are included in the Jar
# note that JSSC returns a signed java byte - hence & 0xFF to convert to an unsigned value
# also note the use of .to_java_bytes to convert the JRuby string into a Java byte array
#============================
def waitForArduino
# wait until the Arduino sends something - allows time for Arduino reset
msg = ""
while msg.include?("Arduino is ready") != true
while $sp.getInputBufferBytesCount == 0
end
msg = recvFromArduino
end
puts msg
puts "========="
end
#=====================================
def sendToArduino(sendStr)
$sp.writeBytes(sendStr.to_java_bytes)
end
#======================================
def recvFromArduino
ck = ""
x = 32 # any value that is not an end- or $startMarker
byteCount = -1 # to allow for the fact that the last increment will be one too many
# wait for the start character
while x != $startMarker
x = $sp.readBytes(1)[0] & 0xFF
end
# save data until the end marker is found
while x != $endMarker
if x != $startMarker
ck = ck + x.chr
byteCount += 1
end
x = $sp.readBytes(1)[0] & 0xFF
end
return ck
end
#=====================================
def setupStuff
$LOAD_PATH << Dir.pwd
require 'java'
require 'lib/jssc.jar'
java_import('jssc.SerialPort')
java_import('jssc.SerialPortException')
end
#==================
def openSerial
baudRate = 9600
serialPort = "/dev/ttyS80"
$sp = SerialPort.new(serialPort)
$sp.openPort
$sp.setParams(baudRate, 8, 1, 0)
at_exit{ $sp.closePort if $sp.isOpened }
puts "Serial port #{serialPort} opened"
puts "Baud Rate #{baudRate}"
end
#=================
def runTest(td)
numLoops = td.size
waitingForReply = false
n = 0
while n < numLoops
teststr = td[n]
if waitingForReply == false
sendToArduino(teststr)
puts "Sent from PC -- LOOP NUM #{n} TEST STR #{teststr}"
waitingForReply = true
end
if waitingForReply == true
while $sp.getInputBufferBytesCount == 0
end
dataRecvd = recvFromArduino
puts "Reply Received #{dataRecvd}"
n += 1
waitingForReply = false
puts "==========="
end
sleep 5
end # while n < numLoops
end
#======================================
# the program starts here
puts
puts
setupStuff
openSerial
$startMarker = 60
$endMarker = 62
waitForArduino
testData = []
testData[0] = "<LED1,200,0.2>"
testData[1] = "<LED1,800,0.7>"
testData[2] = "<LED2,800,0.5>"
testData[3] = "<LED2,200,0.2>"
testData[4] = "<LED1,200,0.7>"
runTest(testData)