|
发表于 2016-8-5 17:24:07
|
显示全部楼层
你这主要就是把这个“CustomUSBeeSuiteDecoder.vb”转换一下就可以了嘛。这个是原代码吧?
- Option Explicit On
- Option Strict On
- Imports System.IO
- Public Class CustomUSBeeSuiteDecoder
- Declare Function SampleData Lib "usbeeste.dll" Alias "?LoggedData@@YGJK@Z" (ByVal Index As Integer) As UInteger
- ' The SampleData routine returns a 4 byte value that contains a single sample of all the signals
- ' The format of the 32 bits is as follows:
- '
- ' MSB LSB
- ' XXXXXXXXYYYYYYYYFEDCBA9876543210
- '
- ' where XXXXXXXX is Channel 2 Analog value (0=-10V, 255 = +10V)
- ' YYYYYYYY is Channel 1 Analog value (0=-10V, 255 = +10V)
- ' F is logic level (0 or 1) for channel F
- ' E is logic level (0 or 1) for channel E
- ' D is logic level (0 or 1) for channel D
- ' ...
- ' 0 is logic level (0 or 1) for channel 0
- Dim GTriggerSample As UInteger
- Dim GX1Sample As UInteger
- Dim GX2Sample As UInteger
- Public Sub SetCaptureParameters(ByVal TriggerSample As UInteger, ByVal X1Sample As UInteger, ByVal X2Sample As UInteger)
- ' This routine is called right at the end of a capture to pass the positions of the Trigger and X1 and X2 Cursors to the
- ' custom decoding process.
- GTriggerSample = TriggerSample
- GX1Sample = X1Sample
- GX2Sample = X2Sample
- End Sub
- Public Sub DecodeCustom(ByVal OutFilename As String, ByVal NumberOfSamples As Integer, ByVal RateIndex As Byte, ByVal Parameters As String)
- Dim OldSample As UInteger
- Try
- ' This is a custom bus decoder Processing Routine
- '
- ' The passed in variables are as follows:
- ' OutFilename - the file that all of the decoded Entries get written to. This is the file that the USBee Suite
- ' will read to display the data on the waveline.
- ' NumberOfSamples - How many samples are in the sample buffer
- ' RateIndex - An index of the sample rate that the samples were taken.
- ' 17=1Msps,27=2Msps,37=3Msps,47=4Msps,67=6Msps,87=8Msps,127=12Msps,167=16Msps,247=24Msps
- ' Parameters - User defined string passed from the USBee Suite user interface Channel Setting for the custom decoder.
- ' Use this string to pass in any parameters that your decoder needs to know, such as what channels to use
- ' in decoding, which protocol if you have multiple protocols supported here, and how you want the data formatted.
- ' Below is an example set of Custom Protocol decoders that show how to access the sample buffer and how to generate output that get sent to the screen.
- ' Setup the File Stream that stores the Output Entry Information
- Dim FS As New FileStream(OutFilename, FileMode.Append, FileAccess.Write)
- Dim BW As New BinaryWriter(FS)
- Dim SamplingRate As Double
- ' Determining the actual sample rate so it can be written in the OutputFile
- ' 17=1Msps,27=2Msps,37=3Msps,47=4Msps,67=6Msps,87=8Msps,127=12Msps,167=16Msps,247=24Msps
- If RateIndex = 17 Then
- SamplingRate = 1000000
- ElseIf RateIndex = 27 Then
- SamplingRate = 2000000
- ElseIf RateIndex = 37 Then
- SamplingRate = 3000000
- ElseIf RateIndex = 47 Then
- SamplingRate = 4000000
- ElseIf RateIndex = 67 Then
- SamplingRate = 6000000
- ElseIf RateIndex = 87 Then
- SamplingRate = 8000000
- ElseIf RateIndex = 127 Then
- SamplingRate = 12000000
- ElseIf RateIndex = 167 Then
- SamplingRate = 16000000
- ElseIf RateIndex = 247 Then
- SamplingRate = 24000000
- End If
- ' Since this file supports 3 different custom decoders, we need to see which one to run for this pass based on the Parameters string
- If CBool(InStr(Parameters.ToUpper, "CHANGE")) Then
- ' Sample Decoder that just detects when a signal changes state
- ' The signal to use for the detection is specified in the Parameters as the second parameter
- Dim Params() = Parameters.Split(CChar(" ,-"))
- Dim SignalToUse As Double = Val(Params(1))
- Dim SignalMask As Integer = 1 << CInt(SignalToUse) ' Make the mask that will mask off the channel we want in the sample
- ' Now go from the start of the samples to the end and process the decoder
- For Sample = 0 To NumberOfSamples - 1
- ' This example decoder places a label at every transition of a digital signal
- Dim DigitalChannel As UInteger = CUInt(SampleData(Sample) And SignalMask)
- If DigitalChannel <> OldSample Then
- WriteEntry(BW, CUInt(Sample), CUInt(Sample + 100), "Changed!")
- End If
- OldSample = DigitalChannel
- Next
- ElseIf CBool(InStr(Parameters.ToUpper, "RISE")) Then
- ' Sample Decoder that just detects when a signal changes state
- ' The signal to use for the detection is specified in the Parameters as the second parameter
- Dim Params() = Parameters.Split(CChar(" ,-"))
- Dim SignalToUse As Double = Val(Params(1))
- Dim SignalMask As Integer = 1 << CInt(SignalToUse) ' Make the mask that will mask off the channel we want in the sample
- ' Now go from the start of the samples to the end and process the decoder
- For Sample = 0 To NumberOfSamples - 1
- ' This example decoder places a label at every transition of digital signal 0
- Dim DigitalChannel As UInteger = CUInt(SampleData(Sample) And SignalMask)
- If (DigitalChannel <> OldSample) And (OldSample = 0) Then
- WriteEntry(BW, CUInt(Sample), CUInt(Sample + 100), "Rise!")
- End If
- OldSample = DigitalChannel
- Next
- ElseIf CBool(InStr(Parameters.ToUpper, "HELLOWORLD")) Then
- ' Simplest Decoder Possible
- ' Print Hello World at the start of the buffer
- WriteEntry(BW, 0, 100, "Hello World!")
- WriteEntry(BW, GTriggerSample, CUInt(GTriggerSample + 100), "Trigger Is Here!")
- WriteEntry(BW, GX1Sample, CUInt(GX1Sample + 100), "X1 Is Here!")
- WriteEntry(BW, GX2Sample, CUInt(GX2Sample + 100), "X2 Is Here!")
- ElseIf CBool(InStr(Parameters.ToUpper, "NECIR")) Then
- ' Sample Decoder that just detects when a signal changes state
- ' The signal to use for the detection is specified in the Parameters as the second parameter
- WriteEntry(BW, CUInt(0), CUInt(100), "NEC IR Decoder 3.0")
- Dim Params() = Parameters.Split(CChar(" ,-"))
- Dim SignalToUse As Double = Val(Params(1))
- Dim SignalMask As Integer = 1 << CInt(SignalToUse) 'Make the mask that will mask off the channel we want in the sample
- Const LOOKING_FOR_HEADER As Integer = 1
- Const LOOKING_FOR_BITS As Integer = 2
- Dim DecodeState As Integer = LOOKING_FOR_HEADER ' Holds what state of the decoder we are in
- Dim ByteAccumlator As Integer = 0 ' Holds the accumulated bits for each byte
- Dim BitCounter As Integer = 0 ' Holds how many bits we have accumulated in this byte so far
- Dim ByteStartSample As Integer ' Holds the sample at the start of the byte
- Dim Data As UInteger ' Holds the state of the signal at the current sample
- Dim tEdge1 As Integer ' Where the first edge is
- Dim tEdge2 As Integer ' Where the second edge is
- Dim tPulseWidth As Double ' The pulsewidth in seconds
- ' Now go from the start of the samples to the end and process the signal
- For Sample As Integer = 0 To NumberOfSamples - 1
- Data = CUInt(SampleData(Sample) And SignalMask)
- If DecodeState = LOOKING_FOR_HEADER Then
- If Data <> 0 Then
- ' We found a High which starts the Header
- ' Now look for the next edge
- tEdge1 = WhereIsTheNextEdge(Sample, SignalMask, NumberOfSamples)
- If tEdge1 >= 0 Then
- ' Check to see if this falling edge is in the right timeframe
- tPulseWidth = (tEdge1 - Sample) / SamplingRate
- If (tPulseWidth >= 0.008) And (tPulseWidth <= 0.01) Then
- ' Now look for the rising edge
- tEdge2 = WhereIsTheNextEdge(tEdge1, SignalMask, NumberOfSamples)
- If tEdge2 >= 0 Then
- ' Check to see if this rising edge is in the right timeframe
- tPulseWidth = (tEdge2 - tEdge1) / SamplingRate
- If (tPulseWidth >= 0.004) And (tPulseWidth <= 0.005) Then
- ' Great! Valid Header Format! Look for bits from this point on
- ' Write out a Header Marker (remove this if you don't need the header)
- WriteEntry(BW, CUInt(Sample), CUInt(tEdge2), "Lead Code")
- DecodeState = LOOKING_FOR_BITS
- Sample = tEdge2
- ' Initialize the Byte Accumulation variables
- ByteStartSample = Sample
- ByteAccumlator = 0
- BitCounter = 0
- Continue For
- ElseIf (tPulseWidth >= 0.002) And (tPulseWidth <= 0.003) Then
- ' Great! Valid Repeat Format!
- ' Find the next falling edge to complete the Repeat
- tEdge2 = WhereIsTheNextEdge(tEdge2, SignalMask, NumberOfSamples)
- ' Write out a Repeat Marker (remove this if you don't need the Repeat)
- WriteEntry(BW, CUInt(Sample), CUInt(tEdge2), "Repeat")
- Sample = tEdge2
- Continue For
- Else
- ' Pulse is not the right size so bail and keep looking
- Sample = tEdge2
- Continue For
- End If
- Else
- ' No edges at all! So we are done
- Exit For
- End If
- Else
- ' Pulse is not the right size so bail and keep looking
- Sample = tEdge1
- Continue For
- End If
- Else
- ' No edges at all! So we are done
- Exit For
- End If
- End If
- ElseIf DecodeState = LOOKING_FOR_BITS Then
- If BitCounter = 8 Then
- ' We have an entire byte worth of data so output the information
- WriteEntry(BW, CUInt(ByteStartSample), CUInt(tEdge2), Hex(ByteAccumlator))
- BitCounter = 0
- ByteAccumlator = 0
- End If
- If Data <> 0 Then
- ' We found a High which starts the bit
- ' Now look for the next edge
- tEdge1 = WhereIsTheNextEdge(Sample, SignalMask, NumberOfSamples)
- If tEdge1 >= 0 Then
- ' Check to see if this falling edge is in the right timeframe
- tPulseWidth = (tEdge1 - Sample) / SamplingRate
- If (tPulseWidth >= 0.0005) And (tPulseWidth <= 0.0007) Then
- ' Good start of a bit
- ' Now look for the rising edge
- tEdge2 = WhereIsTheNextEdge(tEdge1, SignalMask, NumberOfSamples)
- If tEdge2 >= 0 Then
- ' Check to see if this rising edge is in the right timeframe for a logic "0"
- tPulseWidth = (tEdge2 - tEdge1) / SamplingRate
- If (tPulseWidth >= 0.0004) And (tPulseWidth <= 0.0006) Then
- ' Great! Valid 0 Bit Format!
- ' Write out a Bit Marker (remove this if you don't need the bit)
- 'WriteEntry(BW, CUInt(Sample), CUInt(tEdge2), "0")
- ' Add this bit to the accumulators (LSB first)
- ByteAccumlator = ByteAccumlator >> 1 ' Shift the Accumulator
- ByteAccumlator = ByteAccumlator And &H7F ' Clear out the MSBit
- ' Mark the start of the byte if so
- If BitCounter = 0 Then ByteStartSample = Sample
- ' Next Bit next time
- BitCounter = BitCounter + 1
- Sample = tEdge2
- Continue For
- ElseIf (tPulseWidth >= 0.001) And (tPulseWidth <= 0.002) Then
- ' Great! Valid 1 Bit Format!
- ' Write out a Bit Marker (remove this if you don't need the bit)
- 'WriteEntry(BW, CUInt(Sample), CUInt(tEdge2), "1")
- ' Add this bit to the accumulators (LSB first)
- ByteAccumlator = ByteAccumlator >> 1 ' Shift the Accumulator
- ByteAccumlator = ByteAccumlator Or &H80 ' Set the MSBit
- ' Mark the start of the byte if so
- If BitCounter = 0 Then ByteStartSample = Sample
- ' Next Bit next time
- BitCounter = BitCounter + 1
- Sample = tEdge2
- Continue For
- Else
- ' Pulse is not the right size so bail and keep looking
- DecodeState = LOOKING_FOR_HEADER
- Sample = tEdge2
- Continue For
- End If
- Else
- ' No edges at all! So we are done
- Exit For
- End If
- Else
- ' Pulse is not the right size so bail and keep looking
- Sample = tEdge1
- DecodeState = LOOKING_FOR_HEADER
- Continue For
- End If
- Else
- ' No edges at all! So we are done
- Exit For
- End If
- End If
- End If
- Next
- End If
- ' Close the Output File
- FS.Close()
- Catch ex As Exception
- End Try
- End Sub
- Public Function WhereIsTheNextEdge(ByVal tSample As Integer, ByVal tSignalMask As Integer, ByVal NumberOfSamples As Integer) As Integer
- ' This function finds where the next edge is starting at Sample tSample
- Dim OldData As UInteger = CUInt(SampleData(tSample) And tSignalMask)
- For Sample As Integer = tSample To NumberOfSamples - 1
- Dim Data As UInteger = CUInt(SampleData(Sample) And tSignalMask)
- If Data <> OldData Then
- Return Sample
- End If
- Next
- Return -1
- End Function
- Public Sub WriteEntry(ByRef BW As BinaryWriter, ByVal StartSample As UInt32, ByVal EndSample As UInt32, ByRef TextString As String)
- ' DO NOT CHANGE THIS ROUTINE!!!
- ' This routine writes the Entry in the file format that is used by the Custom Decoder
- ' This entry specifies the Start Sample, End Sample and the text string to display
- Try
- BW.Write(StartSample)
- BW.Write(EndSample)
- ' Write the length of the string in bytes (include the 0 at the end in the count)
- Dim tStrLen As UInt32
- tStrLen = CUInt(TextString.Length + 1)
- BW.Write(tStrLen)
- ' Now write out the characters one byte at a time and put a 0 at the end
- For x As Integer = 0 To CInt(tStrLen - 2)
- BW.Write(CByte(Asc(TextString.Chars(x))))
- Next
- BW.Write(CByte(0))
- Catch ex As Exception
- End Try
- End Sub
- End Class
复制代码 |
|