Italian [Risolto]Comportamento strano con if/or

stefanoxjx

Active Member
Licensed User
Longtime User
Ciao a tutti, ho questo codice che si comporta in modo strano:
B4X:
    Dim bc As ByteConverter
    Dim msg As String
    Dim t(1) As Byte
    Dim crc As Int

    If PacketLen = 0 And Data.Length >= 2 Then
        PacketLen = Bit.And(Data(1), 0xFF)
        PacketLen = Bit.ShiftLeft(PacketLen, 8)
        PacketLen = PacketLen + Bit.And(Data(0), 0xFF)
        PacketLen = Bit.And(PacketLen, 0xFFFF)
        Message.Clear
        If PacketLen > 256 Then PacketLen = 0
    End If

    Message.Append(Data)

    If Message.Length = PacketLen Then
        msg = bc.HexFromBytes(Message.ToArray)

        crc = CRC16Calc(Message.ToArray)
       
        Dim crc_l As Short
        Dim crc_h As Short
        crc_l = Bit.And(crc, 0xFF)
        crc_h = Bit.And(Bit.ShiftRight(crc, 8), 0xFF)

        t(0) = crc_h
        Log("CRC H: " & bc.HexFromBytes(t))
        t(0) = crc_l
        Log("CRC L: " & bc.HexFromBytes(t))

        If Data.Length >= 2 Then
            If ((Data(Data.Length-1) <> crc_h) Or (Data(Data.Length-2) <> crc_l)) Then
                t(0) = crc_l
                bc.HexFromBytes(t)
                msg = msg & CRLF &  " >>>>>> CRC calcolato: " & bc.HexFromBytes(t)
                t(0) = crc_h
                msg = msg & " " & bc.HexFromBytes(t) & " <<<<<<"
               
                edittext1.SetColorAnimated(1000, Colors.Red, Colors.White)
            End If
        End If      
       
        If msg.Length <> 0 Then UpdateEditText("Received: " & msg & CRLF & CRLF)
        PacketLen = 0
        PingReceived = True
    End If

Questo codice è contenuto in una funzione e non fa altro che ricevere dei dati via bluetooth fino ad arrivare alla lunghezza del pacchetto.
Fatto questo, calcola il checksum e verifica se corrisponde con quello presente nel pacchetto.
Se corrisponde scrive quanto ricevuto in una editbox, altrimenti scrive quanto ricevuto in una edibox aggiungendo il checksum calcolato per poterlo confrontare.
La cosa strana è che finchè lavoravo in modalità debug entrava nella if (If ((Data(Data.Length-1) <> crc_h) Or (Data(Data.Length-2) <> crc_l))...) sempre nonostante il checksum corrispondesse, ma se mettevo un breakpoint dentro alla if tutto funzionava a regola d'arte.
Quindi ho trasformato la "if" da com'era prima:
"If Data(Data.Length-1) <> crc_h Or Data(Data.Length-2) <> crc_l Then" a com'è adesso "If ((Data(Data.Length-1) <> crc_h) Or (Data(Data.Length-2) <> crc_l)) Then" e tutto ha iniziato a funzionare regolarmente.
Ora che ho compilato e caricato in modalità release, mi si presenta lo stesso problema.
Non capisco se mi sta sfuggendo qualcosa o se sono alle prese con un bug dell'ambiente di sviluppo.
Avete qualche idea?

Grazie.
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Metti dei log così vedi i valori che sono confrontati
 

stefanoxjx

Active Member
Licensed User
Longtime User
Già fatto e sono uguali, quindi non dovrebbe entrare nella if.
Ma quello che è strano è che se metto un breakpoint non entra nella if, se lo tolgo ci entra.
Se riesco più tardi provo a fare un video, perchè immagino non sia facile capire cosa succede.
 

Star-Dust

Expert
Licensed User
Longtime User
Allora è una questione di pausa. Metti uno Sleep(0) prima dell'IF
 

stefanoxjx

Active Member
Licensed User
Longtime User
Messo, ma non cambia nulla.
Ho provato vari valori di sleep da 0 a 5 ma il problema rimane.
 

Star-Dust

Expert
Licensed User
Longtime User
allora i tipi sono diversi. Scambiali:

B4X:
If crc_h<>Data(Data.Length-1)  Or crc_l <> Data(Data.Length-2) Then
Comunque metti nei log anche i confronti

B4X:
Log(crc_h<>Data(Data.Length-1))
log( crc_l <> Data(Data.Length-2) )
 
Last edited:

stefanoxjx

Active Member
Licensed User
Longtime User
allora i tipi sono diversi. Scambiali:

B4X:
If crc_h<>Data(Data.Length-1)  Or crc_l <> Data(Data.Length-2) Then

Avevi ragione, non so cosa mi sia passato per la mente quando ho definito "short" quelle variabili.
Ora le ho definite (correttamente) As Byte e tutto sembra funzionare :)

Grazie.
 
Top