Jump to content

Problem communicating with port


Champak
 Share

Recommended Posts

I put this together for a USB bluetooth dongle to my GPS receiver. I know that general communication is taking place because I see the dongle blinking and the GPS receiver light indicating communication is blinking. However the values are not being relayed and I believe I am following the directions correctly from the place that provided the ActiveX to decode the gps signal. It was originally meant for VB, but I believe I have translated the basics correctly.

This is where the ActiveX came from: Info

This is some quick and basic communication properties: Documentation

#include <GUIConstants.au3>

$GPSTK = ObjCreate("GPSToolKitMod.GPSToolKit")
;$GPSTK.PortName = "COM1"
;$PN=$GPSTK.PortName
$GPSTK.Open("COM1");Opens Com
$GPSTK.BaudRate=4800;Gets Baud
$Baud=$GPSTK.BaudRate;Sets Baud
$FixGood=$GPSTK.FixGood;Checks the Sat Fix
$FixQuality=$GPSTK.FixQuality;Checks the Sat Quality
$Lat=$GPSTK.Latitude;Returns Latitude
$Long=$GPSTK.Longitude;Returns Longitude
$OTDTB=$GPSTK.OrigToDestTrueBearing
$Speed=$GPSTK.Speed;Gets Speed
$GPSTK.SpeedUnits=0;Sets Speed Units -- 0=mph
$SpeedUnits=$GPSTK.SpeedUnits;Gets Speed Units


$Form1 = GUICreate("MY GPS", 394, 272, 192, 125)
    $LLong = GUICtrlCreateLabel($Long, 10, 10, 100, 20)
    GUICtrlSetBkColor(-1, 0xFF0000)
    $LLat = GUICtrlCreateLabel($Lat, 10, 40, 100, 20)
    GUICtrlSetBkColor(-1, 0xFF0000)
    $LFixG = GUICtrlCreateLabel($FixGood, 10, 70, 100, 20)
    GUICtrlSetBkColor(-1, 0xFF0000)
    $LFixQ = GUICtrlCreateLabel($FixQuality, 10, 100, 100, 20)
    GUICtrlSetBkColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)

While 1
    If GuiGetMsg() = $GUI_EVENT_CLOSE Then
        $GPSTK=0;Reminder - This doesn't have to be here, but I put it in anyway to close the shell
        Exit
    EndIf

    GUICtrlSetData($LLong, $Long)
    GUICtrlSetData($LLat, $Lat)
    GUICtrlSetData($LFixG, $FixGood)
    GUICtrlSetData($LFixG, $FixQuality)
Sleep(2000)
WEnd

Can someone tell me what I am doing wrong please.

Link to comment
Share on other sites

I would do it this way to get a full log of what's happening. This also switches the GUI to event mode, and only updates data that has changed:

#include <GUIConstants.au3>
#include <File.au3>

Global $LogFile = "C:\Temp\GPSTK.log"
Global $GPSTK, $Baud, $SpeedUnits, $FixGood, $FixQuality, $Lat, $Long, $OTDTB, $Speed
Global $Form1, $LLong, $LLat, $LFixG, $LFixQ, $iTimer

; Connect to GPS
$GPSTK = ObjCreate("GPSToolKitMod.GPSToolKit")
If IsObj($GPSTK) Then
    _FileWriteLog($LogFile, "Object created: $GPSTK = " & ObjName($GPSTK))
Else
    _FileWriteLog($LogFile, "Failed to create object $GPSTK.")
    MsgBox(16, "Error", "Failed to create object $GPSTK.")
    Exit
EndIf

; Set baud rate
$Baud = 4800
_FileWriteLog($LogFile, "Setting baud rate to: " & $Baud)
$GPSTK.Open("COM1") ; Opens Com
$GPSTK.BaudRate = $Baud ; sets Baud
$Baud = $GPSTK.BaudRate ; gets Baud
_FileWriteLog($LogFile, "Baud rate is now: " & $Baud)

; Set speed units
_FileWriteLog($LogFile, "Setting speed units to 0 (mph)")
$GPSTK.SpeedUnits = 0 ; Sets Speed Units -- 0=mph
$SpeedUnits = $GPSTK.SpeedUnits ; Gets Speed Units
_FileWriteLog($LogFile, "Speed units ($SpeedUnits) = " & $SpeedUnits)

; Create GUI
Opt("GuiOnEventMode", 1) ; Set event mode for GUI
$Form1 = GUICreate("MY GPS", 394, 272, 192, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

$LLong = GUICtrlCreateLabel($Long, 10, 10, 100, 20)
GUICtrlSetBkColor(-1, 0xFF0000)
$LLat = GUICtrlCreateLabel($Lat, 10, 40, 100, 20)
GUICtrlSetBkColor(-1, 0xFF0000)
$LFixG = GUICtrlCreateLabel($FixGood, 10, 70, 100, 20)
GUICtrlSetBkColor(-1, 0xFF0000)
$LFixQ = GUICtrlCreateLabel($FixQuality, 10, 100, 100, 20)
GUICtrlSetBkColor(-1, 0xFF0000)
_UpdateGUI()
GUISetState(@SW_SHOW)

$iTimer = TimerInit()
While 1
    If TimerDiff($iTimer) >= 2000 Then
        $iTimer = TimerInit()
        _UpdateGUI()
    EndIf
    Sleep(20)
WEnd

Func _UpdateGUI()
    Local $ReadFixGood, $ReadFixQuality, $ReadLat, $ReadLong, $ReadOTDTB, $ReadSpeed
    $ReadFixGood = $GPSTK.FixGood ; Checks the Sat Fix
    $ReadFixQuality = $GPSTK.FixQuality ; Checks the Sat Quality
    $ReadLat = $GPSTK.Latitude ; Returns Latitude
    $ReadLong = $GPSTK.Longitude ; Returns Longitude
    $ReadOTDTB = $GPSTK.OrigToDestTrueBearing
    $ReadSpeed = $GPSTK.Speed ; Gets Speed
    _FileWriteLog($LogFile, "Data read from GPS:" & @CRLF & _
            @TAB & "Fix = " & $ReadFixGood & @CRLF & _
            @TAB & "Fix Quality = " & $ReadFixQuality & @CRLF & _
            @TAB & "Lat = " & $ReadLat & @CRLF & _
            @TAB & "Long = " & $ReadLong & @CRLF & _
            @TAB & "OTDTB = " & $ReadOTDTB & @CRLF & _
            @TAB & "Speed = " & $ReadSpeed)
    
    If $ReadLong <> GUICtrlRead($LLong) Then GUICtrlSetData($LLong, $ReadLong)
    If $ReadLat <> GUICtrlRead($LLat) Then GUICtrlSetData($LLat, $ReadLat)
    If $ReadFixGood <> GUICtrlRead($LFixG) Then GUICtrlSetData($LFixG, $ReadFixGood)
    If $ReadLat <> GUICtrlRead($LFixQ) Then GUICtrlSetData($LFixQ, $ReadFixQuality)
EndFunc   ;==>_UpdateGUI

Func _Quit()
    $GPSTK = 0 ; Remove object reference
    Exit
EndFunc   ;==>_Quit

You would also eventually want to detect changes like when the GPS loses fix, etc.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks. Could you help me understand what exactly is happening here? Besides logging what is happening, and putting the updating in a function, I don't see/"understand" what you did that's different from what I did to make you get the information. Odd question in light of yours being there to see the difference, but exactly why/where in my script did it fail?

Also:

Why did you do the following instead of doing Sleep(2000)

$iTimer = TimerInit()
While 1
    If TimerDiff($iTimer) >= 2000 Then
        $iTimer = TimerInit()
        _UpdateGUI()
    EndIf
    Sleep(20)
WEnd

Does this "<>" mean something different? Like 'if this is "different" then do this'.

I did put in a check to see if the fix is lost, but what I want to do is test if the device is turned off and/or disconnected from the port.

Link to comment
Share on other sites

Thanks. Could you help me understand what exactly is happening here? Besides logging what is happening, and putting the updating in a function, I don't see/"understand" what you did that's different from what I did to make you get the information. Odd question in light of yours being there to see the difference, but exactly why/where in my script did it fail?

Once it gets to the While/WEnd loop, your version keeps writing the data (i.e. $Lat and $Long) to the controls (i.e. $LLat and $LLong) over and over, but the data variables never get updated. It's the SAME data over and over, nothing updates it, so you don't see the changes.

Also:

Why did you do the following instead of doing Sleep(2000)

$iTimer = TimerInit()
While 1
    If TimerDiff($iTimer) >= 2000 Then
        $iTimer = TimerInit()
        _UpdateGUI()
    EndIf
    Sleep(20)
WEnd
You should avoid long sleeps wherever possible. Sleep() stops the script, which means nothing else gets done (like updating $Lat and $Long). The very short Sleep(20) keeps the script from hogging the CPU. This timer loop can update/do-something-else every 20 milliseconds, which means I can add more functionality to the script later.

Does this "<>" mean something different? Like 'if this is "different" then do this'.

The operator <> simply means "not equal to" for all boolean/string/numeric compares.

I did put in a check to see if the fix is lost, but what I want to do is test if the device is turned off and/or disconnected from the port.

AutoIt can use COM Events, and it's possible that COM object you are using fires an event when the device loses lock, or is powered off or unplugged. You'll have to dig into their documentation to learn more.

Good luck with it.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...