Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 15:35 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Trapping Rs485 Packetts

Author Message
Zonker

Guru

Joined: 18/08/2012
Location: United States
Posts: 761
Posted: 05:38pm 24 Nov 2012
Copy link to clipboard 
Print this post

Good evening all..!

I was wondering if anyone had example code that could receive incoming Rx data for the MM. I tryed writing one myself, but can't seem to work out the "cr-lf" detector.

To get bytes from the buffer, I tried this...

getbyte:
buffbytes = 0
While buffbytes = 0
buffbytes = Loc(#1)
Wend
rxbyte$ = Input$(1, #1)
Return


Kinda hackie, but seems to be working...

I have the first MM sending out packetts consisting of this type protocoll

#1Edddd(cr) (lf)

# = start byte of packett
1 = address byte of the reciever (1-9)
E = scale type to display with (CHT, EGT, ect..)
dddd = data value of the instrument (could be 1 digit or up to 4 digits)
(cr) (lf) = end of packett bytes

Trapping the first part of the packett went ok, but getting the variable length data area is where I need the help... I was trying to use the VAL command to read in the ASCII represented instrument data. It would be looking for ASCII digits for the numbers 0-9 (ASCII hex 30-39). I was hoping to use this function to reconstruct the number sent. The first byte would be checked and stored as a number type variable. the next byte would be checked and if valid, you would multiply the first number stored by 10 and add in the second byte to form the new number. This would continue until you got to the end of the packett. But, I can't figure out how to detect the end of the packett bytes. The VAL command does have an error return value, but it returns 0 on error... 0 is a valid data character it's returning normally. If the data is "out of range" how would you know..? Is there a way to check for a non printable character in a string type variable.?

This is probably just another "Nubbie" thing...!!

Thanks for any help with this..!

P.S. - some kind of data packett check byte should be added..
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:44pm 24 Nov 2012
Copy link to clipboard 
Print this post

You want to check with chr$() or asc()
For numbers you check like this:
If rxbyte$ >= "0" and rxbytes$ <= "9"

To check for a carriage return use:
if rxbyte$ = chr$(13)
linefeed:
if rxbyte$= chr$(10)

the same but then with the asc() function:
numbers: if asc(rxbyte$) >= 48 and asc(rxbyte$) <= 57
carriage return: if asc(rxbyte$) = 13
linefeed: if asc(rxbyte$) = 10

I like "protocols" that can be read easy by humans.
Yours is fixed position so you need to count the bytes you are reading. You process the rxbyte$ according to its position.
For the value you first collect the digits combine them into a string and then do a val() of that string to get the number.

Microblocks. Build with logic.
 
ajkw
Senior Member

Joined: 29/06/2011
Location: Australia
Posts: 290
Posted: 09:26pm 24 Nov 2012
Copy link to clipboard 
Print this post

maybe make your protocol fixed length.

#1Edddd(cr) (lf)

rx$ = "#1E0234"

dddd = val(mid$(rX$,4,4))


and dddd will = 234
 
Zonker

Guru

Joined: 18/08/2012
Location: United States
Posts: 761
Posted: 09:38pm 24 Nov 2012
Copy link to clipboard 
Print this post

Ausome..!!! All is now working correctly..

I have the first MM connected to 6 MAX31855 temp IC's and creating a VGA "Bar-Guage" style readout with color temp ranges controled bay a simple setup program. The first MM also sends out the IC's data to the second MM connected to a small stepper motor based guage needle driver. This can be used as a remote guage readout mounted elsware (sweet) ..!

Guage driver code - (sofar)


For N=17 To 20: Pin(N)=0: SetPin N,8: Next N ' setup pins as output LOW
N=0: S=0: coilstate=1: Open "com1:9600, 1024" As #1

Print @(300,200) S
Print @(300,220) coilstate
Print "homeing.."
N = 655: GoSub moveguage
N = 0: GoSub moveguage

Do
' If N = S Then Input "New position "; N
If N = S Then GoSub Getvalue
Print N;
GoSub moveguage
Loop

moveguage:
While S <> N
Print @(300,200) " "
Print @(300,200) S
Print @(300,220) " "
Print @(300,220) coilstate

Pin(0)=1
If S <> N Then
If S > N Then coilstate = coilstate + 1
If S < N Then coilstate = coilstate - 1
If coilstate <= 0 Then coilstate = 4
If coilstate >= 5 Then coilstate = 1
On coilstate GoTo state1, state2, state3, state4
GoTo stepdone
state1:
Pin(20)=1: Pin(19)=1: Pin(18)=0: Pin(17)=0: GoTo stepdone
state2:
Pin(20)=0: Pin(19)=0: Pin(18)=1: Pin(17)=1: GoTo stepdone
state3:
Pin(20)=1: Pin(19)=0: Pin(18)=0: Pin(17)=0: GoTo stepdone
state4:
Pin(20)=0: Pin(19)=0: Pin(18)=1: Pin(17)=0
stepdone:
If S > N Then S = S-1
If S < N Then S = S+1
If S = N Then Pin(20)=0: Pin(18)=0
EndIf
Pin(0)=0
Wend
Return

Getvalue:
While rxbyte$ <> "#"
GoSub getbyte
Wend
Print "#"
GoSub getbyte
If rxbyte$ <> "E" Then GoTo getvalue
Print "E";
GoSub getbyte
If rxbyte$ <> "1" Then GoTo getvalue
Print "1";
GoSub getbyte
If rxbyte$ <> " " Then GoTo getvalue
Print " ";
GoSub getbyte
If rxbyte$ = Chr$(13) Or rxbyte$ = Chr$(10) Then GoTo getdone
If rxbyte$ >= "0" And rxbyte$ <= "9" Then N$ = rxbyte$
GoSub getbyte
If rxbyte$ = Chr$(13) Or rxbyte$ = Chr$(10) Then GoTo getdone
If rxbyte$ >= "0" And rxbyte$ <= "9" Then N$ = N$ + rxbyte$
GoSub getbyte
If rxbyte$ = Chr$(13) Or rxbyte$ = Chr$(10) Then GoTo getdone
If rxbyte$ >= "0" And rxbyte$ <= "9" Then N$ = N$ + rxbyte$
GoSub getbyte
If rxbyte$ = Chr$(13) Or rxbyte$ = Chr$(10) Then GoTo getdone
If rxbyte$ >= "0" And rxbyte$ <= "9" Then N$ = N$ + rxbyte$
getdone:
N = Val(n$)
Return

getbyte:
buffbytes = 0
While buffbytes = 0
buffbytes = Loc(#1)
Wend
rxbyte$ = Input$(1, #1)
Return



I used two DS3695 IC's as coil drivers. pin 20 is enable for coil one, with pin 19 being the coil direction of current flow. Same for coil two using pins 18 & 17...

now that it seems to be working, I wonder if the coils can be driven directly from the MM's I/O pins.. The coil current is very low but the 3.3v drive might not be enough... Will let ya know...

Still kinda hackie, but works... I need to study-up on more basic programming.!!

Thanks for all the help everyone..!
Edited by Zonker 2012-11-26
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024