How to decode received bytes of the RF Receiver?

Forum about Rfxcom home automation Domotica devices.

Moderator: b_weijenberg

Post Reply
iamjwk
Starting Member
Starting Member
Posts: 39
Joined: Thu Oct 04, 2007 9:01 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by iamjwk »

I am building a simple logging application in visual basic 2005 based on the rfreceiver source. The received data of RF Receiver will be stored in a MS-Access database file. In this process I want to modify the core routines for receiving and parsing data from the serial port and make use of the .NET serial port component.

I am now in the phase of decoding the received bytes but are struggling. Example of received bytes (hex values) is:
4D 18 50 1A 2D 40 25 72 08 60 C8 46 9F 50 1A 2D 40 25 72 08 60 C8 46 9F

I know the first two bytes are the response on the request for the version. How do I find out what the rest is? Also it is possible the program starts receiving bytes somewhere in the middle; how to solve?

I looked into the rfreceiver source (tmrRead_tick) but could not understand what is happening.

Jan-Willem
User avatar
b_weijenberg
Forum Moderator
Forum Moderator
Posts: 1744
Joined: Sun May 14, 2006 4:32 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by b_weijenberg »

Jan-Willem,

check this thread http://www.domoticaforum.eu/topic.asp?TOPIC_ID=346

in fact use these steps:
1. send an init command and wait for a response. F02C returns 2C
2. receive 1 byte. This must be the length byte.
3. receive the number of full bytes indicated by the length byte of step 2
4. if a timeout occurs clear the receive buffer and goto step 2.
5. if all bytes received decode the received data.

Bert
iamjwk
Starting Member
Starting Member
Posts: 39
Joined: Thu Oct 04, 2007 9:01 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by iamjwk »

Bert,

I figuring out what following piece of code means/does. Its from the same routine and decodes the packet length:

Code: Select all

recbits = temp And &H7F
If (temp And &H80) = 0 Then
  slave = False
Else
  slave = True
End If
If (recbits And &H7) = 0 Then
  recbytes = (recbits And &H7F) >> 3
Else
  recbytes = ((recbits And &H7F) >> 3) + 1
End If
In my case the received length byte is 50 hex (=80 decimal). This is from my oregon sensor (501A2D4025220770C841A8 THGR228N); 10 bytes + packet length byte.

I do not understand the piece of code above. I could just calculate 80 bits / 8 = 10 bytes. The routine above gives 11 bytes. Also the purpose of shifting bits to the right is not clear.

Jan-Willem
User avatar
b_weijenberg
Forum Moderator
Forum Moderator
Posts: 1744
Joined: Sun May 14, 2006 4:32 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by b_weijenberg »

Jan-Willem,
hex 50 = 0101 0000
hex 07 = 0000 0111
an AND on those two bytes results in zero.

So the 1st statement will be executed:
0101 0000 shift right 3 bits is 0000 1010 = hex 0A = 10 decimal bytes.

bert
iamjwk
Starting Member
Starting Member
Posts: 39
Joined: Thu Oct 04, 2007 9:01 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by iamjwk »

hmm something wrong in the translation I made in visual basic. Because I arrive in stepping trough my code at 11. Will have a look.

But still why this "difficult" and / shifting bits if a simple division gets the same result; do I miss something? I try to keep my code as simple as possible, so other people can also use it.

Jan-Willem
User avatar
b_weijenberg
Forum Moderator
Forum Moderator
Posts: 1744
Joined: Sun May 14, 2006 4:32 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by b_weijenberg »

Jan-Willem,

As most processing is bit oriented it is better to use shifting and and-ing and or-ing to get the results. As you can see all data is in byte fields which makes is easier to process it this way. When using character oriented field you will run sometimes into problems with signs and other unwanted behaviour. A better language to do this is probably C or C# but I prefered VB when started writing the programs.
The coding can be cleaned on some parts because I've added coding without giving much attention to write clean coding. Most of the time my priority was to have a test program available for new functions. To change the COM part to .NET is something I had also on my to-do-list so if you can do this I like it. Please use byte oriented fields for the receive data buffers.

Bert
iamjwk
Starting Member
Starting Member
Posts: 39
Joined: Thu Oct 04, 2007 9:01 pm
Location: Netherlands

How to decode received bytes of the RF Receiver?

Post by iamjwk »

Bert,

All serial port processing I am writing is using byte types, so should be OK.

Can you explain the instruction "recbits = temp And &H7F" in plain language? temp contains the length byte. So in decimal "temp" is either 80 or 32. And with &H7F (=127 decimal) does not seem to make any difference in visual basic (i.e. recbits=temp).

Jan-Willem
Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

How to decode received bytes of the RF Receiver?

Post by Digit »

Jan-Willem,

Only bits 6-0 contain the packet length. So by and-ing with $7F (=1111111 binary) you get the packet length. The MSB of the first byte you receive designates whether the packet came from either a master or slave receiver. To get the correct packet length you have to ignore this bit. That's what the and $7F is for.

BTW, it's all in the docs...

Robert.
Gourmet
Starting Member
Starting Member
Posts: 12
Joined: Thu Nov 15, 2007 2:07 pm
Location: France

How to decode received bytes of the RF Receiver?

Post by Gourmet »

<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by iamjwk</i>
<br />I am building a simple logging application in visual basic 2005 based on the rfreceiver source. The received data of RF Receiver will be stored in a MS-Access database file. In this process I want to modify the core routines for receiving and parsing data from the serial port and make use of the .NET serial port component.

I am now in the phase of decoding the received bytes but are struggling. Example of received bytes (hex values) is:
4D 18 50 1A 2D 40 25 72 08 60 C8 46 9F 50 1A 2D 40 25 72 08 60 C8 46 9F

I know the first two bytes are the response on the request for the version. How do I find out what the rest is? Also it is possible the program starts receiving bytes somewhere in the middle; how to solve?

I looked into the rfreceiver source (tmrRead_tick) but could not understand what is happening.

Jan-Willem
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
I made something last sunday after having received my Rfx box and having watched the receiver the way it works.
IMHO, the simple way to do it is like retrieving data from serial interfaces : use stacks.

Moreover, in this case, you don't have to take care of the integrity of the data along since they are repeated.
As Bert said : you listen, you try to build a complete frame, if you don't manage to (timeout, garbage) you clear the buffer and begin again to wait for new data. Und so weiter.
Once you have a complete frame you stack it upon and leave another parallel process the task to decode it.
So 3 processes : one to catch and to build frames, another to decode and a thrid one to do something with the decoded data.

I have 5 processes : the first 2 above plus one in order to make something (according to the hour, the external temperature, the temperaturs of such or such room, triggering appliances), one for reading (at regular interval) from a config file (I want to be able to change the average temperature of one room without relauching the whole program) and one in order to listen for web services (for clients).
I could also create another process to manage the CM11 (with a stack in order to communicate between the main process and this new one).


db
Post Reply

Return to “Rfxcom Receivers & Transmitters Forum”