storm32bgc
storm32bgc copied to clipboard
Serial CRC wird nicht ausgewertet
Hallo OlliW,
ich habe etliche V1.3x Gimbals, geupdatet mit V0.96. nun habe ich mich mal mit der seriellen Verbindung beschäftigt. folgendes ist mir aufgefallen:
- egal welchen CRC ich sende, ich erhalte immer ein Byte 3: 00, also laut: Wiki V1
0 = SERIALRCCMD_ACK_OK der gimbal führt den Pitch Winkel dann auch aus.
hier die debug Ausgabe:
Serielle Verbindung hergestellt
250
14
17
0
0
248
193
0
0
0
0
0
0
0
0
0
0
0
0
111
Pitch-Winkel gesendet: -31 °
Antwort erhalten:
Byte 0: FB (û)
Byte 1: 01 ()
Byte 2: 96 ()
Byte 3: 00 (
Byte 4: 62 (b)
Byte 5: 2E (.)
hier mein Code, ist nur schnell in PureBasic zusammengeklopft:
; Funktion zur Berechnung der X25 CRC-16 Prüfsumme
Procedure.i CRC16_X25(*data, length)
Protected crc.l = $FFFF
Protected i, j
For i = 1 To length - 1
;For i = 0 To length - 1
crc ! (PeekA(*data + i) << 8)
For j = 0 To 7
If crc & $8000
crc = (crc << 1) ! $1021
Else
crc << 1
EndIf
Next
Next
ProcedureReturn crc & $FFFF
EndProcedure
; Funktion zur Konvertierung von Float zu Byte-Array
Procedure ConvertFloatToBytes(float.f, *bytes)
CopyMemory(@float, *bytes, SizeOf(Float)) ; Konvertiere Float in 4 Byte
EndProcedure
; Öffne die serielle Schnittstelle mit Puffern
If OpenSerialPort(0, "COM5", 115200, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
Debug "Serielle Verbindung hergestellt"
; Befehls-Array vorbereiten
Dim command.a(19)
command(0) = $FA ; Start-Byte
command(1) = $0E ; Längenbyte
command(2) = $11 ; CMD_SETANGLE
pitchAngle = Random(90, 0) -45
rollAngle = 0.0
yawAngle = 0.0
; Konvertiere die Floats und füge sie in das Array ein
ConvertFloatToBytes(pitchAngle, @command(3)) ; Pitch in Bytes (4 Byte)
ConvertFloatToBytes(rollAngle, @command(7)) ; Roll in Bytes (4 Byte)
ConvertFloatToBytes(yawAngle, @command(11)) ; Yaw in Bytes (4 Byte)
command(15) = $00 ; Flag-Byte
command(16) = $00 ; Typ-Byte
; Prüfsumme berechnen
crc.i = CRC16_X25(@command(0), 16)
;command(17) = crc & $FF ; CRC Low-Byte
command(17) = $00
;command(18) = (crc >> 8) & $FF ; CRC High-Byte
command(18) = $00
command (19) = $6F
For i = 0 To 19
Debug command(i)
Next
; Befehl senden
If WriteSerialPortData(0, @command(0), 19)
Debug "Pitch-Winkel gesendet: " + Str(pitchAngle) + " °"
Else
Debug "Fehler beim Senden der Daten"
EndIf
Delay(500)
; Prüfen, ob Daten verfügbar sind
responseLength = AvailableSerialPortInput(0)
If responseLength > 0
Dim response.a(responseLength - 1)
ReadSerialPortData(0, @response(0), responseLength)
Debug "Antwort erhalten: "
For i = 0 To responseLength - 1
Debug "Byte " + Str(i) + ": " + RSet(Hex(response(i)), 2, "0") + " (" + Chr(response(i)) + ")"
Next
Else
Debug "Keine Antwort erhalten."
EndIf
CloseSerialPort(0)
Else
Debug "Fehler beim Öffnen der seriellen Schnittstelle"
EndIf