PDA

View Full Version : Serial from PC into an Array



andyf
- 12th May 2005, 14:25
I have managed to make a really cool design that is perfect for testing the PIC16F877-20/P is getting what I send it. it simply echo's back to the PC what Is recieved at 19,200 on a 20MHZ clock.

ok, so this is great and runs sweat, even have several subroutines to act upon the data/codes I send the PIC.

Now I have a short and simple problem and am sure the answer will be very obvious so here goes.

HSERIN [WAIT("*TEST"),STR in_ctl\11\"*"]

in-ctl is an array that can contain upto 24 characters, the question is how can I know if it only has 6, 7 10 20 etc...

input string examples:
TESTABCDEF! in-ctl contains 7 characters
TESTABCDEFGHI! in-ctl contains 10 characters
TESTABC! in-ctl contains 3 characters

I want to do this so I can have a loop similar in practice to this syntax:

AR = arreycount(in-ctl)
do
.
. some code
.
X=X+1
loop until X = AR


Andy

Dave
- 12th May 2005, 18:41
andyf, What I used to do is make sure the receiving array is all null by clearing it in a sort of loop. Then using the HSERIN options of "STR arrayname/maxumum characters/13" will load the array with no more than the maximum characters and terminate the input or, any number of characters until a carriage return is encountered. Then within some sort of loop look for the last character to be null and that is the last received character. Something simillar to this code:
Counter = 0
While counter <= maxcharacters
in_ctl(counter) = 0
Counter = Counter + 1
wend
HSERIN [WAIT("*TEST"),STR in_ctl\Maxcharacters\13]
Counter = 0
While (in_ctl(Counter) <> 0) and (Counter < Maxcharacters)
Counter = Counter + 1
wend
'At this point Counter has the number of characters in the received array -1

Dave Purola,
N8NTA

mister_e
- 13th May 2005, 01:13
Good idea Dave.

If you want to have another way,
Here's is another way (http://www.picbasic.co.uk/forum/showthread.php?t=1639&page=3)

NavMicroSystems
- 13th May 2005, 11:34
You could send a single byte containing the length of the string.
Send this byte right after the "SYNC" string (*TEST)

STR in_ctl[0] would then tell you how many valid chars are in this array.

Dave
- 13th May 2005, 18:33
Mister-e, That is an OK way to do it at a slower baud rate but at 9600+ baud I would be concerned with loosing characters especially at lower osc frequency's.

Dave Purola,
N8NTA

mister_e
- 13th May 2005, 18:53
Dave,

you're 100% right on that, was just an example snip. For faster Baudrate I'll for sure prefer using an other way like yours or Ralph's suggestion. Well, serial communication handling is an endless story and depend of too much thing to have an single solution :)

NavMicroSystems
- 13th May 2005, 19:50
...For faster Baudrate I'll for sure prefer using an other way like yours or Ralph's suggestion...
Whenever I can control the "Sender" I add this byte that tells the receiver how many bytes are to be received, and I can tell it works perfectly.

The advantage is I don't need to clear the array or scan the array for some kind of "End of String".

mister_e
- 13th May 2005, 20:10
yep i agree. BTW i have few minutes to spend on it and here's an update tested @19200 bauds @ 4MHZ


' Program to echo incoming serial data string
' Baudrate : 19200 Bauds
' Method : Polling USART interrupts flags

' Pic Definition
' ==============
' Using PIC 18F2320 @ 4MHZ and bootloader
'
DEFINE LOADER_USED 1
DEFINE OSC 4

' Hardware configuration
' ======================
'
'
TRISC = %10000000 ' PORTC.7 is the RX input
' PORTC.6 is the TX output

' Serial communication definition
' ===============================
' Using internal USART and MAX232 to interface to PC
'
RCSTA=$90 ' enable serial port,
' enable continuous receive
'
TXSTA=$24 ' enable transmit,
' BRGH=1
'
SPBRG=12 ' set baudrate to 19200 Bauds

' Alias definition
' ================
'
'
RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
CREN var RCSTA.4 ' Receiver enable bit
OERR var RCSTA.1 ' Overrun error

' Variable definition
' ===================
'
'
SerialString var byte[40]
Counter var byte
Success var bit
i var byte
temp var byte
header var bit

' Hardware initialisation
' =======================
'
'
pause 100 ' safe start-up delay
clear

Main:
IF oerr then
cren=0
cren=1
endif

if RCIF then ' incomming data?
I=RCREG ' take it

if header then
if i ="L" then discard ' is it the end?
serialstring[counter]=i ' Store into the array
counter=counter+1 '
endif

if i ="!" then header=1 ' is it the header character?
Discard:
while RCIF
temp=RCREG
wend

if i="L" then
success=1 ' Yeah i got a valid string
header=0
endif
endif

if success then
cren=0 ' Disable Receiver
hserout ["String received : ",str serialstring\counter,13,10,_
"Character count : ", dec counter,13,10]
cren=1 ' Enable receiver
clear
endif
goto main