Sign in to follow this  
Followers 0
Automaton

Cicode Com Function

2 posts in this topic

Hello I am trying to interface an anonometer to Citect via a serial port. I have configured a device called "Wind" on com 7 I am trying to use the comread() function to read in a string of text. I can then decode the text string and then convert it to integer data. When I call the Wind() function, I can see the characters in the Debug window ((TraceMsg(sGPS_Buffer) displays this)) The tag TestTag is a local String, this remains blank when I run the application (I have it displayed in a graphics page). Has anyone any advice or recommendations? All I want to do is get the ASCII from the serial port and write it to a string, If someone can help with this I would be most grateful after that I can decode it. This is what I have tried so far (taken from a GPS program I found on the net); FUNCTION Wind() INT hGPS; STRING sPort = "Wind"; hGPS = ComOpen(sPort,0); IF hGPS = -1 THEN Prompt("Cannot open port "+ sPort); ELSE ComReset(hGPS); _Gps_Read(hGPS); ComClose(hGPS); END END FUNCTION _GPS_Read(INT hGPS) INT iReading_Not_valid = 1; INT iGPS_Length; INT iGPS_Error; INT iCR_Position; STRING sGPS_Buffer; STRING sGPS_String; INT i; INT cError = 1; WHILE iReading_Not_valid DO ! must set length as read modifies iGPS_Length = 128; iGPS_Error = ComRead(hGPS,sGPS_Buffer,iGPS_Length,60); TraceMsg(sGPS_Buffer); TestTag = sGPS_Buffer; IF iGPS_Error <> 7 THEN IF iGPS_Error = cError THEN Prompt("Error from port "+ IntToStr(iGPS_Error)); ComReset(hGPS); ELSE iCR_Position = StrSearch(0,sGPS_Buffer,CharToStr(10)); IF iCR_Position <> -1 THEN ! remove control characters and spaces from string sGPS_String = StrTrim(StrClean(sGPS_Buffer)); ! process GPS string iReading_Not_valid = _GPS_Process(sGPS_String); END END END END END INT FUNCTION _GPS_Process(STRING sGPS_String) REAL rTime; INT iGPS_Time_Position; STRING sHH; STRING sMM; STRING sSS; IF StrSearch(0,sGPS_String,"$GPVTG") <> -1 THEN Prompt("GPS Valid sattelite fixing obtained"); GPS_Time_Fix = 0; iGPS_Time_Position = StrSearch(0,sGPS_String,"$GPGGA"); IF iGPS_Time_Position <> -1 THEN ! get greenwich mean time sHH = StrMid(sGPS_String,iGPS_Time_Position + 7,2); sMM = StrMid(sGPS_String,iGPS_Time_Position + 9,2); sSS = StrMid(sGPS_String,iGPS_Time_Position + 11,2); rTime = sHH+sMM+sSS; RETURN 0; END END RETURN 1;

Share this post


Link to post
Share on other sites
Hello I made a bit of progress on this, most of the problem were due to my poor understanding of How the Com functions operate. This might be of use to someone out there as the help files are not great on this subject. It is a test routine reading time from an anonometer ( it uses NMEA ASCII) Call ReadVal() as an event every 2 sec ComData is a local variable string and ComVal is the same value as an integer If anyone has any recommendations on improving this code I would be interested in hearing from you To test copy this into hyperterminal: $GPGGA,183506,5320.8532,N,00612 FUNCTION ReadVal() INT A; STRING SRES,BufRES,VarRES,Char,Temp; ComData = ""; INT Length,hport,OKRES; hPort = ComOpen("Wind",0); OKRES = 0; WHILE OKRES = 0 DO Length = 128; ComRead(hport,BufRES,Length,5); TraceMsg(BufRES); IF Length > 0 THEN Temp = StrTrim(StrClean(BufRES)); OKRES = Process(Temp); END END ComClose(hport); END INT FUNCTION Process(STRING S) INT Pos; STRING A,B,C; Pos = StrSearch(0,S,"$GPGGA"); IF Pos <> -1 THEN A = StrMid(S,Pos + 7,4); ComData = A; IF A <> "" THEN ComVal = StrToInt(A); END RETURN 1; ELSE RETURN 0; END END

Share this post


Link to post
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
Sign in to follow this  
Followers 0