PDA

View Full Version : HSER revisited



jmgelba
- 18th January 2008, 02:42
I need to send and receive bytes in a hex format. Is it as simple as HSEROUT $aa, $bb, $cc etc etc?
And to receive bytes in hex is just HSERIN HEX B0, B1, B2 ?

Now if I want to send out an incoming byte but swap the first and last characters around - example recieve $ab and send back $ba, how would I split up the byte and reverse it and send it back again?
Do I need to split the byte upon receipt or save it to a variable and split the variable?

BrianT
- 18th January 2008, 11:53
I have had mixed success with HSERIN and HSEROUT. You certainly MUST set the defines for HSER_TXSTA, HSER_RCSTA and baud rate. I think you also need to manually clear framing and overrun errors for complete success. Maybe also to tie the input high or low with 4k7. I never found out why the command was unreliable and so I gave up on HSER and went for SERIN/OUT and SERIN2/OUT2 which are software UARTS. They take a little more code and time but they have been absolutely reliable in my usage.

I also send and receive HEX from time to time. I use the bit masking technique.

Assume the 8 bit value for A has been received by your favourite serial input routine, such as SERIN. Further assume the upper 4 bit nibble is B and the lower 4 bit nibble is C

Find the upper nibble
B = A >> 4 ' here the lower four bits 'fall off' and the new incoming upper bits are zeros.
More properly perhaps
B = A >> 4 & %00001111

Isolate the lower nibble
C = A & %00001111 'here the upper4 bits are masked to zeros

To build a new A in the reverse hex/nibble order to that received.

A = (C << 4) | B 'this is the bitwise addition of the upper and lower 4 bits to make a new 8 bit byte.


HTH
BrianT

Bruce
- 18th January 2008, 13:31
Using the HEX modifiers with HSERIN/HSEROUT works just like it's shown in the manual.

Here's an example of receiving .HEX files from a PC serial connection;
http://www.microengineeringlabs.com/resources/samples/x1/pbp/progeex.bas

Reversing the upper & lower nibbles is really simple with a single line of assembler.

X VAR BYTE
X = $AB

@ SWAPF _X,F ; Swap upper & lower nibbles in X. Now X = $BA.

jmgelba
- 18th January 2008, 19:13
OK, so lets say I'm being sent $ab and I need it to be placed in a variable a0.

HSERIN [HEX4 a0]

This will place the hex value of $ab into a0, correct?

What if I'm recieving several bytes such as $aa $ab $ac $ad etc etc and I want them in a1 a2 a3 a4 etc?

HSERIN [HEX4 ao, a1, a2, a3, a4]
or do I need multiple lines?
HSERIN [HEX4 a0]
HSER [HEX4 a1] etc etc.

skimask
- 18th January 2008, 19:41
This will place the hex value of $ab into a0, correct?
Should do it.


What if I'm recieving several bytes such as $aa $ab $ac $ad etc etc and I want them in a1 a2 a3 a4 etc?
HSERIN [HEX4 ao, a1, a2, a3, a4]
or do I need multiple lines?
HSERIN [HEX4 a0]
HSER [HEX4 a1] etc etc.

Your first example would almost work, except it should be like this:
HSERIN [ HEX2 a0, HEX2 a1, HEX2 a2 ]
HEX2 for bytes
HEX4 for words
And multiple lines/statements would work too.