String and array
hooktxu
Posts: 33
I want to pass an array to a string.
My piece of code is the next and the only thing missing is that the tag is a string.
id12.readRFID
repeat i from 0 to 15
tag:=id12.rValue(i)
''tagNum:=
My piece of code is the next and the only thing missing is that the tag is a string.
id12.readRFID
repeat i from 0 to 15
tag:=id12.rValue(i)
''tagNum:=
Comments
____repeat i from 0 to 15
______tag[noparse][[/noparse] i ]:=id12.rValue(i)
______'tagNum:=
I don't understand what your problem is currently, so I guess:
You can not pass an array to the string() SPIN command, as it is -like the constant() command- something which is only relevant at compile-time. string() reserves some memory for the string given as argument and uses it's adress in the instruction in which string is involved.
If your array already contains characters, then you can simply add a string terminator ( $0 ) and you can treat it like a string.
Is it possible?
var byte tag[noparse][[/noparse] 10 ]
pub main
tag[noparse][[/noparse] 0 ]:='T'
tag[noparse][[/noparse] 1 ]:='E'
tag[noparse][[/noparse]·2 ]:='S'
tag[noparse][[/noparse]·3 ]:='T'
tag[noparse][[/noparse]·4 ]:= 0
is in terms of the content the same as
dat
tstr byte "TEST",0
and for example
strcmp( @tag, string("TEST") )
strcmp( @tstr, string("TEST") )
strcmp( @tag, @tstr )
should all give you the same result.
If you use string("xx") with the same string inside, it is better to use a dat-section, as it saves space and can be used as often as you want whereas string() will produce several sections in the memory even if the string is the same.
So, in your case you should simply make sure that after the·last character in you array·there is really a 0 and then you can use strcmp with the array directly.
aplicacionTwitter:= string("01072176AEFF")
tagNum:=serialPC.tx(tag)
strcomp(tagNum,aplicacionTwitter)
is it correct?
2) The Extended FullDuplexSerial object (obex.parallax.com/objects/31/) has a routine to receive a sequence of characters into a byte array (RxStr). You can use any termination character you want (like a return - $0D - the default). The string is stored temporarily in a variable inside the object that can hold up to 15 characters. Parallax's RFID reader sends the bytes $0A,"0","1","0","7","2","1","7","6","A","E","F","F",$0D. RxStr replaces the $0D with the zero byte that terminates the string. You could do:
and it doesn´t work
Post Edited (hooktxu) : 6/11/2009 10:22:12 AM GMT
I don't know the extended full duplex yet, but in your case you need to be sure that the buffer is big enough, because you first send out 15 characters after that you receive. So the buffer must be at least 15 bytes big.
·Is this correct? I don't know the id12 object, but it looks like you get the RFID bytewise with the rValue(i) where i is the number of the byte. But you always store it in tag, which is equal to tag[noparse][[/noparse]0]. I guess you should say
tag[noparse][[/noparse]i]:=id12.rValue(i)
·
[code]
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
byte tag[noparse][[/noparse]16]
byte i
byte idPB
byte idOverlay
byte tipoFuente
byte datoEvento
byte accion
byte aplicacionTwitter
byte aplicacionMaquinas
byte tagNum[noparse][[/noparse]16]
byte tagEstado
long A
OBJ
sensorToq : "SensorToque"
sensorMagnetico : "SensorMagnetico"
id12: "RFID"
id12accion: "RFIDaccion"
''iwifi: "iWiFiXXX"
serialPC:"Simple_Serial"
duplex: "Extended_FDSerial"
'serialPC: "Extended_FDSerial"
DAT
Str1 word "01072176AEFF", 0 'Aplicacion Twitter
Str2 word "0107216B1B57", 0 'Aplicacion Maquinas
Str3 word "010721924CF9", 0 'Tag de estado
PUB Main
idPB:=1
idOverlay:=0
tipoFuente:=0
datoEvento:=0
accion:=0
'tagNum:=string("01072176AEFF")
aplicacionTwitter:= string("01072176AEFF")
'aplicacionMaquinas:= string("0107216B1B57")
'tagEstado:= string("010721924CF9")
serialPC.init(-1,30,9600)
duplex.Start(31,-1,0,9600)
sensorToq.arrancar
waitcnt(clkfreq*2 + cnt)
repeat
serialPC.str(string("Decision de la aplicacion: "))
id12.readRFID
repeat i from 0 to 15
tag:=id12.rValue(i)
serialPC.tx(tag)
serialPC.str(string("Lectura del RFID correcto"))
{{repeat i from 1 to 12
tagNum:=serialPC.tx(tag)
serialPC.tx(tagNum)
serialPC.str(string("Pasa a String el RFID")) }}
repeat i from 1 to 12
serialPC.tx($0A)
serialPC.tx($0D)
serialPC.tx(tag)
serialPC.str(string(" Vamos a comparar los strings "))
'serialPC.str(string(" TagNum: "))
'serialPC.str(tagNum)
serialPC.str(@tag) 'Imprime el contenido del tag
bytemove(tagNum,@tag+1,12)
serialPC.str(@tagNum)
'duplex.RxStr(@tag)
serialPC.str(string(" LLega "))
if strcomp(tagNum,@Str1) ''Aplicacion Twitter
serialPC.str(string("Entra en el primer if"))
idOverlay:= 1
tipoFuente:= 1 ''Solo tenemos un tipo de dispositivo, que son los sensores magn
duplex.RxStr(@tag)