Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Reading data trough RS232 interface

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

Bascom Member



Joined: 08 Nov 2016
Posts: 19

PostPosted: Wed Oct 30, 2019 1:06 pm    Post subject: Reading data trough RS232 interface Reply with quote

I have ATMEGA128A3U and on first port (COM1) read TXT data.
Would someone have an idea, how read this data?

First character is space, on the end of rows is CRLF.

Code:
chann rs  dBm MCC MNC BCC  C1  C2
 122  39  -71 293  41   5  40  40
 112  34  -76 293  41   0  31  31
 116  34  -76 293  41   6  35  35
 118  33  -77 293  41   5  34  34
 113  32  -78 293  41   7  29  29
 663  31  -79 293  41   1  26  26

OK


Number of rows is not constant but it is depend of situation, maximum is 10.
For me it will be good solution, that I can read each rows.

I tryed with Ischarwaiting() function and wait for "space", but it doesn't work.

This table appear each second, between this time uC read from the same COM port other data in TXT format.
With other data I don't have any problems, because each rows start with special character f.e. * or +.

Best regards M.

(BASCOM-AVR version : 2.0.8.2 )
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 677

poland.gif
PostPosted: Wed Oct 30, 2019 1:51 pm    Post subject: Reply with quote

Space is a character with value 32 so rest of the values also comes like a string with these spaces like you presents?

Code:

Const Max_len = 10

Dim Mystr(10) As String * Max_len
Dim Char As Byte , Idx As Byte , Row As Byte
Dim Got_msg As Byte , Receiving As Byte , Err_flag As Byte

Open "COM1:" For Binary As #1

Enable Interrupts

Do

 If 0 < Ischarwaiting(#1) Then

   Char = Waitkey(#1)

   Select Case Char
    Case 32                                                 'space
     If Receiving = 0 Then                                  'first space
      Receiving = 1
      Row = 1 : Idx = 0
     Else                                                   'next space
      Incr Row
       If Row > 10 Then
        Err_flag = 1 : Row = 1                              'this is probably error because no enter after 10 msg`s
       End If
     End If
    Case 13                                                 'simply ignore, swallow enter
    Case 10                                                 'LF sign->Parse new data
      Got_msg = 1
    Case Else
     If Receiving = 1 Then                                  'wait for first space for synchro
      Mystr(row) = Mystr(row) + Chr(char)
       Incr Idx
         If Idx = Max_len Then
          Err_flag = 1 : Incr Row                           'error cause msg is too big
         End If
     End If
   End Select

   If Got_msg = 1 Then                                      'parse new msg

    If Err_flag = 0 Then

      'here you have array(row) of strings


    Else

      'there you can judge if you want to parse corrupted data

     Err_flag = 0
    End If
    Got_msg = 0 : Receiving = 0                             'we wait for new start after first space Wink
   End If

 End If
Loop
End
Back to top
View user's profile Visit poster's website
matjazs

Bascom Member



Joined: 08 Nov 2016
Posts: 19

PostPosted: Tue Nov 12, 2019 7:53 am    Post subject: Reply with quote

EDC Than'k you very much. You are realy expert.

I tested this code and works OK. Otherwise all spaces in row disappear, but this is not a big problem.

Troubless appear when I recieve other text on the same COM port aproximately each second.
On this COM port recive also other text information with first character f.e. "+" or "*", or "$", but sometimes text start also with space.

How make solution with this mix of text is for me big problem Smile

Best regards, Matjaz
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 677

poland.gif
PostPosted: Tue Nov 12, 2019 8:44 am    Post subject: Reply with quote

If every message ends with CR LF then this can be quite simple. I dont even know why I dont saw this opportunity in the previous code Very Happy
There is many, many another ways to parse data.

Code:
Const Max_msg_len = 100    
Dim New_msg As String * Max_msg_len , Mystr(10) As String * 10
Dim Char As Byte , Idx As Byte , Got_msg As Byte , Tststr As String * 1

Do

 If 0 < Ischarwaiting(#1) Then

  Char = Waitkey(#1)

   Select Case Char
    Case 10 : Got_msg = 1
    Case 13                                                 'swallow/ignore
    Case Else
     New_msg = New_msg + Chr(char)
     Incr Idx : If Idx >= Max_msg_len Then Got_msg = 1      'force parsing or cancel msg. becaause it is too long
   End Select

    If Got_msg = 1 Then

      Tststr = Left(new_msg , 1)                            'take and check first character of the msg.

      Select Case Tststr

       Case " "                                             'space

         Idx = Split(new_msg , Mystr(1) , " ")              'split by spaces into the rows

       Case "+"

       Case "$"

       Case "*"

      End Select



     Got_msg = 0 : Idx = 0 : New_msg = "" 'prepare for new msg.
    End If
 End If


Loop
End
Back to top
View user's profile Visit poster's website
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