La fréquence de rafraîchissement du cadre d’affichage de l’appareil n’est pas nécessairement la même que la fréquence d’alimentation électrique.
Un post connexe récent et opportun :
support higher refresh rates screens, like 90hz and 120hz
La fréquence de rafraîchissement du cadre d’affichage de l’appareil n’est pas nécessairement la même que la fréquence d’alimentation électrique.
support higher refresh rates screens, like 90hz and 120hz
Sub Astreams1_NewData (Buffer() As Byte) ' bytes received
Dim ExpectedPacketLength As Int = 19
Dim ExpectedFirstByte As Byte = 0x85
Dim ExpectedLastByte As Byte = 0xF9
If Buffer.Length < ExpectedPacketLength Then
Log(DateTime.Now & "short packet" & Buffer.Length & "bytes")
Else
Dim FoundPacketAt As Int = -1
For LastByteAt = Buffer.Length - 1 To ExpectedPacketLength - 1 Step -1 'search for last complete packet
If Buffer(LastByteAt) = ExpectedLastByte Then 'if last byte is ok
Dim FirstByteAt As Int = LastByteAt - ExpectedPacketLength + 1
If Buffer(FirstByteAt) = ExpectedFirstByte Then 'and first byte is ok
FoundPacketAt = FirstByteAt 'then we'll call that a win!
Exit
End If
End If
Next
If FoundPacketAt < 0 Then
Log(DateTime.Now & " no packet found in " & Buffer.Length & " bytes")
Else
Dim TidyPacket(ExpectedPacketLength) As Byte
For I = 0 To ExpectedPacketLength - 1
TidyPacket(I) = Buffer(FoundPacketAt + I)
Next
HandleNixiePacket(TidyPacket)
End If
End If
End Sub
Sub HandleNixiePacket(B() As Byte)
Dim bc As ByteConverter
Log(DateTime.Now & TAB & bc. HexFromBytes(B))
End Sub
Waiting for debugger to connect...
Program started.
1709620898015 857F00AA0869B4AEAA2271493A04F470F3FBF9
1709620898034 857F00AA08F74EE8462602AC495843038719F9
1709620898034 857F00AA08832B23847D11B3AF968FB464CDF9
1709620898040 857F00AA088C36DFC40893DD57F54497CE9CF9
1709620898044 857F00AA081B89C462E479B3605CEA62DC3FF9
1709620898050 857F00AA080F81C5E3E2BFA55D8733683A6BF9
1709620898051 857F00AA088E3E63249136FF3DFF1CCB0732F9
1709620898052 857F00AA08F42001EE06A8146D760E0C8082F9
For LastByteAt = Buffer.Length - 1 To ExpectedPacketLength - 1 Step -1 'search for last complete packet
If True Or Buffer(LastByteAt) = ExpectedLastByte Then 'if last byte is ok
Dim FirstByteAt As Int = LastByteAt - ExpectedPacketLength + 1
If True Or Buffer(FirstByteAt) = ExpectedFirstByte Then 'and first byte is ok
FoundPacketAt = FirstByteAt 'then we'll call that a win!
Exit
End If
End If
Next
Si vous remplacez ce paramètre parHandleNixiePacket(TidyPacket)
CallSubDelayed2(Me, "HandleNixiePacket", TidyPacket)
If you change this to
you place the HandleNixiePacket call onto the queue and free up your app to handle the next _NewData event as soon as possible. You don't have to worry about out of order execution, since the queue is processed in a linear fashion.B4X:CallSubDelayed2(Me, "HandleNixiePacket", TidyPacket)
Vous n’avez pas à vous soucier de l’exécution dans le désordre
Private Sub serial_DataAvailable (Buffer() As Byte)
Dim ExpectedFirstByte As Byte = resdeci
If Buffer.Length < 16 Then
Log(DateTime.Now & "short packet" & Buffer.Length & "bytes")
Else
Dim FoundPacketAt As Int = -1
For LastByteAt = Buffer.Length - 1 To 16 - 1 Step -1 'search for last complete packet
If Buffer(LastByteAt) = 251 Then 'if last byte is ok
Dim FirstByteAt As Byte = Bit.And(0xFF,Buffer(0)) + Bit.shiftright(Buffer(2),4)
If Buffer(FirstByteAt) = ExpectedFirstByte Then 'and first byte is ok
FoundPacketAt = FirstByteAt 'then we'll call that a win!
Exit
End If
End If
Next
If FoundPacketAt < 0 Then
Log(DateTime.Now & " no packet found in " & Buffer.Length & " bytes")
Else
Dim TidyPacket(16) As Byte
For I = 0 To 16 - 1
TidyPacket(I) = Buffer(FoundPacketAt + I)
Next
CallSubDelayed2(Me, "HandleNixiePacket", TidyPacket)' HandleNixiePacket(TidyPacket)
End If
End If
End Sub
Sub HandleNixiePacket(B() As Byte)
Dim bc As ByteConverter
Log(DateTime.Now & TAB & bc. HexFromBytes(B))
End Sub
1709908803235 no packet found in 19 bytes
1709908803411 no packet found in 19 bytes
1709908803610 no packet found in 19 bytes
1709908803807 no packet found in 19 bytes
1709908803974 no packet found in 19 bytes
1709908804173 no packet found in 19 bytes
Sub Astreams1_NewData (Buffer() As Byte) ' bytes received
Dim ExpectedPacketLength As Int = 19
Dim ExpectedFirstByte As Byte = 0x85
Dim ExpectedLastByte As Byte = 0xF9
If Buffer.Length < ExpectedPacketLength Then
Log(DateTime.Now & "short packet" & Buffer.Length & "bytes")
Else
Dim FoundPacketAt As Int = -1
For LastByteAt = Buffer.Length - 1 To ExpectedPacketLength - 1 Step -1 'search for last complete packet
If Buffer(LastByteAt) = ExpectedLastByte Then 'if last byte is ok
Dim FirstByteAt As Int = LastByteAt - ExpectedPacketLength + 1
If Buffer(FirstByteAt) = ExpectedFirstByte Then 'and first byte is ok
FoundPacketAt = FirstByteAt 'then we'll call that a win!
Exit
End If
End If
Next
If FoundPacketAt < 0 Then
Dim bc As ByteConverter
Log(bc.HexFromBytes(Buffer))
Log(DateTime.Now & " no packet found in " & Buffer.Length & " bytes")
Else
Dim TidyPacket(ExpectedPacketLength) As Byte
For I = 0 To ExpectedPacketLength - 1
TidyPacket(I) = Buffer(FoundPacketAt + I)
Next
HandleNixiePacket(TidyPacket)
End If
End If
End Sub
Sub HandleNixiePacket(B() As Byte)
Dim bc As ByteConverter
Log(DateTime.Now & TAB & bc.HexFromBytes(B))
End Sub
Sub btnConnect_Click
If manager.GetDevices.Length = 0 Then
Label1.Text = "No USB devices."
Else
Dim device As UsbDevice = manager.GetDevices(0) 'the device
If manager.HasPermission(device) = False Then
manager.RequestPermission(device)
Else
usbserial.BUFFER_READ_SIZE = 64
usbserial.BUFFER_WRITE_SIZE = 64
usbserial.Initialize("serial", device, -1)
usbserial.BaudRate = 460800
' usbserial.bitStart = usbserial
usbserial.DataBits = usbserial.DATA_BITS_8
usbserial.StopBits = usbserial.Stop_bits_1
usbserial.Parity = usbserial.PARITY_NONE
usbserial.FlowControl = usbserial.FLOW_CONTROL_OFF
usbserial.StartReading
Label1.Text = "Connected"
End If
End If
End Sub
Private Sub serial_DataAvailable (Buffer() As Byte)
' Dim lenghth, fb0, fb1, fb2, fb3, fb4, fb5, fb6, fb7, fb8, fb9 As Int 'Log("L-E-N-G-T-H-") 'lenghth = Buffer.length 'Log (Bit.And(0xff,lenghth))
Dim ExpectedFirstByte As Byte = resdeci
' If Buffer.Length > 16 Then
If Buffer.Length < 16 Then
Log(DateTime.Now & "short packet" & Buffer.Length & "bytes")
Else
Dim FoundPacketAt As Int = -1
For LastByteAt = Buffer.Length - 1 To 16 - 1 Step -1 'search for last complete packet
If Buffer(LastByteAt) = 251 Then 'if last byte is ok
Dim FirstByteAt As Byte = Bit.And(0xFF,Buffer(0)) + Bit.shiftright(Buffer(2),4)
If Buffer(FirstByteAt) = ExpectedFirstByte Then 'and first byte is ok
FoundPacketAt = FirstByteAt 'then we'll call that a win!
Exit
End If
End If
Next
If FoundPacketAt < 0 Then
Dim bc As ByteConverter
Log(bc.HexFromBytes(Buffer))
Log(DateTime.Now & " no packet found in " & Buffer.Length & " bytes")
Else
Dim TidyPacket(16) As Byte
For I = 0 To 16 - 1
TidyPacket(I) = Buffer(FoundPacketAt + I)
Next
CallSubDelayed2(Me, "HandleNixiePacket", TidyPacket)' HandleNixiePacket(TidyPacket)
End If
End If
End Sub
Sub HandleNixiePacket(B() As Byte)
Dim bc As ByteConverter
Log(DateTime.Now & TAB & bc. HexFromBytes(B))
End Sub
1709187211650 19 857F00AA08000F0000122400D2410000F807F9
1709187211682 19 857F00AA0800000000157E00D24100005C00F9
1709187211698 19 857F00AA08000F000018D800D2410000B900F9
1709187211714 19 857F00AA08000000001C3200D34100001800F9
1709187211746 19 857F00AA08000F00001F8C00D34100007500F9
1709187211762 19 857F00AA08000F000022E600D3410000D200F9
1709187211794 19 857F00AA0800000000274000D44100003200F9
1709187211810 19 857F00AA08000F0000299A00D44100008E00F9
1709187211842 19 857F00AA08000F00002CF400D4410000EB00F9
422A002A45000100006FF300F4000031FBFBFB
422A002A45000000008AC300F700001FFBFBFB
422A002A4500000000A59300F800200DFBFBFB
422A002A4500000000BD6300F80050F8FBFBFB
422A002A4500000000D63300000000E4FBFBFB
422A002A4500000000F10300030000D2FBFBFB
422A002A45000000000DD400050000C1FBFBFB
422A002A450000000028A400080000AFFBFBFB
422A002A45000100003F74000B000099FBFBFB
Oui les paquet ont toujours une longueur de 19 octets et la valeur 252 est au 19e octet.
L'octet N°16 , car il y a 17 octets , vaut 251
Sub Process_Globals
...
Dim ExpectedPacketLength As Int = 19 'probably wrong but will be updated when valid-looking packet is received
Dim ExpectedFirstByte As Byte = 0x85 'ditto
Dim ExpectedLastByte As Byte = 0xF9 'ditto
End Sub
Sub Astreams1_NewData (Buffer() As Byte) ' bytes received
Log(DateTime.Now & TAB & "Buffer(" & Buffer.Length & ")") 'to help debugging; comment out when working
If Buffer.Length >= 9 And Buffer.Length <= 27 Then '18 +/- 50% (18 =approximate usual packet length = average of observed lengths 17 and 19)
Dim LastByte As Byte = Buffer(Buffer.Length - 1)
If Bit.And(LastByte, 0xF0) = 0xF0 Then 'terminal byte has value 240 or higher eg 251, 252
Dim LastByteIsUniqueFlag As Boolean = True
For I = 0 To Buffer.Length - 2
If Buffer(I) = LastByte Then
LastByteIsUniqueFlag = False
Exit
End If
Next
If LastByteIsUniqueFlag Then 'terminal byte not contained within packet data
ExpectedPacketLength = Buffer.Length
ExpectedFirstByte = Buffer(0)
ExpectedLastByte = LastByte
End If
End If
End If
If Buffer.Length < ExpectedPacketLength Then
Log(DateTime.Now & " short packet " & Buffer.Length & " bytes") 'feel free to replace with proper error handler
Else
Dim FoundPacketAt As Int = -1
For LastByteAt = Buffer.Length - 1 To ExpectedPacketLength - 1 Step -1 'search for last complete packet
If Buffer(LastByteAt) = ExpectedLastByte Then 'if last byte is ok
Dim FirstByteAt As Int = LastByteAt - ExpectedPacketLength + 1
If Buffer(FirstByteAt) = ExpectedFirstByte Then 'and first byte is ok
FoundPacketAt = FirstByteAt 'then we'll call that a win!
Exit
End If
End If
Next
If FoundPacketAt < 0 Then
Log(DateTime.Now & " no packet found in " & Buffer.Length & " bytes") 'feel free to replace with proper error handler
Else
Dim TidyPacket(ExpectedPacketLength) As Byte
For I = 0 To ExpectedPacketLength - 1
TidyPacket(I) = Buffer(FoundPacketAt + I)
Next
HandleNixiePacket(TidyPacket)
End If
End If
End Sub
Sub HandleNixiePacket(B() As Byte)
Dim bc As ByteConverter
Log(DateTime.Now & TAB & bc.HexFromBytes(B))
End Sub
Je ne comprends toujours pas. Oui, les communications série doivent être suffisamment rapides pour capturer toutes les données à capturer. Mais les données doivent-elles être traitées aussi vite qu’elles arrivent (toutes les 0,5 ms) ? Sinon, pourquoi capturer des paquets à ce rythme ? Augmentez simplement le tampon série entrant à la quantité de données que vous souhaitez traiter à un moment donné (disons 8 ms ?). Ensuite, l'événement _newData ne sera lancé que toutes les 8 ms sans aucune perte de données (s'il se produit, il n'a aucun rapport avec B4X, mais une limite du système sous-jacent lui-même). Tout traitement immédiat peut être effectué à ce moment-là et tout traitement supplémentaire peut ensuite être repoussé via un CallSubDelayed stratégique (comme l'affichage des données, le stockage des données, etc.).Ce qui ne fonctionne pas c'est que nous n'arrivons pas a recevoir les 17 octets de données séparés de 0,5 ms voire 0,3 ms.
I still don't understand. Yes, serial communications may need to be fast enough to capture whatever data needs to be captured. But does the data need to be processed as fast as it comes in (every .5ms)? If not, why capture packets at that rate? Just increase the incoming serial buffer to the amount of data you want to process at any given time (let's say 8ms?). Then the _newData event will only be thrown every 8ms without any data loss (if it does occur, it is unrelated to B4X, but a limit of the underlying system itself). Any immediate processing can be done at that time and any additional processing can then be pushed off via a strategic CallSubDelayed (such as displaying the data, storing the data, etc).
Augmentez simplement le tampon série entrant à la quantité de données que vous souhaitez traiter à un moment donné (disons 8 ms ?).
tout traitement supplémentaire peut ensuite être repoussé via un CallSubDelayed stratégique (comme l'affichage des données, le stockage des données, etc.)