Page 1 of 1

Problema comunicazione seriale

PostPosted: 29 Nov 2016, 15:45
by tnnbascom
Salve,
nel codice allegato, invio il comando "IOCHK" , che viene ricevuto correttamente, ed entro nel blocco "lbl_iochk".
Nel loop di questo blocco, ho il problema che spesso, non viene ricevuto il comando "MENU" per uscire dal loop...

Come posso evitare?
Grazie.

Code: Select all
$regfile = "m32def.dat"
$crystal = 4000000
$baud = 38400
$hwstack = 40
$swstack = 32
$framesize = 40


Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Serialin = Buffered , Size = 254
Config Serialout = Buffered , Size = 254

Config Portb = Input
Config Portd.2 = Output                                     'buzzer

In0 Alias Pinb.0
In1 Alias Pinb.1
In2 Alias Pinb.2
In3 Alias Pinb.3
In4 Alias Pinb.4
In5 Alias Pinb.5
In6 Alias Pinb.6
In7 Alias Pinb.7

Buzzer Alias Portd.2

Declare Sub Bip

Dim Rx_cmd As Byte , Ric As Byte
Dim Command As String * 254 , Ioval_prec As String * 21 , Cmdsend As String * 21 , S As String * 5 , Cmd_tnn As String * 254
Dim Refresh As Word , Vadc0 As Word , Vadc2 As Word , Vadc4 As Word
Dim Count As Long

Waitms 250

Main_menu:

Command = ""
Ric = 0
Rx_cmd = 0

Do
   Rx_cmd = Ischarwaiting()

   If Rx_cmd = 1 Then
      Rx_cmd = Waitkey()
      Ric = 1
      Command = Command + Chr(rx_cmd)
      Waitms 1
   Else
      If Ric = 1 Then
         Ric = 0
         If Mid(command , 1 , 5) = "IOCHK" Then
            Bip
            Waitms 250
            Print "TNN";
            Waitms 1
            Gosub Check_tnn
            Goto Lbl_iochk
         End If
         Command = ""
      End If
   End If

Loop

Lbl_iochk:

   Refresh = 0
   IOVAL_prec = ""

   Command = ""
   Ric = 0
   Rx_cmd = 0

   Do
      Incr Refresh

      If Refresh = 2000 Then
         Refresh = 0
         Vadc0 = Getadc(0)
         Waitms 1
         Vadc2 = Getadc(2)
         Waitms 1
         Vadc4 = Getadc(4)
         S = Str(vadc0)
         Cmdsend = "ADCVL" + Format(s , "0000")
         S = Str(vadc2)
         Cmdsend = Cmdsend + Format(s , "0000")
         S = Str(vadc4)
         Cmdsend = Cmdsend + Format(s , "0000")

         Print Cmdsend;
         Waitms 1
         Gosub Check_tnn
      End If

      Cmdsend = "IOVAL" + Str(in0) + Str(in1) + Str(in2) + Str(in3) + Str(in4) + Str(in5) + Str(in6) + Str(in7)

      If Cmdsend <> IOVAL_prec Then
         Ioval_prec = Cmdsend
         Print Cmdsend;
         Waitms 1
         Gosub Check_tnn
      End If

      Rx_cmd = Ischarwaiting()

      If Rx_cmd = 1 Then
         Rx_cmd = Waitkey()
         Ric = 1
         Command = Command + Chr(rx_cmd)
         Waitms 1
      Else
         If Ric = 1 Then
            Ric = 0
            If Mid(command , 1 , 4) = "MENU" Then
               Bip
               Waitms 250                          'Waitms 1
               Print "TNN";
               Waitms 1
               Gosub Check_tnn
               Command = ""
               Exit Do
            End If
            Command = ""
         End If
      End If
   Loop

Goto Main_menu

End

'****** Check that you received the "TNN" code, indicating that the command sent was received correctly.
Check_tnn:

Count = 0
Cmd_tnn = ""
Do
   Rx_cmd = Ischarwaiting()
   If Rx_cmd = 1 Then
      Rx_cmd = Waitkey()
      Ric = 1
      Cmd_tnn = Cmd_tnn + Chr(rx_cmd)
      Waitms 1
   Else
      If Ric = 1 Then
         Ric = 0
         If Mid(cmd_tnn , 1 , 3) = "TNN" Then
            Cmd_tnn = ""
            Count = 0
            Exit Do
         End If
         Cmd_tnn = ""
      End If
   End If
  Incr Count
  If Count = 130000 Then                                    'waiting for about 3 seconds, if no response, alarm ....
    'Bip
    Count = 0
    ''RESET PIN
    Do
      Bip
      Waitms 500
    Loop
  End If
Loop
Return

Sub Bip
   Buzzer = 0
   Waitms 20
   Buzzer = 1
End Sub