-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extended_FDSerial.spin
358 lines (318 loc) · 28.9 KB
/
Extended_FDSerial.spin
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
{{
*************************************************
* Extended Full-Duplex Serial Object *
* Version 1.1.0 *
* Copyright (c) 2007, Martin Hebel *
* See end of file for terms of use. *
*************************************************
* Extended Methods for FullDuplexSerial *
* by Chip Gracey, Pallax, Inc *
* *
* Primary Author: Martin Hebel *
* Electronic Systems Technologies *
* Southern Illinois University Carbondale *
* www.siu.edu/~isat/est *
* *
* Questions? Please post on the Propeller forum *
* http://forums.parallax.com/forums/ *
*************************************************
This object extends Chip Gracey's FullDuplexSerial to allow easy reception of
multiple bytes for decimal values, hex values and strings which end with carriage returns (ASCII 13)
OR the defined delimiter character ( default is comma - , )
Use as you would FullDuplex for Rx, Tx, Start, Stop, RxTime, RxCheck, RxFlush, DEC, HEX, BIN Str
****************************************************************************************************
Adds the following:
x := Serial.RxDec Returns decimal string as value
x := Serial.RxDecTime(ms) Returns decimal string as value with timeout
x := Serial.RxHex Returns received hex string value as decimal
x := Serial.RxHexTime(ms) Returns received hex string value as decimal with timeout
Serial.RxStr(@myStr) Passes received string
Serial.RxStrTime(ms,@myStr) Passes received string with timeout
ver 1.1:
SetDelimiter(char) Sets the delimiter character, such as comma or space. it is , by deafult.
CR, or ASCII 13, will also be a delimiter in addition to this.
x := Serial.IntOfString(@myStr) Returns the integer (whole) portion of a string
"-123.456" returns -123
x := Serial.FracOfString(@myStr) Returns the fractional decimal portion of a string
"-123.456" returns 456
***************************************************************************************************
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
OBJ
Serial : "Extended_FDSerial"
Pub Start
Serial.start(31,30,0,9600) ' Rx,Tx, Mode, Baud
}}
VAR
Byte datain[16]
Byte Delimiter
OBJ
ExtSerial : "FullDuplexSerial"
Pub Start (RXpin, TXpin, Mode, Baud)
{{
Start serial driver - starts a cog
returns false if no cog available
mode bit 0 = invert rx
mode bit 1 = invert tx
mode bit 2 = open-drain/source tx
mode bit 3 = ignore tx echo on rx
Serial.Start(31,30,0, 9600)
}}
ExtSerial.Start(RXPin, TXPin, Mode, Baud)
Delimiter := ","
PUB Stop
'' See FullDuplex Serial Documentation
ExtSerial.Stop
PUB RxCheck
'' See FullDuplex Serial Documentation
return (ExtSerial.RxCheck)
PUB RxFlush
'' See FullDuplex Serial Documentation
ExtSerial.RxFlush
PUB Tx (data)
'' See FullDuplex Serial Documentation
'' Serial.tx(13) ' Send byte of data
ExtSerial.tx(data)
Pub Rx
'' See FullDuplex Serial Documentation
'' x := Serial.RX ' receives a byte of data
return (ExtSerial.rx)
PUB RxTime(ms)
'' See FullDuplex Serial Documentation
'' x:= Serial.RxTime(100) ' receive byte with 100mS timeout
return (ExtSerial.RxTime(ms))
PUB str(stringptr)
'' See FullDuplex Serial Documentation
'' Serial.str(String("Hello World")) ' transmit a string
ExtSerial.str(stringptr)
PUB dec(value)
'' See FullDuplex Serial Documentation
'' Serial.dec(1234) ' send decimal value as chracters
ExtSerial.dec(value)
PUB hex(value, digits)
'' See FullDuplex Serial Documentation
'' Serial.hex(1234,4) ' send value as hex string for 4 digits
ExtSerial.hex(value, digits)
PUB bin(value, digits)
'' See FullDuplex Serial Documentation
'' Serial.bin(32,4) ' send value as binary string for 8 digits
ExtSerial.bin(value, digits)
Pub SetDelimiter(char)
{{ Sets the delimiter value for string parsing.
comma by default.
serial.SetDelimiter(" ") 'change to a space
Also always delimits using ASCII 13 (CR)
}}
Delimiter := char
PUB rxDec : Value | place, ptr, x
{{
Accepts and returns serial decimal values, such as "1234" as a number.
String must end in a carriage return (ASCII 13)
x:= Serial.rxDec ' accept string of digits for value
}}
place := 1
ptr := 0
value :=0
dataIn[ptr] := RX
ptr++
repeat while (DataIn[ptr-1] <> 13) and (DataIn[ptr-1] <> Delimiter)
dataIn[ptr] := RX
ptr++
if ptr > 2
repeat x from (ptr-2) to 1
if (dataIn[x] => ("0")) and (datain[x] =< ("9"))
value := value + ((DataIn[x]-"0") * place)
place := place * 10
if (dataIn[0] => ("0")) and (datain[0] =< ("9"))
value := value + (DataIn[0]-48) * place
elseif dataIn[0] == "-"
value := value * -1
elseif dataIn[0] == "+"
value := value
PUB rxDecTime(ms) :Value | place, ptr, x, temp
{{
Accepts and returns serial decimal values, such as "1234" as a number
with a timeout value. No data returns -1
String must end in a carriage return (ASCII 13)
x := Serial.rxDecTime(100) ' accept data with timeout of 100mS
}}
place := 1
ptr := 0
value :=0
temp := RxTime(ms)
if temp == -1
return -1
abort
dataIn[ptr] := Temp
ptr++
repeat while (DataIn[ptr-1] <> 13) and (DataIn[ptr-1] <> Delimiter)
dataIn[ptr] := RxTime(ms)
if datain[ptr] == 255
return -1
abort
ptr++
if ptr > 2
repeat x from (ptr-2) to 1
if (dataIn[x] => ("0")) and (datain[x] =< ("9"))
value := value + ((DataIn[x]-"0") * place)
place := place * 10
if (dataIn[0] => ("0")) and (datain[0] =< ("9"))
value := value + (DataIn[0]-48) * place
elseif dataIn[0] == "-"
value := value * -1
elseif dataIn[0] == "+"
value := value
PUB rxHex :Value | place, ptr, x, temp
{{
Accepts and returns serial hexadecimal values, such as "A2F4" as a number.
String must end in a carriage return (ASCII 13)
x := Serial.rxHex ' accept string of digits for value
}}
place := 1
ptr := 0
value :=0
temp := Rx
if temp == -1
return -1
abort
dataIn[ptr] := Temp
ptr++
repeat while (DataIn[ptr-1] <> 13) and (DataIn[ptr-1] <> Delimiter)
dataIn[ptr] := Rx
if datain[ptr] == 255
return -1
abort
ptr++
if ptr > 1
repeat x from (ptr-2) to 0
if (dataIn[x] => ("0")) and (datain[x] =< ("9"))
value := value + ((DataIn[x]-"0") * place)
if (dataIn[x] => ("a")) and (datain[x] =< ("f"))
value := value + ((DataIn[x]-"a"+10) * place)
if (dataIn[x] => ("A")) and (datain[x] =< ("F"))
value := value + ((DataIn[x]-"A"+10) * place)
place := place * 16
PUB rxHexTime(ms) :Value | place, ptr, x, temp
{{
Accepts and returns serial hexadecimal values, such as "A2F4" as a number.
with a timeout value. No data returns -1
String must end in a carriage return (ASCII 13)
x := Serial.rxHexTime(100) ' accept string of digits for value with 100mS timeout
}}
place := 1
ptr := 0
value :=0
temp := RxTime(ms)
if temp == -1
return -1
abort
dataIn[ptr] := Temp
ptr++
repeat while (DataIn[ptr-1] <> 13) and (DataIn[ptr-1] <> Delimiter)
dataIn[ptr] := RxTime(ms)
if datain[ptr] == 255
return -1
abort
ptr++
if ptr > 1
repeat x from (ptr-2) to 0
if (dataIn[x] => ("0")) and (datain[x] =< ("9"))
value := value + ((DataIn[x]-"0") * place)
if (dataIn[x] => ("a")) and (datain[x] =< ("f"))
value := value + ((DataIn[x]-"a"+10) * place)
if (dataIn[x] => ("A")) and (datain[x] =< ("F"))
value := value + ((DataIn[x]-"A"+10) * place)
place := place * 16
PUB RxStr (stringptr) : Value | ptr
{{
Accepts a string of characters - up to 15 - to be passed by reference
String acceptance terminates with a carriage return or the defined delimiter character.
Will accept up to 15 characters before passing back.
Serial.Rxstr(@MyStr) ' accept
serial.str(@MyStr) ' transmit
}}
ptr:=0
bytefill(@dataIn,0,15)
dataIn[ptr] := Rx
ptr++
repeat while ((DataIn[ptr-1] <> 13) and (DataIn[ptr-1] <> Delimiter)) and (ptr < 15)
dataIn[ptr] := RX
ptr++
dataIn[ptr-1]:=0
byteMove(stringptr,@datain,16)
PUB RxStrTime (ms,stringptr) : Value | ptr, temp
{{
Accepts a string of characters - up to 15 - to be passed by reference
Allow timeout value.
String acceptance terminates with a carriage return or defined delimter character.
Will accept up to 15 characters before passing back.
Serial.RxstrTime(200,@MyStr) ' accept
serial.str(@MyStr) ' transmit
}}
ptr:=0
bytefill(@dataIn,0,15)
bytefill(stringptr,0,15)
temp := RxTime(ms)
if temp <> -1
dataIn[ptr] := temp
ptr++
repeat while (((DataIn[ptr-1] <> 13) and (DataIn[ptr-1] <> Delimiter)) and (ptr < 15))
temp := RxTime(ms)
if temp == -1
ptr++
quit
dataIn[ptr] := temp
ptr++
dataIn[ptr-1]:=0
byteMove(stringptr,@datain,16)
PUB IntOfString (stringptr): Value | negFlag
{{ Returns the integer or whole portion of a string with a decimal point.
serial.RxString(@myStr))
x := serial.IntOfString(@myStr)
"-123.456" will return -123
}}
repeat strsize(stringptr)
if byte[stringptr] == "-"
negFlag := true
if (byte[stringptr] => ("0")) and (byte[stringptr] =< ("9"))
value := value * 10 + (byte[stringptr]-"0")
if byte[stringptr] == "."
if NegFlag == true
value := value * -1
!negFlag
quit
stringptr++
if NegFlag == true
value := value * -1
!negFlag
PUB FracOfString (stringptr): Value | decFlag
{{ Returns the fractional or decimal portion of a string with a decimal point.
serial.RxString(@myStr))
x := serial.FracOfString(@myStr)
"-123.456" will return 456
}}
repeat strsize(stringptr)
if byte[stringptr] == "."
decFlag := true
if decFlag == True
if (byte[stringptr] => ("0")) and (byte[stringptr] =< ("9"))
value := value * 10 + (byte[stringptr]-"0")
stringptr++
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│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: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│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. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}