Byte handling
Archiver
Posts: 46,084
I have the following byte data captured using SERIN.
DATA(0)=49 (ASCII 1)
DATA(1)=52 (ASCII 4)
DATA(2)=50 (ASCII 2)
I would like to do query these 3 bytes at once. The basis of the
query is
"If FREQ = 142 then do_lamp"
where FREQ is the ASCII equivalents of the 3 bytes..it represents
14.2MHZ as seen in the serial data from my radio.
I've managed to do it with a bunch of "IF..Then" statements and
queried each byte but I am looking for a shorter and smoother way.
I do have it working just fine but if there is a better way, there is
always room to learn more.
Thanks
Jay
DATA(0)=49 (ASCII 1)
DATA(1)=52 (ASCII 4)
DATA(2)=50 (ASCII 2)
I would like to do query these 3 bytes at once. The basis of the
query is
"If FREQ = 142 then do_lamp"
where FREQ is the ASCII equivalents of the 3 bytes..it represents
14.2MHZ as seen in the serial data from my radio.
I've managed to do it with a bunch of "IF..Then" statements and
queried each byte but I am looking for a shorter and smoother way.
I do have it working just fine but if there is a better way, there is
always room to learn more.
Thanks
Jay
Comments
Well, there are a few options. The best way would be to rewrite your SERIN
so that it didn't read it as bytes:
serin x,y,[noparse][[/noparse]blah..., dec3 freq,....]
This would read the 3 bytes in as an integer. Then you could do just what
you want:
if FREQ=142 then...
However, maybe thereis a reason you can't do that.
How about (data is a reserved word, so I'll use xdata):
FREQ=xdata(0)-$30*100+(xdata(1)-$30*10)+xdata(2)-$30
if FREQ=142 then ....
I think I got the parens right. Keep in mind the BS2 does math right to
left. So the idea is to -$30 from each byte (convert "0"-"9" into 0-9). Then
multiply the first digit by 100, the second digit by 10, and add all the
digits together.
Regards,
Al Williams
AWC
*8 channels of PWM: http://www.al-williams.com/awce/pak5.htm
>
Original Message
> From: Jay Kobelin [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=EpYVjZ165yxwMTmfyI-JjVNIsBmUFu7OhQ00A-M_t23rDoJVnDUQk7cQxNAjfMsQjVfLI8JV_deBrA]pcb4u@e...[/url
> Sent: Sunday, October 08, 2000 1:50 PM
> To: basicstamps@egroups.com
> Subject: [noparse][[/noparse]basicstamps] Byte handling
>
>
> I have the following byte data captured using SERIN.
>
> DATA(0)=49 (ASCII 1)
> DATA(1)=52 (ASCII 4)
> DATA(2)=50 (ASCII 2)
>
> I would like to do query these 3 bytes at once. The basis of the
> query is
>
> "If FREQ = 142 then do_lamp"
> where FREQ is the ASCII equivalents of the 3 bytes..it represents
> 14.2MHZ as seen in the serial data from my radio.
>
> I've managed to do it with a bunch of "IF..Then" statements and
> queried each byte but I am looking for a shorter and smoother way.
>
> I do have it working just fine but if there is a better way, there is
> always room to learn more.
>
> Thanks
> Jay
>
>
>
>
>
>
be used like this so make it dataa or something
if you add them like your thinking then if one number goes up by 1 and
another goes down by one you get the same value
if data(0) = 49 and data(1) = 50 and data(2) = 51 then do_lamp
Jay Kobelin wrote:
>
> I have the following byte data captured using SERIN.
>
> DATA(0)=49 (ASCII 1)
> DATA(1)=52 (ASCII 4)
> DATA(2)=50 (ASCII 2)
>
> I would like to do query these 3 bytes at once. The basis of the
> query is
>
> "If FREQ = 142 then do_lamp"
> where FREQ is the ASCII equivalents of the 3 bytes..it represents
> 14.2MHZ as seen in the serial data from my radio.
>
> I've managed to do it with a bunch of "IF..Then" statements and
> queried each byte but I am looking for a shorter and smoother way.
>
> I do have it working just fine but if there is a better way, there is
> always room to learn more.
>
> Thanks
> Jay
Bingo!!
The process of subtracting $30 works....now, could you briefly describe why
"0" becomes 0 and the use of the multipliers. I have always dealt with high
level English programming and never got involved with the bytes .. I let the
compiler do that!!
Thanks a bunch..
I tried going another byte to test the 10khz bit (1426 where the 6 would be
the next byte) using 1000 as a multiplier, then 100, etc...but that didn't
work.
(where 1=10 MHZ, 4=mhz, 2=200khz, 6=60khz or a final frequency readout on
the radio of 14.26mhz)
Jay
Original Message
From: Al Williams <alw@a...>
To: <basicstamps@egroups.com>
Sent: Sunday, October 08, 2000 12:04 PM
Subject: RE: [noparse][[/noparse]basicstamps] Byte handling
> Hi Jay,
>
> Well, there are a few options. The best way would be to rewrite your SERIN
> so that it didn't read it as bytes:
>
> serin x,y,[noparse][[/noparse]blah..., dec3 freq,....]
>
> This would read the 3 bytes in as an integer. Then you could do just what
> you want:
>
> if FREQ=142 then...
>
> However, maybe thereis a reason you can't do that.
>
> How about (data is a reserved word, so I'll use xdata):
>
> FREQ=xdata(0)-$30*100+(xdata(1)-$30*10)+xdata(2)-$30
>
>
> if FREQ=142 then ....
>
>
> I think I got the parens right. Keep in mind the BS2 does math right to
> left. So the idea is to -$30 from each byte (convert "0"-"9" into 0-9).
Then
> multiply the first digit by 100, the second digit by 10, and add all the
> digits together.
>
> Regards,
>
> Al Williams
> AWC
> *8 channels of PWM: http://www.al-williams.com/awce/pak5.htm
>
>
> >
Original Message
> > From: Jay Kobelin [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=aFAP08MmlfZW_do13otfeXz2Nm30JCkbD5G9NNO79U1ehjEqvx39p8pbZGvuOQLrAMd7u_n3UfYN6TKJWA]pcb4u@e...[/url
> > Sent: Sunday, October 08, 2000 1:50 PM
> > To: basicstamps@egroups.com
> > Subject: [noparse][[/noparse]basicstamps] Byte handling
> >
> >
> > I have the following byte data captured using SERIN.
> >
> > DATA(0)=49 (ASCII 1)
> > DATA(1)=52 (ASCII 4)
> > DATA(2)=50 (ASCII 2)
> >
> > I would like to do query these 3 bytes at once. The basis of the
> > query is
> >
> > "If FREQ = 142 then do_lamp"
> > where FREQ is the ASCII equivalents of the 3 bytes..it represents
> > 14.2MHZ as seen in the serial data from my radio.
> >
> > I've managed to do it with a bunch of "IF..Then" statements and
> > queried each byte but I am looking for a shorter and smoother way.
> >
> > I do have it working just fine but if there is a better way, there is
> > always room to learn more.
> >
> > Thanks
> > Jay
> >
> >
> >
> >
> >
> >
>
>
>
>
>
x-$30 is 0 to 9.
Now for multiplying, remember somewhere a long time ago, we learned that:
4235 is really
4 * 10**3 + 2 * 10**2 + 3 * 10**1 + 5 * 10**0 (where ** is exponentiation)
So:
4*1000 + 2*100 + 3*10 + 5*1 (10**0 is 1)
And therefore:
4000 + 200 + 30 + 5 = 4235.
Of course, the Stamp does math left to right, not in the usual order, so we
have to add parenthesis.
You should have been able to get it to work for 1000 just by moving
everything over one:
xdata(0)-$30*1000 + (xdata(1)-$30*100) + ....
You can also take advantage of the fact that 10*10 is 10 squared and
10*10*10 is 10 cubed, etc, and write a clever loop:
FREQ=0
for i=0 to lastdigit
FREQ=FREQ*10+xdata(i)-$30
next
This will do any number of digits up to last digit. Consider 4235 in the
xdata buffer.
On the first loop, FREQ=4
Next time, it will equal 40+2 or 42
Then 420 + 3 or 423
Finally 4230 + 5 or 4235, the correct answer.
Regards,
Al Williams
AWC
*Measure 8 pulse channels at once: http://www.al-williams.com/awce/pak7.htm
>
Original Message
> From: Jay Kay [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=V2fVThkEzzPsJ3EF5t2p0BCnghHP6VC-xdf9bxbkAbS6xBFaZi26kgrA5HohM1sjotW7ZYd3ice8yw]pcb4u@e...[/url
> Sent: Sunday, October 08, 2000 4:41 PM
> To: basicstamps@egroups.com
> Subject: Re: [noparse][[/noparse]basicstamps] Byte handling
>
>
> Al,
>
> Bingo!!
>
> The process of subtracting $30 works....now, could you briefly
> describe why
> "0" becomes 0 and the use of the multipliers. I have always
> dealt with high
> level English programming and never got involved with the bytes
> .. I let the
> compiler do that!!
>
> Thanks a bunch..
>
> I tried going another byte to test the 10khz bit (1426 where the
> 6 would be
> the next byte) using 1000 as a multiplier, then 100, etc...but that didn't
> work.
> (where 1=10 MHZ, 4=mhz, 2=200khz, 6=60khz or a final frequency readout on
> the radio of 14.26mhz)
>
> Jay
>
>
>
Original Message
> From: Al Williams <alw@a...>
> To: <basicstamps@egroups.com>
> Sent: Sunday, October 08, 2000 12:04 PM
> Subject: RE: [noparse][[/noparse]basicstamps] Byte handling
>
>
> > Hi Jay,
> >
> > Well, there are a few options. The best way would be to rewrite
> your SERIN
> > so that it didn't read it as bytes:
> >
> > serin x,y,[noparse][[/noparse]blah..., dec3 freq,....]
> >
> > This would read the 3 bytes in as an integer. Then you could do
> just what
> > you want:
> >
> > if FREQ=142 then...
> >
> > However, maybe thereis a reason you can't do that.
> >
> > How about (data is a reserved word, so I'll use xdata):
> >
> > FREQ=xdata(0)-$30*100+(xdata(1)-$30*10)+xdata(2)-$30
> >
> >
> > if FREQ=142 then ....
> >
> >
> > I think I got the parens right. Keep in mind the BS2 does math right to
> > left. So the idea is to -$30 from each byte (convert "0"-"9" into 0-9).
> Then
> > multiply the first digit by 100, the second digit by 10, and add all the
> > digits together.
> >
> > Regards,
> >
> > Al Williams
> > AWC
> > *8 channels of PWM: http://www.al-williams.com/awce/pak5.htm
> >
> >
> > >
Original Message
> > > From: Jay Kobelin [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=V2fVThkEzzPsJ3EF5t2p0BCnghHP6VC-xdf9bxbkAbS6xBFaZi26kgrA5HohM1sjotW7ZYd3ice8yw]pcb4u@e...[/url
> > > Sent: Sunday, October 08, 2000 1:50 PM
> > > To: basicstamps@egroups.com
> > > Subject: [noparse][[/noparse]basicstamps] Byte handling
> > >
> > >
> > > I have the following byte data captured using SERIN.
> > >
> > > DATA(0)=49 (ASCII 1)
> > > DATA(1)=52 (ASCII 4)
> > > DATA(2)=50 (ASCII 2)
> > >
> > > I would like to do query these 3 bytes at once. The basis of the
> > > query is
> > >
> > > "If FREQ = 142 then do_lamp"
> > > where FREQ is the ASCII equivalents of the 3 bytes..it represents
> > > 14.2MHZ as seen in the serial data from my radio.
> > >
> > > I've managed to do it with a bunch of "IF..Then" statements and
> > > queried each byte but I am looking for a shorter and smoother way.
> > >
> > > I do have it working just fine but if there is a better way, there is
> > > always room to learn more.
> > >
> > > Thanks
> > > Jay
> > >
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
> >
> >
>
>
>
four positions...
Thanks Al...your support is welcomed
Jay
Original Message
From: Al Williams <alw@a...>
To: <basicstamps@egroups.com>
Sent: Sunday, October 08, 2000 4:08 PM
Subject: RE: [noparse][[/noparse]basicstamps] Byte handling
> Well, "0" is $30, "1" is $31.... "9" is $39. So if x is "0" to "9" then
> x-$30 is 0 to 9.
>
> Now for multiplying, remember somewhere a long time ago, we learned that:
>
> 4235 is really
>
> 4 * 10**3 + 2 * 10**2 + 3 * 10**1 + 5 * 10**0 (where ** is exponentiation)
>
> So:
>
> 4*1000 + 2*100 + 3*10 + 5*1 (10**0 is 1)
>
> And therefore:
>
> 4000 + 200 + 30 + 5 = 4235.
>
> Of course, the Stamp does math left to right, not in the usual order, so
we
> have to add parenthesis.
>
> You should have been able to get it to work for 1000 just by moving
> everything over one:
>
> xdata(0)-$30*1000 + (xdata(1)-$30*100) + ....
>
> You can also take advantage of the fact that 10*10 is 10 squared and
> 10*10*10 is 10 cubed, etc, and write a clever loop:
>
> FREQ=0
> for i=0 to lastdigit
> FREQ=FREQ*10+xdata(i)-$30
> next
>
> This will do any number of digits up to last digit. Consider 4235 in the
> xdata buffer.
>
> On the first loop, FREQ=4
> Next time, it will equal 40+2 or 42
> Then 420 + 3 or 423
> Finally 4230 + 5 or 4235, the correct answer.
>
> Regards,
>
> Al Williams
> AWC
> *Measure 8 pulse channels at once:
http://www.al-williams.com/awce/pak7.htm
>
>
> >
Original Message
> > From: Jay Kay [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ZdHUL-7VFZ_W3SiXO64iOfZDlR4xCQRtoMZ7ujQzAuN8EYsg69uOwzTqah34gOR5OlNWaCkqA-JxC1rq]pcb4u@e...[/url
> > Sent: Sunday, October 08, 2000 4:41 PM
> > To: basicstamps@egroups.com
> > Subject: Re: [noparse][[/noparse]basicstamps] Byte handling
> >
> >
> > Al,
> >
> > Bingo!!
> >
> > The process of subtracting $30 works....now, could you briefly
> > describe why
> > "0" becomes 0 and the use of the multipliers. I have always
> > dealt with high
> > level English programming and never got involved with the bytes
> > .. I let the
> > compiler do that!!
> >
> > Thanks a bunch..
> >
> > I tried going another byte to test the 10khz bit (1426 where the
> > 6 would be
> > the next byte) using 1000 as a multiplier, then 100, etc...but that
didn't
> > work.
> > (where 1=10 MHZ, 4=mhz, 2=200khz, 6=60khz or a final frequency readout
on
> > the radio of 14.26mhz)
> >
> > Jay
> >
> >
> >
Original Message
> > From: Al Williams <alw@a...>
> > To: <basicstamps@egroups.com>
> > Sent: Sunday, October 08, 2000 12:04 PM
> > Subject: RE: [noparse][[/noparse]basicstamps] Byte handling
> >
> >
> > > Hi Jay,
> > >
> > > Well, there are a few options. The best way would be to rewrite
> > your SERIN
> > > so that it didn't read it as bytes:
> > >
> > > serin x,y,[noparse][[/noparse]blah..., dec3 freq,....]
> > >
> > > This would read the 3 bytes in as an integer. Then you could do
> > just what
> > > you want:
> > >
> > > if FREQ=142 then...
> > >
> > > However, maybe thereis a reason you can't do that.
> > >
> > > How about (data is a reserved word, so I'll use xdata):
> > >
> > > FREQ=xdata(0)-$30*100+(xdata(1)-$30*10)+xdata(2)-$30
> > >
> > >
> > > if FREQ=142 then ....
> > >
> > >
> > > I think I got the parens right. Keep in mind the BS2 does math right
to
> > > left. So the idea is to -$30 from each byte (convert "0"-"9" into
0-9).
> > Then
> > > multiply the first digit by 100, the second digit by 10, and add all
the
> > > digits together.
> > >
> > > Regards,
> > >
> > > Al Williams
> > > AWC
> > > *8 channels of PWM: http://www.al-williams.com/awce/pak5.htm
> > >
> > >
> > > >
Original Message
> > > > From: Jay Kobelin [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ZdHUL-7VFZ_W3SiXO64iOfZDlR4xCQRtoMZ7ujQzAuN8EYsg69uOwzTqah34gOR5OlNWaCkqA-JxC1rq]pcb4u@e...[/url
> > > > Sent: Sunday, October 08, 2000 1:50 PM
> > > > To: basicstamps@egroups.com
> > > > Subject: [noparse][[/noparse]basicstamps] Byte handling
> > > >
> > > >
> > > > I have the following byte data captured using SERIN.
> > > >
> > > > DATA(0)=49 (ASCII 1)
> > > > DATA(1)=52 (ASCII 4)
> > > > DATA(2)=50 (ASCII 2)
> > > >
> > > > I would like to do query these 3 bytes at once. The basis of the
> > > > query is
> > > >
> > > > "If FREQ = 142 then do_lamp"
> > > > where FREQ is the ASCII equivalents of the 3 bytes..it represents
> > > > 14.2MHZ as seen in the serial data from my radio.
> > > >
> > > > I've managed to do it with a bunch of "IF..Then" statements and
> > > > queried each byte but I am looking for a shorter and smoother way.
> > > >
> > > > I do have it working just fine but if there is a better way, there
is
> > > > always room to learn more.
> > > >
> > > > Thanks
> > > > Jay
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > >
> > >
> > >
> >
> >
> >
>
>
>
>
>