Husker_Jason Posted June 28, 2023 Posted June 28, 2023 I am trying to learn how to use the COM Port UDF provided by: Serial port UDF / COM port UDF by MazeM That said, I have it connected to a GPS serial port and am exploring how the different commands work to try and send it data and get that data back. I eventually figured out that to receive data, I had to put a sleep command in the loop else I kept getting -1 as a result when I tried to read a character in. Once I got by that, I seemed to be doing fine as I could read each character in and then break it into lines of text based on the Chr(13) being sent. I then moved on to how to send commands to the receiver so I could configure it. By sending a "FRESET" command, the receiver resets to its default configuration. I can then send it a command "LOG GPGGA ONTIME 1" to configure the GPS port to start sending location messages 1 times a second. I wrote a script to test things and output the data to my console to see how things work. Everything was going fine until I tried to detect a specific message sent from the receiver to tell me that the command has been accepted and it is ready to receive the next command. If I watch the response for setting the "FRESET" command with Putty, I see the receiver gives me the following response after it is complete. FREESET <OK [COM1][COM1] The "<OK" and first "[COM1]" response are pretty much instantons. The second one shows up a few seconds later as the receiver resets and comes back online. I planned on watching for the second "[COM1]" to know when to send my next command. However, when I try to detect it, the second "[COM1]" is behaving different as I read it into AutoIt. My script is reading each character using the "_COM_ReadChar()" function provided by @MazeM and @Filipp_G. I am ignoring anything that comes back as -1. If it a character, I then write the character to the console followed by the ASCII reference, a counter, and then the conjugation of all the characters that have been sent since the last Chr(13). This allows me to see everything the port is bringing in and track how my script is working (or not). The following represents the Output from the console. expandcollapse popupCOM Port Def = baud=9600 parity=N data=8 stop=1 COM_OpenPort Results = 0x000000000000029C SENT: LOG GPGGA ONTIME 1 0 : : 13 RECIEVED: $X = 0 $Y = 0 Line: 0 : : 10 0 : <: 60 100 : < 1 : O: 79 100 : <O 2 : K: 75 100 : <OK 3 : : 13 RECIEVED: $X = 1 $Y = 3 Line: <OK 0 : : 10 0 : [: 91 200 : [ 1 : C: 67 200 : [C 2 : O: 79 200 : [CO 3 : M: 77 200 : [COM 4 : 1: 49 200 : [COM1 5 : ]: 93 200 : [COM1] RECIEVED [COM]: $X = 2 $Y = 6 Line: [COM1] 0 : $: 36 200 : $ 1 : G: 71 200 : $G 2 : P: 80 200 : $GP 3 : G: 71 200 : $GPG 4 : G: 71 200 : $GPGG 5 : A: 65 200 : $GPGGA 6 : ,: 44 200 : $GPGGA, 7 : 2: 50 200 : $GPGGA,2 8 : 2: 50 200 : $GPGGA,22 9 : 3: 51 200 : $GPGGA,223 10 : 4: 52 200 : $GPGGA,2234 11 : 0: 48 200 : $GPGGA,22340 12 : 7: 55 200 : $GPGGA,223407 13 : .: 46 200 : $GPGGA,223407. 14 : 0: 48 200 : $GPGGA,223407.0 15 : 0: 48 200 : $GPGGA,223407.00 16 : ,: 44 200 : $GPGGA,223407.00, 17 : 3: 51 200 : $GPGGA,223407.00,3 18 : 6: 54 200 : $GPGGA,223407.00,36 19 : 1: 49 200 : $GPGGA,223407.00,361 20 : 3: 51 200 : $GPGGA,223407.00,3613 21 : .: 46 200 : $GPGGA,223407.00,3613. 22 : 3: 51 200 : $GPGGA,223407.00,3613.3 23 : 3: 51 200 : $GPGGA,223407.00,3613.33 24 : 1: 49 200 : $GPGGA,223407.00,3613.331 25 : 7: 55 200 : $GPGGA,223407.00,3613.3317 26 : ,: 44 200 : $GPGGA,223407.00,3613.3317, 27 : N: 78 200 : $GPGGA,223407.00,3613.3317,N 28 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N, 29 : 1: 49 200 : $GPGGA,223407.00,3613.3317,N,1 30 : 1: 49 200 : $GPGGA,223407.00,3613.3317,N,11 31 : 9: 57 200 : $GPGGA,223407.00,3613.3317,N,119 32 : 2: 50 200 : $GPGGA,223407.00,3613.3317,N,1192 33 : 1: 49 200 : $GPGGA,223407.00,3613.3317,N,11921 34 : .: 46 200 : $GPGGA,223407.00,3613.3317,N,11921. 35 : 5: 53 200 : $GPGGA,223407.00,3613.3317,N,11921.5 36 : 8: 56 200 : $GPGGA,223407.00,3613.3317,N,11921.58 37 : 0: 48 200 : $GPGGA,223407.00,3613.3317,N,11921.580 38 : 4: 52 200 : $GPGGA,223407.00,3613.3317,N,11921.5804 39 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804, 40 : W: 87 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W 41 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W, 42 : 1: 49 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1 43 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1, 44 : 0: 48 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,0 45 : 6: 54 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06 46 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06, 47 : 1: 49 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1 48 : .: 46 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1. 49 : 7: 55 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7 50 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7, 51 : 8: 56 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,8 52 : 0: 48 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80 53 : .: 46 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80. 54 : 8: 56 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.8 55 : 1: 49 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81 56 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81, 57 : M: 77 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M 58 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M, 59 : -: 45 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,- 60 : 3: 51 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-3 61 : 2: 50 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32 62 : .: 46 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32. 63 : 6: 54 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.6 64 : 0: 48 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60 65 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60, 66 : M: 77 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M 67 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M, 68 : ,: 44 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M,, 69 : *: 42 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M,,* 70 : 6: 54 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M,,*6 71 : 7: 55 200 : $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M,,*67 72 : : 13 RECIEVED: $X = 3 $Y = 72 Line: $GPGGA,223407.00,3613.3317,N,11921.5804,W,1,06,1.7,80.81,M,-32.60,M,,*67 0 : : 10 0 : $: 36 300 : $ 1 : G: 71 300 : $G 2 : P: 80 300 : $GP 3 : G: 71 300 : $GPG 4 : G: 71 300 : $GPGG 5 : A: 65 300 : $GPGGA 6 : ,: 44 300 : $GPGGA, 7 : 2: 50 300 : $GPGGA,2 8 : 2: 50 300 : $GPGGA,22 9 : 3: 51 300 : $GPGGA,223 10 : 4: 52 300 : $GPGGA,2234 11 : 0: 48 300 : $GPGGA,22340 12 : 8: 56 300 : $GPGGA,223408 13 : .: 46 300 : $GPGGA,223408. 14 : 0: 48 300 : $GPGGA,223408.0 15 : 0: 48 300 : $GPGGA,223408.00 16 : ,: 44 300 : $GPGGA,223408.00, 17 : 3: 51 300 : $GPGGA,223408.00,3 18 : 6: 54 300 : $GPGGA,223408.00,36 19 : 1: 49 300 : $GPGGA,223408.00,361 20 : 3: 51 300 : $GPGGA,223408.00,3613 21 : .: 46 300 : $GPGGA,223408.00,3613. 22 : 3: 51 300 : $GPGGA,223408.00,3613.3 23 : 3: 51 300 : $GPGGA,223408.00,3613.33 24 : 1: 49 300 : $GPGGA,223408.00,3613.331 25 : 8: 56 300 : $GPGGA,223408.00,3613.3318 26 : ,: 44 300 : $GPGGA,223408.00,3613.3318, 27 : N: 78 300 : $GPGGA,223408.00,3613.3318,N 28 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N, 29 : 1: 49 300 : $GPGGA,223408.00,3613.3318,N,1 30 : 1: 49 300 : $GPGGA,223408.00,3613.3318,N,11 31 : 9: 57 300 : $GPGGA,223408.00,3613.3318,N,119 32 : 2: 50 300 : $GPGGA,223408.00,3613.3318,N,1192 33 : 1: 49 300 : $GPGGA,223408.00,3613.3318,N,11921 34 : .: 46 300 : $GPGGA,223408.00,3613.3318,N,11921. 35 : 5: 53 300 : $GPGGA,223408.00,3613.3318,N,11921.5 36 : 8: 56 300 : $GPGGA,223408.00,3613.3318,N,11921.58 37 : 0: 48 300 : $GPGGA,223408.00,3613.3318,N,11921.580 38 : 4: 52 300 : $GPGGA,223408.00,3613.3318,N,11921.5804 39 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804, 40 : W: 87 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W 41 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W, 42 : 1: 49 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1 43 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1, 44 : 0: 48 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,0 45 : 6: 54 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06 46 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06, 47 : 1: 49 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1 48 : .: 46 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1. 49 : 7: 55 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7 50 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7, 51 : 8: 56 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,8 52 : 1: 49 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81 53 : .: 46 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81. 54 : 0: 48 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.0 55 : 4: 52 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04 56 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04, 57 : M: 77 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M 58 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M, 59 : -: 45 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,- 60 : 3: 51 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-3 61 : 2: 50 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32 62 : .: 46 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32. 63 : 6: 54 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.6 64 : 0: 48 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60 65 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60, 66 : M: 77 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M 67 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M, 68 : ,: 44 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M,, 69 : *: 42 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M,,* 70 : 6: 54 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M,,*6 71 : B: 66 300 : $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M,,*6B 72 : : 13 RECIEVED: $X = 4 $Y = 72 Line: $GPGGA,223408.00,3613.3318,N,11921.5804,W,1,06,1.7,81.04,M,-32.60,M,,*6B SENT: FRESET 0 : : 13 RECIEVED: $X = 0 $Y = 0 Line: 0 : : 10 0 : <: 60 400 : < 1 : O: 79 400 : <O 2 : K: 75 400 : <OK 3 : : 13 RECIEVED: $X = 1 $Y = 3 Line: <OK 0 : : 10 0 : [: 91 500 : [ 1 : C: 67 500 : [C 2 : O: 79 500 : [CO 3 : M: 77 500 : [COM 4 : 1: 49 500 : [COM1 5 : ]: 93 500 : [COM1] RECIEVED [COM]: $X = 2 $Y = 6 Line: [COM1] 0 : 500 : 1 : [: 91 500 : 2 : C: 67 500 : 3 : O: 79 500 : 4 : M: 77 500 : 5 : 1: 49 500 : 6 : ]: 93 500 : RECIEVED [COM]: $X = 3 $Y = 7 Line: Everything is working the way I expect it, except for the very end. AutoIt is reading in the "<OK" and "[COM1]" like I expect it. Each character is added to the end of my Line variable ($sCOM_StringIn) and I see the total of my line variable. However, after the pause between the first and second [COM1], the console shows that I'm still receiving the characters, but they are not being handled the same. At this point, it stops adding the characters to the end of my $sCOM_StringIn. In fact, it looks like it is skipping my "Else" statement altogether where it adds the characters, writes results to the console, and adds the CR to the end of that line. The other weird part is that once the serial port sees all the characters for "[COM1]", the If statement looking in the $aCOM_StringIn variable for that string sees it and prints it's command to the screen. However, the "[COM1]" text that it supposedly detected, doesn't get printed. Here is the code I used below. expandcollapse popup#include '..\FunctionLib\COM_Maze\ComUDF.au3' Local $sCOM_Port = "COM9" Local $sCOM_Baud = "9600" Local $sCOM_Data = "8" Local $sCOM_Parity = "N" Local $sCOM_Stop = "1" Local $hCOM_Port Local $sCOM_StringIn Local $sCOM_StringBit Local $sCOM_StringOut = "FRESET" ;COM1 baud=9600 parity=n data=8 stop=1 to=off xon=off odsr=off octs=off dtr=off rts=off idsr=off Local $sCOM_Def = "baud=" & $sCOM_Baud & " " & _ "parity=" & $sCOM_Parity & " " & _ "data=" & $sCOM_Data & " " & _ "stop=" & $sCOM_Stop ConsoleWrite("COM Port Def = " & $sCOM_Def & @CR) Local $hCOM_Port = _COM_OpenPort($sCOM_Port & $sCOM_Def) ConsoleWrite("COM_OpenPort Results = " & $hCOM_Port & @CR) $Y = 0 $X = 0 $Z = 0 _COM_SendString($hCOM_Port, "LOG GPGGA ONTIME 1" & @CRLF, 1) ConsoleWrite("SENT: " & "LOG GPGGA ONTIME 1" & @CR) While 1 $sCOM_StringBit = _COM_ReadChar($hCOM_Port) If Not ($sCOM_StringBit = -1) Then ConsoleWrite($Y & " : " & $sCOM_StringBit & ": " & Asc($sCOM_StringBit) & " ") If $sCOM_StringBit = Chr(13) Or $Y > 100 Then ; Carrage Return or more than 100 characters ConsoleWrite("RECIEVED: $X = " & $X & " $Y = " & $Y & " Line: " & $sCOM_StringIn & @CR) $sCOM_StringIn = "" $Y = 0 $X += 1 ElseIf $sCOM_StringBit = Chr(10) Then $Z +=100 Else $sCOM_StringIn = $sCOM_StringIn & $sCOM_StringBit ConsoleWrite($Z & " : " & $sCOM_StringIn & @CR) $Y += 1 EndIf EndIf If $X = 5 Then ; Send this once to reset the reciever _COM_SendString($hCOM_Port,$sCOM_StringOut & @CRLF, 1) _COM_ClearInputBuffer($hCOM_Port) ConsoleWrite("SENT: " & $sCOM_StringOut & @CR) $X = 0 EndIf If StringInStr($sCOM_StringIn, "[COM1]") > 0 Then ConsoleWrite("RECIEVED [COM]: $X = " & $X & " $Y = " & $Y & " Line: " & $sCOM_StringIn & @CR) $sCOM_StringIn = "" $Y = 0 $X += 1 EndIf Sleep(1) WEnd ConsoleWrite('COM_ClosePort='&_COM_ClosePort($hCOM_Port)& ' #@error = ' & @error & '#' & @CRLF) I am new to working with trying to read and send data to a COM port and do not know the details. I realize it will be hard to duplicate this error without access to the GPS receiver, but I'm hoping someone can see what I might be doing wrong with my code.
crackdonalds Posted July 6, 2023 Posted July 6, 2023 try replacing this: Else $sCOM_StringIn = $sCOM_StringIn & $sCOM_StringBit ConsoleWrite($Z & " : " & $sCOM_StringIn & @CR) $Y += 1 EndIf EndIf with this: Else $sCOM_StringIn = $sCOM_StringIn & $sCOM_StringBit $Y += 1 If StringInStr($sCOM_StringIn, "[COM1]") > 0 Then ConsoleWrite("RECIEVED [COM]: $X = " & $X & " $Y = " & $Y & " Line: " & $sCOM_StringIn & @CR) $sCOM_StringIn = "" $Y = 0 $X += 1 Else ConsoleWrite($Z & " : " & $sCOM_StringIn & @CR) EndIf EndIf
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now