Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Array di bytes, estrarne uno

    Salve,
    sto facendo un programmino che comunica con un dispositivo via porta seriale.
    Funziona bene la trasmissione da e verso...

    codice:
    Private Sub SerialPort_DataReceived(...) Handles SerialPort1.DataReceived
            Dim bytes As Integer = SerialPort1.BytesToRead
            'create a byte array to hold the awaiting data
            Dim comBuffer As Byte() = New Byte(bytes - 1) {}
            'read the data and store it
            SerialPort1.Read(comBuffer, 0, bytes)
            'display the data to the user
            RichTextBox1.AppendText(ByteToHex(comBuffer))
    
    Private Function ByteToHex(comByte As Byte()) As String
            'create a new StringBuilder object
            Dim builder As New StringBuilder(comByte.Length * 3)
            'loop through each byte in the array
            For Each data As Byte In comByte
                'convert the byte to a string and add to the stringbuilder
                builder.Append(Convert.ToString(data, 16).PadLeft(2, "0"c).PadRight(3, " "c))
            Next
            'return the converted value
            Return builder.ToString().ToUpper()
        End Function
    Ora mi sono bloccata su un punto, riesco a fare una richiesta al dispositivo ed esso correttamente mi risponde, ed ho in esadecimale: FE FE 00 AA 00 11 22 FD.

    Io dovrei prendere dall'array solo AA 00 11 22, come faccio?

  2. #2
    Utente di HTML.it L'avatar di patel
    Registrato dal
    Jan 2008
    Messaggi
    1,994
    invece di convertire in testo tutto l'array prelevi i dati dalla posizione 3 alla 6, quindi usa un ciclo del tipo
    for i = 3 to 6 invece del for each

  3. #3
    Risolto con questo:
    codice:
    Dim nuovoArray() = comBuffer.Skip(4).Take(4).ToArray()

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.