Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Simulating UART Coommands and CRLF

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 82
Location: Aachen

germany.gif
PostPosted: Mon Sep 11, 2017 12:28 pm    Post subject: Simulating UART Coommands and CRLF Reply with quote

Dear All,
I want to filter information out of a RS232 datastream.
The information that I will receive starts with "F" and finishes with CRLF. Lenth of the received string to be received varies.

I am using te hardware UART on an ATMEGA 16. Config Serialin0 = Buffered , Size = 60 , Bytematch = Catchchar
In the simulation I can type in the letters in the blue terminal emulator window but I do not see a possibility how type in CR and LF

How can I simulate the CRLF ?

In the IDE Options I checked Use integrated Simulator and I selected as com port COM 11.
In the Options sheet communication I also selected COM 11 with 19200,N,8;1

I wanted to use the BASCOM-AVR Terminal emulator.
I saw that I could define some commands with CRLF.
Somehow it does not work

How can I simulate complete commands ?

Appendix: The more I read in the forum the more I get uncertain about the function of _rs232inbuf0.
My understanding was that the buffer is filled quickly via interrupt and that I can read out this buffer.

Now I put an overlay over _rs232inbuf0 and saw that during simulation I have only the last character that I have typed in the buffer.
When typing very fast it may be 2 -2 characters. Is this a feature of the simulator ?
It is also strange to me that the simulator shows at _rs232inbuf0 e.g value 115 but the value for _rs_bufcountr0 shows the value 0, This means nothing in the buffer.
Do I get access to _rs232inbuf0 via waitkey() ?

It may be that I have a wrong understanding of the buffer and I must not use it as I thought.

Please be so kind and give me some advice.

best regards

Christian



(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1198
Location: France

france.gif
PostPosted: Mon Sep 11, 2017 5:12 pm    Post subject: Reply with quote

you know the char to start (F) ascii 70 and the char to stop (LF) ascii 10

so try something like that :
Open "com3:" For Binary As #1 'your com
Do
If Ischarwaiting(#1) = 1 Then
Jbyte = Waitkey(#1)

If Jbyte = 70 Then
Serialflag = 1 ' you start
End If
If Serialflag = 1 Then
If Jbyte = 10 Then 'the End of your sentence
Clear Serialin3 'very important to clear the buffer
Close #1
Exit Do
End If
Tabdata(kbyte) = Jbyte 'the array of data
Kbyte = Kbyte + 1
End If
End If
Loop

it is working for me
JP Wink
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1198
Location: France

france.gif
PostPosted: Mon Sep 11, 2017 5:51 pm    Post subject: Reply with quote

CR+LF means " Carriage return Line feed" so on the simulator, you don't see the character but you see a blank line
These codes come from the telex age !
Back to top
View user's profile Visit poster's website
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2165

netherlands.gif
PostPosted: Mon Sep 11, 2017 8:09 pm    Post subject: Re: Simulating UART Coommands and CRLF Reply with quote

Quote:

How can I simulate the CRLF ?

Just hit the enter key.

Or if you want some more controle, press on the numeric keypad alt+010 (LF) and alt+013(CR).
So hold down the alt key and pres 010 on the nummeriek keypad, release the alt key an the ascii code 10 (LF) is send.
To see if its working you can try for example the letter E, alt+ 069.

_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 82
Location: Aachen

germany.gif
PostPosted: Mon Sep 11, 2017 11:21 pm    Post subject: Reply with quote

Dear JP, Dear Evert,
many thanks for your advice. With the simulation I was successful. With my code I was not.(yet)
I will implements JPs method.
Please have a short look on my little piece of code. It basically branches to the right procedures but it does not wait until thew CRLF was received.
[/code]


Code:

Sub Eval_strings()
    B_valid = 0                                             ' Set Received String non valid
 
      Zeichen = "F" ' Zeichen is the String in which the command is stored


          Do
             B_cc = Inkey()


               Select Case B_cc
                   Case 0 :            Clear Serialin
                                            Exit Do
                   Case 13 :           B_invalid = 0 ' When 13 is received the 1st criterium for a valid string is achieved
                                            Clear Serialin
                                            Exit Do

                   Case 10 :            B_invalid = 0
                                            Clear Serialin
                                            Exit Do
                   Case 49 To 51 : Zeichen = Zeichen + Chr(b_cc)       ' Append to String

                   Case 65 To 90 : Zeichen = Zeichen + Chr(b_cc)
                End Select



          Loop

      If Len(zeichen) > 5 Then ' next criterium to have a valid string
                 B_command = Zeichen_byte(2)       ' extract command letter
      else
                B_invalid =1  ' String is not valid
      End If


    If B_invalid = 0 Then               'IS valid
      Select Case B_command             ' Branch accordung to command letter
         Case "S" : Call Meth_1            ' Clear Serialin is executed in Meth_x ( x=1,2,3)
         Case "H" : Call Meth_2                      
         Case "I" :  Call Meth_3                        
          End Select
       B_invalid = 1
   
     Else
       Clear Serialin                                      
    End If

Return
End Sub


 


Best regards
Christian
Back to top
View user's profile
autoguider

Bascom Member



Joined: 24 Sep 2007
Posts: 82
Location: Aachen

germany.gif
PostPosted: Tue Sep 12, 2017 12:26 pm    Post subject: Reply with quote

Dear All,
thanks to your advice my code seems to work.
As there may be also programmers that have the same problems I want to share the solution.

I introduced the variable Endflag that is set to 1 in case the carriage return is received. You can use basically any character that terminates the string that you want to extract.
I am not sure if _rs232inbuf0 is really reset or only the Headpointer / Endpointer.
I also go through the main loop of the program and call Eval_Strings.
When I use the loop as JP has it in his code there has to be the carriage return (Value 13) in the buffer. If not I stay in the loop. It was at least so in the simulation. In real life I did not test yet.
I think that I am now independent from the speed of transmission.

When JByte has the value 70 the serialflag is set. This indicates the start of the command string. Here you can adapt to your needs.
From there on all data that are allowed ( see case ...) will be added to the command string until carriage return is received.
When CR is received the serial in buffer is cleared, the index counter for the array which contains the command string is reset to 1 and endflag is set to 1.
The endflag indicates that the incoming string is finished. This means of course thaat the PC sends commands that have CRLF at the end.

Then it comes to the next check which is length of the string and if the endflag is set.
If both conditions are fulfilled the program can branch to the subroutines to handle the command.

Here some declarations:
Code:

Dim Zeichen As String * 60
Dim Zeichen_byte(61) As Byte At Zeichen Overlay
Dim Serialflag As Byte
Dim Endflag As Byte

Dim Kbyte As Byte
Dim Jbyte As Byte
 


Now the subroutine

Code:

'----------------------------------------------------------------------------------
'###############################################################################
'#                                                                             #
'#             Read seriell In / Evaluate String                               #
'#                                                                             #
'###############################################################################



Sub Eval_strings()
    B_invalid = 1                        ' Set Received String non valid
    Endflag = 0                         ' Set only when Carriage return received


  If Ischarwaiting() = 1 Then


   Jbyte = Waitkey()

      If Jbyte = 70 Then
         Serialflag = 1                 ' you start
      End If

      If Serialflag = 1 Then
         If Jbyte = 13 Then             'Carriage Return
            Clear Serialin0             'clear the serial buffer
            Kbyte = 1                   'Reset counter for array
            Serialflag = 0              'Reset serial flag
            Endflag = 1                 ' CR received, really the end
            'Exit Do
         End If


         'Here some further code filtering can be inserted

         Select Case Jbyte

           Case 49 To 51 : Zeichen_byte(kbyte) = Jbyte       'numbers 1-3
                           Kbyte = Kbyte + 1


           Case 65 To 90 : Zeichen_byte(kbyte) = Jbyte       'Capital letters
                          Kbyte = Kbyte + 1:

         End Select

      End If

  End If






      If Len(zeichen) > 5 And Endflag = 1 Then
      B_invalid = 0
      B_command = Zeichen_byte(2)       ' extract command letter
      End If


    If B_invalid = 0 Then                    '  is valid
      Select Case B_command             ' Branch according to command letter

     end select

       B_invalid = 1
       Endflag = 0
 Return
 End sub
 


Many thanks again for your help.

best regards

Christian
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR 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