PDA

View Full Version : Decimal variable input with HSERIN command



pxidr84
- 18th April 2013, 17:57
Hello,

I would like to do a simple thing with the HSERIN command :

I send from my computer the ASCII chain "A1234". What I want it's to store the number (1234) in a variable (for instance, "vartest").
Also, if I send from my computer the ASCII chain "B0005", I want to store the number (5) into an another variable (let's say "vartest2").

I think it's quite simple, but I don't know how to handle that within a loop.

Thanks ;)

HenrikOlsson
- 18th April 2013, 18:19
Hello,

Perhaps something like this


CMD VAR BYTE
TestVar1 VAR BYTE
TestVar2 VAR BYTE

HSERIN [CMD]

Select Case CMD
Case "A"
HSERIN[DEC4 TestVar1]
Case "B"
HSERIN[DEC4 TestVar2]
END SELECT

/Henrik.

pxidr84
- 21st April 2013, 19:47
Thanks a lot Henrik, it works fine.

The only problem I got is that HSERIN hangs my loop...

HenrikOlsson
- 21st April 2013, 21:20
Yes, that's the way it works....
If the loop time is less that 2 "byte-times" you can poll the receive flag each time thru the loop and get the byte(s) from the receive buffer. If the loop time is longer you need to use interrupts.

/Henrik.

pxidr84
- 21st April 2013, 21:38
Yes, that's the way it works....
If the loop time is less that 2 "byte-times" you can poll the receive flag each time thru the loop and get the byte(s) from the receive buffer. If the loop time is longer you need to use interrupts.

/Henrik.

Hmm right now I just want to retreive a 4-number decimal value in my loop, and put it in the "ref" variable. I don't even need a letter, I just send raw numbers (in ASCII) , like "0586" from my computer.
The problem is that in my program I use very precise and quick timing interrupts so it's a bit annoying.
Maybe there is a way to send not an ASCII number but directly a hex number, so 0586 will be 24A... max value is 1200 (4B0, so I use 2 bytes)

I have no idea how to use the RX buffer...

pedja089
- 21st April 2013, 21:46
You can add timeout label. Then it won't hang forever..
HSERIN 10,TimeOut, [CMD]


Select Case CMD
Case "A"
HSERIN 10,TimeOut,[DEC4 TestVar1]
Case "B"
HSERIN 10,TimeOut,[DEC4 TestVar2]
END SELECT
TimeOut:

pxidr84
- 22nd April 2013, 17:26
You can add timeout label. Then it won't hang forever..
HSERIN 10,TimeOut, [CMD]


Select Case CMD
Case "A"
HSERIN 10,TimeOut,[DEC4 TestVar1]
Case "B"
HSERIN 10,TimeOut,[DEC4 TestVar2]
END SELECT
TimeOut:

I can't use that, too program time-consuming, my main timing interrupt is refreshing every 92µs or so...

HenrikOlsson
- 22nd April 2013, 19:00
Hi,
This is totally untested but could serve as a starting point for a polled receive buffer. Short of this you need to use interrupts to receive the data in the background but I suspect that with your timing sensetive loop even that might cause to much problem.


RC1IF VAR PIR1.5 'Alias to Interrupt request bit for USART1, double check against datasheet.

RxData VAR BYTE[5]
Value VAR WORD
ByteCount VAR BYTE

TestVar1 VAR WORD
TestVar2 VAR WORD

ByteCount = 0 ' Reset buffer pointer.

Main:
' Main loop has to run faster than the bytes can arrive.
If RC1IF THEN ' Is there a byte in the receive buffer?
HSERIN[RxData[ByteCount]] ' Get it and put in the buffer array, RCIF will be cleared by the hardware.
IF ByteCount = 4 THEN ' Do we have 5 bytes yet?
GOSUB ProcessFrame 'If so go process them...
ELSE
ByteCount = ByteCount + 1 ' If not, get ready for the next.
ENDIF
ENDIF

Goto Main

ProcessFrame:
ByteCount = 0 ' Get ready for next frame.

' Now convert the next 4 bytes from ASCII to a numeric value.
' Could probably use ArrayRead here instead.
Value = (RXData[1] - 48) * 1000
Value = Value + ((RxData[2] - 48) * 100)
Value = Value + ((RxData[3] - 48) * 10)
Value = Value + (RxData[4] - 48)

If RxData[0] = "A" THEN
TestVar1 = Value
ENDIF

If RxData[0] = "B" THEN
TestVar2 = Value
ENDIF
RETURN

/Henrik.

pxidr84
- 28th April 2013, 14:14
Hi,
This is totally untested but could serve as a starting point for a polled receive buffer. Short of this you need to use interrupts to receive the data in the background but I suspect that with your timing sensetive loop even that might cause to much problem.


RC1IF VAR PIR1.5 'Alias to Interrupt request bit for USART1, double check against datasheet.

RxData VAR BYTE[5]
Value VAR WORD
ByteCount VAR BYTE

TestVar1 VAR WORD
TestVar2 VAR WORD

ByteCount = 0 ' Reset buffer pointer.

Main:
' Main loop has to run faster than the bytes can arrive.
If RC1IF THEN ' Is there a byte in the receive buffer?
HSERIN[RxData[ByteCount]] ' Get it and put in the buffer array, RCIF will be cleared by the hardware.
IF ByteCount = 4 THEN ' Do we have 5 bytes yet?
GOSUB ProcessFrame 'If so go process them...
ELSE
ByteCount = ByteCount + 1 ' If not, get ready for the next.
ENDIF
ENDIF

Goto Main

ProcessFrame:
ByteCount = 0 ' Get ready for next frame.

' Now convert the next 4 bytes from ASCII to a numeric value.
' Could probably use ArrayRead here instead.
Value = (RXData[1] - 48) * 1000
Value = Value + ((RxData[2] - 48) * 100)
Value = Value + ((RxData[3] - 48) * 10)
Value = Value + (RxData[4] - 48)

If RxData[0] = "A" THEN
TestVar1 = Value
ENDIF

If RxData[0] = "B" THEN
TestVar2 = Value
ENDIF
RETURN

/Henrik.

Oh thanks Henrik, I will see what I can do with this.
But I think I'm gonna to use preselected values, less complicated to handle for the PIC...