Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

RS232, hard or soft?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Wed Sep 15, 2004 8:07 am    Post subject: RS232, hard or soft? Reply with quote

I have a requirement to parse a serial datastream.. Its constructed of a start byte, four data bytes, and a checksum byte..

So.. i've seen the ischarwaiting() function.. Would it be suitable just to have a loop which checks if there is a char, and then act on it? Or do you have to use a software uart to get the buffer? Can anyone clarify how it works?

I've seen how you can configure a buffer with config serialin, but then you have to use OPEN etc.. If you just do a basic $baud and ischarwaiting() and inkey(), how big is the buffer? (if there is one)..

Thanks

Stu
Back to top
View user's profile MSN Messenger
oe9vfj

Moderator



Joined: 17 Jun 2004
Posts: 269
Location: Austria, Hard

austria.gif
PostPosted: Wed Sep 15, 2004 8:28 am    Post subject: Reply with quote

Hi,

As far as I know, Soft-UART has no buffer. You must call the INKEY() before the char receives.

I advise you to use the Hardware-UART of the chip and config a small Serialin-buffer. This gives you more flexibility.
Than you can check in a loop with ischarwaiting(), whether there are character(s) in the buffer and read it out with INKEY().

The size of the buffer depends of your loop time and the baud-rate of your rs232 connection.

_________________
regards Josef

DOS - File System for BASCOM-AVR on http://members.aon.at/voegel
Back to top
View user's profile Visit poster's website
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Wed Sep 15, 2004 9:48 am    Post subject: Reply with quote

Cool. thanks.

As the data is coming through as 5 single bytes in a packet, should I use inkey() or inputbin, do you think?

-Stu
Back to top
View user's profile MSN Messenger
oe9vfj

Moderator



Joined: 17 Jun 2004
Posts: 269
Location: Austria, Hard

austria.gif
PostPosted: Wed Sep 15, 2004 10:38 am    Post subject: Reply with quote

I would use INKEY, but don't know exactly the difference between INKEY and INPUTBIN according to your problem.
I think, to read to a BYTE is is the same. With INPUTBIN you can read to other variables than BYTE too.

But you have to check for the starting byte, so you have to check each received byte and storing them to an array or whatever you want.

_________________
regards Josef

DOS - File System for BASCOM-AVR on http://members.aon.at/voegel
Back to top
View user's profile Visit poster's website
DToolan

Bascom Member



Joined: 14 Aug 2004
Posts: 1384
Location: Dallas / Fort Worth, Texas (USA)

blank.gif
PostPosted: Wed Sep 15, 2004 4:35 pm    Post subject: Reply with quote

Personally, I would make a buffer and receive the characters and perform the checksum calculation in the background via ISR.

This routine isn't all inclusive but gives you another idea of how it could be done.

Code:
$REGFILE = "m128def.dat"                                    '<- whatever your chip is
$CRYSTAL = 16000000                                         '<- your crystal
$BAUD = 9600                                                '<- USART0 baud rate

DIM USART0_Buffer(6) As Byte                                  '<- 6 character buffer
DIM USART0_Pointer As Byte

DIM TEMP As Byte

DIM USART0_Status As Byte

DataReady ALIAS USART0_Status.1
StartCondition ALIAS USART0_Status.0

USART0_Pointer = 1                                          '<- initialize the pointer to the start of the buffer

StartByte ALIAS USART0_Buffer(1)
DataByte1 ALIAS USART0_Buffer(2)
DataByte2 ALIAS USART0_Buffer(3)
DataByte3 ALIAS USART0_Buffer(4)
DataByte4 ALIAS USART0_Buffer(5)
ChkSumByte ALIAS USART0_Buffer(6)

CONST TRUE = 1
CONST FALSE = 0
CONST PASSED = 0

CONST StartByteValue = &H03                                 '<- make this whatever your start byte is supposed to be

DIM ChecksumCode As Byte

CONFIG Com1 = Dummy , Synchrone = 1 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0


ENABLE URXC : ON URXC USART0_RXC_ISR                        'allow interrupts on USART0 receive complete

ENABLE INTERRUPTS                                           'globally allow interrupts to occur



'MAIN

DO

WHILE DataReady = TRUE

   'do something with DataByte1
   'do something with DataByte2
   'do something with DataByte3
   'do something with DataByte4

   'you are done so reset the DataReady flag
   DataReady = FALSE

WEND

LOOP

END

USART0_RXC_ISR:

TEMP = UDR                                                  '<- put the contents received from the UART data register into TEMP

IF TEMP = StartByteValue THEN                               '<- see if it matches your start byte value

   StartCondition = TRUE

END IF

IF StartCondition = TRUE THEN

   SELECT CASE USART0_Pointer

         CASE 1                                             'the start byte

            USART0_Buffer(USART0_Pointer) = TEMP
            INCR USART0_Pointer                             '<- increment for the next character to come

         CASE 2 TO 5                                        '4 data bytes

            USART0_Buffer(USART0_Pointer) = TEMP
            INCR USART0_Pointer                             '<- increment for the next characters to come

         CASE 6                                             'the checksum byte

            USART0_Buffer(USART0_Pointer) = TEMP

            'here you can perform a test of the buffer and checksum
            'i don't know your checksum code so we'll just pretend it's here
            'and it's result is called ChecksumCode

            'checksum routines should equal zero when perfomed on an array
            'that includes a checksum received

            IF ChecksumCode = PASSED THEN                   'the result of the checksum routine is zero

               DataReady = TRUE                             'let the main program know it can do whatever it wants with this data
               StartCondition = FALSE                       'reset for the next data packet to arrive
               USART0_Pointer = 1                           'reset the buffer pointer for the next data packet

            ELSE                                            'checksum test routine says the data failed

               StartCondition = FALSE                       'reset for the next data packet
               USART0_Pointer = 1                           'reset the buffer pointer for the next data packet

            END IF

   END SELECT

END IF

RETURN


Last edited by DToolan on Fri Sep 17, 2004 3:01 pm; edited 1 time in total
Back to top
View user's profile Yahoo Messenger
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Wed Sep 15, 2004 4:48 pm    Post subject: Reply with quote

Excellent, thankyou. I like the way you've laid it out.. Thanks for your time..
Back to top
View user's profile MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum