StrComp question
Hello!
I try to compare a string recieved from my gsm modem with a given DAT byte, but without luck. I think the problem is that the recieved string does not contain a terminating zero.
Can someone please give me a example how i can add that zero to my recieved string, or what can it be??
Thanks!
/Micke
I try to compare a string recieved from my gsm modem with a given DAT byte, but without luck. I think the problem is that the recieved string does not contain a terminating zero.
Can someone please give me a example how i can add that zero to my recieved string, or what can it be??
Thanks!
/Micke

Comments
Here comes the code:
var byte message[15], phonenumber[16], mess1[16], mess2[16], mess3[16], mess5[16], mess6[16], mess7[16] dat on byte "on", 0 off byte "off", 0 pub main debug.start (115200) gsm.start (26, 27, 0, 9600) waitcnt (clkfreq * 2 + cnt) dira[2] :=1 readmessage pub readmessage repeat waitcnt (clkfreq * 5 + cnt) gsm.rxflush gsm.str (string("AT+CMGR=1")) 'Read sms in first memory gsm.tx(13) 'CT debug.str (string("Get message in memory1...")) debug.newline gsm.RxStrTime(300,@mess1) gsm.RxStrTime(300,@mess2) gsm.RxStrTime(300,@mess3) gsm.RxStrTime(300,@phonenumber) gsm.RxStrTime(300,@mess5) gsm.RxStrTime(300,@mess6) gsm.RxStrTime(300,@mess7) gsm.RxStrTime(200,@message) waitcnt (clkfreq * 1 + cnt) gsm.str(string("AT+CMGD=1")) 'Delete message in first memory gsm.tx(13) waitcnt (clkfreq * 1 + cnt) if strcomp (@on, @message) on2 elseif strcomp (@off, @message) off2 debug.str(string("no message, repeat"))I think I added up 127 bytes in your buffers.
Example using a 4port serial object:
PUB ReadData | ii, val 'copy content of incoming message to an array ii := 1 response := ser.rxtime(2, 1000) serial port 2, waits here for first byte response for 1 second If response > -1 ' if something was received, get the rest of the data now Repeat 511 'look for more, else return Val := ser.rxtime(2, 2) 'read more, wait only 2 ms if no byte found If val == -1 'if the wait timed out with no data response[ii] := 0 'set this array position to a 0 quit 'quit, return, or abort... no more reading response[ii] := Val ' write to the array if data was received ii++It doesent work for me...
I have increased the "var byte message" to 127, and i have put the "bytefill(@message, 0, 127" just before the gsm.RxStr.
What is it im doing wrong, can someone please help me?!
/Micke
Now i have tried that, same result...
Here is my latest code, the message consists just of the two letters "on".
[code]
obj
gsm: "Extended_FDSerial"
debug: "Parallax Serial Terminal"
var
byte message[127], phonenumber[16], mess1[16], mess2[16], mess3[16], mess5[16], mess6[16], mess7[16]
dat
on byte "on", 0
off byte "off", 0
pub main
debug.start (115200)
gsm.start (26, 27, 0, 9600)
waitcnt (clkfreq * 2 + cnt)
dira[2] :=1
readmessage
pub readmessage
repeat
waitcnt (clkfreq * 5 + cnt)
gsm.rxflush
gsm.str (string("AT+CMGR=1")) 'Read sms in first memory
gsm.tx(13) 'CT
debug.str (string("Get message in memory1..."))
bytefill(@message, 0, 127)
debug.newline
gsm.RxStrTime(300,@mess1)
gsm.RxStrTime(300,@mess2)
gsm.RxStrTime(300,@mess3)
gsm.RxStrTime(300,@phonenumber)
gsm.RxStrTime(300,@mess5)
gsm.RxStrTime(300,@mess6)
gsm.RxStrTime(300,@mess7)
gsm.RxStrTime(200,@message)
waitcnt (clkfreq * 1 + cnt)
gsm.str(string("AT+CMGD=2")) 'Delete message in first memory
gsm.tx(13)
waitcnt (clkfreq * 1 + cnt)
debug.newline
debug.str(@mess1)
debug.newline
debug.str(@mess2)
debug.newline
debug.str(@mess3)
debug.newline
debug.str(@phonenumber)
debug.newline
debug.str(@mess5)
debug.newline
debug.str(@mess6)
debug.newline
debug.str(@mess7)
debug.newline
debug.str(string("H
if message[0] == "o" if message[1] == "n" outa[2] := 1 'assumed to be an LED??? if message[0] == "o" if message[1] == "f" if message[2] == "f" outa[2] := 0debug.str(string("H
(I cant copy and paste from Serial Terminal, i hope this will do)
var byte test1[3] pub test1[0] := "o" test1[1] := "n" test1[2] := 0 if strcomp(@test1, @test2) dosomething dat test2 byte "on", 0The only last thing I might try for some understanding is to look at each element in the var:
debug.str(@message[0])
debug.newline
debug.str(@message[1])
debug.newline
debug.str(@message[2])
debug.newline
Hopefully someone will enlighten us both.
CON _clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz _xinfreq = 5_000_000 VAR byte Str2[30] OBJ SIO : "FullDuplexSerialPlus" Pub Main waitcnt(clkfreq * 4 + cnt) SIO.StartPST(115200) repeat SIO.GetStr(@Str2) SIO.Str(string("Str2 is: ")) SIO.Str(@Str2) SIO.tx(13) if strcomp(@Str1, @Str2) SIO.Str(string("Str1 and Str2 are equal",13,13)) else SIO.Str(string("Str1 and Str2 are different",13,13)) DAT Str1 byte "Hello World", 0Results from Parallax Serial Terminal
John Abshier
My problem is why i cant parse the incomming message with the actual three given words.
debug.str (string("Get message in memory1...")) bytefill(@message, 0, 127) debug.newlineadd the following looprepeat if (c := gsm.rx) < 32 debug.char("<") debug.hex(c, 2) debug.char(">") else debug.char(c)This is simply to verify whether the command actually arrives.But in my code i have the debug.str(@message) and that give me the right result from the string (on).
Another question, is there any way to copy the result from the Serial Terminal, without using the print screen?
copy/paste is possible. Just select the area with the mouse then press ctrl+C. Once you have your clock setup sorted can you generate another data dump?
Apologies for using the wrong method names. PST uses char instead of tx (I modified the code fragment posted earlier).
Or is there any other solution to read a "part" of a string without knowing the start bit??
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 DAT message byte " Some stuff", $0A, "My received string", $00 compareTo byte "My received string", $00 VAR Byte buffer[3] OBJ pst : "Parallax Serial Terminal" PUB Main | i, isMatch pst.start(115200) Pause(500) i := 0 isMatch := false ' Find the string that starts after an <lf> 'Then compare the string to "compareTo" repeat strsize(@message) if(message[i++] == $0A) isMatch := strcomp(@message[i], @compareTo) 'Print the results pst.str(string("Have a match: ")) if(isMatch) pst.str(string("True", $0D)) else pst.str(string("False", $0D)) PRI Pause(Duration) waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt) returnpub instr(str1, str2) | len1, len2, pos, idx '' Returns position of str2 in str1 '' -- if str2 not in str1 returns -1 len1 := strsize(str1) len2 := strsize(str2) pos := -1 idx := 0 if (len1 => len2) repeat (len1 - len2 + 1) if (byte[str1] == 0) quit else if (strncmp(str1++, str2, len2) == 0) pos := idx quit else ++idx return posOr, try this test:
PUB parsemessage | i i := 0 repeat 4 '10 should handle 'on', or repeat strsize(message) for crude method of repeats if message[i] == "o" if message[i+1] == "n" outa[2] := 1 'assumed to be an LED??? if message[i] == "o" if message[i+1] == "f" if message[i+2] == "f" outa[2] := 0 i++This would normally search within the string and find on or off, not sure how it reacts with LF as part of the string.
How can i add the <LF> in my DAT byte?
I have a "on byte "on", 0" in my DAT section, is there any way to add the <LF> command before the "on" ?
Just to test my theory...
Now to the next issue....
I use the code below to read the message from my modem, and it seems to work fine, for a while...
It seems like the prop hangs if the modem recieves a message exactly at the same time this code is executed.
If there is no message to read, i just use the time-out part in the RxStrTime, and its then this seems to happen.
[code]
gsm.rxflush
gsm.str (string("AT+CMGR=1")) 'Read sms in first memory
gsm.tx(13) 'CT
debug.str (string("Get message in memory 1..."))
bytefill(@message, 0, 127)
gsm.RxStrTime(300,@mess1) '