COM3 Problem

Started by jxpicciri2000 March 15, 2005

I've used COM3 successfully before so I think I understand the
basics however I'm think I'm having a problem with the code below. I
say "think" because the initial testing is without any COM on the
input line, I just wanted to test the looping. I thought there were
sufficient safeguards to prevent problems but apparently not. The
code not only hangs up erratically but doesn't loop correctly which
sounds like some variable or address is getting clobbered. For
instance, the code will print the initial "start" several times
without printing "end".
--
Option Explicit

Public Const Behave1 As Byte = 20
Public Const Behave2 As Byte = 19
Public Const Behave3 As Byte = 18
Public Const Behave4 As Byte = 17
Public Const Behave5 As Byte = 16
Public Const Behave6 As Byte = 15
Public Const Behave7 As Byte = 14
Public Const Behave8 As Byte = 13

Public Const TurnPin As Byte = 26
Public Const SpeedPin As Byte = 27

Public Speed as Integer, Turn as Integer
Public InQue(1 to 25) as Byte, OutQue (1 to 10) as Byte

Sub Main()
Dim Success as Boolean

Do
Call Delay (0.5)
Debug.Print "start"
Call Behaviors(Success)
Debug.Print "end"
Loop

End Sub

'------------------------

Sub Behaviors(ByRef Success as Boolean)
Dim SpeedValue as Byte, TurnValue as Byte
Dim BehavePin as Byte, BehaChr As Byte, Dummy as Byte
Dim J as Byte, Interval as Single
Dim BInputs as Byte, PTest as Byte, Priority as Byte
Const Mask as Byte = bx00000001

Interval = 0.020

' Read PortA pins and find lowest bit set high

BInputs = Register.PinA
If (BInputs = 0) Then
Success = False
Exit Sub
Else
For J = 0 to 7
Priority = J
PTest = BInputs AND Mask
If PTest = 1 Then
Exit For
Else
BInputs = BInputs\2
End If
Next
End If

BehavePin = 13 + Priority

Call DefineCom3 (BehavePin, 0, bx1000_1000)
Call OpenCom (3, 9600, InQue, OutQue)

' Do While StatusQueue(InQue) 'flush input queue
' Call GetQueue(InQue, Dummy, 1)
' Loop

Call PutTime(0,0,0.0)
Call PulseOut (BehavePin, 0.001, 0)

Do Until (Timer() >= Interval)
If StatusQueue(InQue) Then
Call GetQueue(InQue, BehaChr, 1)
If (BehaChr = ASC("$")) Then
Call GetQueue(InQue, SpeedValue, 1)
Call GetQueue(InQue, TurnValue, 1)
Success = True
Exit Sub
End If
End If
Loop
Success = False

Debug.Print " Speed = "; CStr(Speed); " Turn = "; CStr(Turn)
Debug.Print

End Sub
--

John-



--- In , "jxpicciri2000" <jpicciri@r...> wrote:
> [...] The code not only hangs up erratically but doesn't loop
> correctly which sounds like some variable or address is getting
> clobbered. [...]

You forgot to call OpenQueue() on the two queues to initialize them.
You must always do this before using a queue, e.g. passing it to
OpenComm(). Without this, the queue's state information is bogus and
the results are undefined.

Don


The open com and define com need only be done once, before your do loop
neil
jxpicciri2000 wrote:

>
> I've used COM3 successfully before so I think I understand the
> basics however I'm think I'm having a problem with the code below. I
> say "think" because the initial testing is without any COM on the
> input line, I just wanted to test the looping. I thought there were
> sufficient safeguards to prevent problems but apparently not. The
> code not only hangs up erratically but doesn't loop correctly which
> sounds like some variable or address is getting clobbered. For
> instance, the code will print the initial "start" several times
> without printing "end".
> --
> Option Explicit
>
> Public Const Behave1 As Byte = 20
> Public Const Behave2 As Byte = 19
> Public Const Behave3 As Byte = 18
> Public Const Behave4 As Byte = 17
> Public Const Behave5 As Byte = 16
> Public Const Behave6 As Byte = 15
> Public Const Behave7 As Byte = 14
> Public Const Behave8 As Byte = 13
>
> Public Const TurnPin As Byte = 26
> Public Const SpeedPin As Byte = 27
>
> Public Speed as Integer, Turn as Integer
> Public InQue(1 to 25) as Byte, OutQue (1 to 10) as Byte
>
> Sub Main()
> Dim Success as Boolean
>
> Do
> Call Delay (0.5)
> Debug.Print "start"
> Call Behaviors(Success)
> Debug.Print "end"
> Loop
>
> End Sub
>
> '------------------------
>
> Sub Behaviors(ByRef Success as Boolean)
> Dim SpeedValue as Byte, TurnValue as Byte
> Dim BehavePin as Byte, BehaChr As Byte, Dummy as Byte
> Dim J as Byte, Interval as Single
> Dim BInputs as Byte, PTest as Byte, Priority as Byte
> Const Mask as Byte = bx00000001
>
> Interval = 0.020
>
> ' Read PortA pins and find lowest bit set high
>
> BInputs = Register.PinA
> If (BInputs = 0) Then
> Success = False
> Exit Sub
> Else
> For J = 0 to 7
> Priority = J
> PTest = BInputs AND Mask
> If PTest = 1 Then
> Exit For
> Else
> BInputs = BInputs\2
> End If
> Next
> End If
>
> BehavePin = 13 + Priority
>
> Call DefineCom3 (BehavePin, 0, bx1000_1000)
> Call OpenCom (3, 9600, InQue, OutQue)
>
> ' Do While StatusQueue(InQue) 'flush
> input queue
> ' Call GetQueue(InQue, Dummy, 1)
> ' Loop
>
> Call PutTime(0,0,0.0)
> Call PulseOut (BehavePin, 0.001, 0)
>
> Do Until (Timer() >= Interval)
> If StatusQueue(InQue) Then
> Call GetQueue(InQue, BehaChr, 1)
> If (BehaChr = ASC("$")) Then
> Call GetQueue(InQue, SpeedValue, 1)
> Call GetQueue(InQue, TurnValue, 1)
> Success = True
> Exit Sub
> End If
> End If
> Loop
> Success = False
>
> Debug.Print " Speed = "; CStr(Speed); " Turn = "; CStr(Turn)
> Debug.Print
>
> End Sub
> --
>
> John- > *
> click here
> <http://www.netflix.com/Default?mqso`190075" target="_blank" rel="nofollow">http://us.ard.yahoo.com/SIG92t2910/M)8184.6018725.7038619.3001176/D=groups/S06554205:HM/EXP10996420/A%93423/R=0/SIGel9gslf/*http://www.netflix.com/Default?mqso`190075 >
>
> *>.






Groan - thanks. Sorry to clutter the list with this
oversight.
I did try to find the OpenComEx.bas file referenced in
the manual but couldn't locate it.

John-

--- In , "Don Kinzer" <dkinzer@e...> wrote:
>
> --- In , "jxpicciri2000" <jpicciri@r...> wrote:
> > [...] The code not only hangs up erratically but doesn't loop
> > correctly which sounds like some variable or address is getting
> > clobbered. [...]
>
> You forgot to call OpenQueue() on the two queues to initialize them.
> You must always do this before using a queue, e.g. passing it to
> OpenComm(). Without this, the queue's state information is bogus and
> the results are undefined.
>
> Don