2016-08-23 5 views
0

オシロスコープでデジタル化した232 UARTのサンプルがあります。 受け取ったデータをASCII文字でデコードするマクロを書きたいと思います。ここには が添付されています。信号は0から3ボルトに移動します。 サンプリング周波数は1MHZです。 どこから始めることができますか?オシロスコープでデジタル化されたシリアルライン信号をデコードするマクロをエクセルします(オーバーサンプリング)

http://www.tr3ma.com/Dati/C3lightbridge00004.txt

http://www.tr3ma.com/Dati/C3lightbridge00005.txt

+0

"受け取ったデータをASCII文字でデコードしますか?文字列内の各文字のascii値を取得しますか? – Brian

+0

あなたの最低値は-0.0283892で、私が見た最高値は3.28621でした(しかし、他の値はもっと高くなるかもしれません)。あなたの精密さのレベルは、3300万を超える潜在的な価値をもたらします。あなたの最初のステップは、3300万の値を26文字にマッピングする方法を決定することです。いったんそれを持っていれば、[各行のテキストを読む](http://stackoverflow.com/questions/11528694/read-parse-text-file-line-by-line-in-vba)の値と[return対応する文字](http://stackoverflow.com/questions/15746368/vba-if-then-or-statements)。 – Tim

+0

これらはデジタル化されたアナログ信号のサンプルです。オシロスコープでデジタル化された信号。それはオシロスコープでオーバーサンプリングされたシリアルラインデータです。併合を見ると、これはかなり明確です。 – Gaucho

答えて

1

私は解決しました。 https://wcscnet.com/tutorials/introduction-to-rs232-serial-communication/)を使用し、データがcrcやパリティなどでフォーマットされていると仮定すると、サンプリングされた信号の中に、スタートビット、8データビット、およびストップビット。 2ストップビットとパリティビットがあっても、私のコードは正常に動作するはずです。

だから、これは私が書いたマクロです:

Sub Serial232() 
' 
' Serial232 Macro 
' 

' 
Dim curSamplePos As Long 


Dim SamplingFrequency As Long 
SamplingFrequency = 1000000 '1MSps 
Dim SerialLineSpeed As Long 
SerialLineSpeed = 115200 '115200bps 
Dim oneBitDuration As Double 
oneBitDuration = 1/SerialLineSpeed 'time in seconds (8,68us) 
oneBitDurationSamples = SamplingFrequency/SerialLineSpeed '8,68 Samples 


Dim signalThreshold As Double 

signalThreshold = 1.7 



Dim totalNumberOfSamples As Long 
totalNumberOfSamples = ThisWorkbook.Worksheets(1).UsedRange.Rows.Count ' Rows.Count ' Range("B6").End(xlDown).Row 

'color all che cells to white: 
Range("B6:B" & totalNumberOfSamples).Interior.ColorIndex = 0 
Range("C6:C" & totalNumberOfSamples).Value = "" 

totalNumberOfSamples = ThisWorkbook.Worksheets(1).UsedRange.Rows.Count 
    'start from first sample, search the first transition (start bit) 
'from 1 to zero 

Dim MachineStatus As String 

MachineStatus = "searchBegin" 'status of the processign statusMachine 

Dim BitAquired As Long 'it's the counter of the bits, from 1 to 8, inside the byte 
Dim currentByte As String 'it's the byte aquired in binary format 

Dim CompleteDecodedASCII As String 
CompleteDecodedASCII = "" 
Dim completeDecodedHex As String 
completeDecodedHex = "" 

Dim remainder As Double 'remainder from quantization (from oversampling of the bit) 

Dim hexByte As String 'temporary variable where the info about last byte is stored 



For curSamplePos = 6 To totalNumberOfSamples 

Range("B" & curSamplePos).Interior.ColorIndex = 37 

Select Case MachineStatus 
    Case "searchBegin" 
     If (Val(Range("B" & curSamplePos).Value) < signalThreshold) Then 
      Range("C" & curSamplePos).Value = "Wait..Status:searchingBegin" 
     Else 
      'found the nothing 
      Range("C" & curSamplePos).Value = "Wait..Status:beginOfEmptyArea" 
      MachineStatus = "searchStartBit" 
     End If 
    Case "searchStartBit" 
      If (Val(Range("B" & curSamplePos).Value) > signalThreshold) Then 
      Range("C" & curSamplePos).Value = "Wait..Status:searchingStartBit" 
     Else 
      'found the start bit 
      Range("C" & curSamplePos).Value = "Wait..Status:beginOfStartBit" 

      'found begin of start bit 
      'now move to the middle of the start bit 
      curSamplePos = curSamplePos + Int(oneBitDurationSamples/2) 
      If (Val(Range("B" & curSamplePos).Value) > signalThreshold) Then 
       'maybe we found a spike, let's report it, and ignore it 
       Range("C" & curSamplePos).Value = "Error:Spike Found" 
      Else 

       Range("C" & curSamplePos).Value = "Wait..Status:middle Of start Bit" 

       MachineStatus = "acquireBits" 'go to next status 
       'reset the variable for the byte that we are going to aquire 
       BitAquired = 0 
       currentByte = "" 
       remainder = 0 
      End If 
     End If 

    Case "acquireBits" 
     'aquire bits 
     'move on the next bit to acquire 
     curSamplePos = curSamplePos + Int(oneBitDurationSamples) - 1 

     remainder = remainder + oneBitDurationSamples - Int(oneBitDurationSamples) 

     If (remainder > 1) Then 'if we accumulated big remainder, add one sample 
      curSamplePos = curSamplePos + 1 
      remainder = remainder - 1 
     End If 

     If (Val(Range("B" & curSamplePos).Value) > signalThreshold) Then 
      'found 1 
      currentByte = "1" & currentByte 
     Else 
      'found 0 
      currentByte = "0" & currentByte 
     End If 

     BitAquired = BitAquired + 1 

     Range("C" & curSamplePos).Value = "Wait..Status:middle Of Bit number " & BitAquired 

     If (BitAquired = 8) Then 
      'byte completed 
      'print the output 

      hexByte = Application.WorksheetFunction.Bin2Hex(currentByte) 


      completeDecodedHex = completeDecodedHex & "-" & hexByte 
      CompleteDecodedASCII = CompleteDecodedASCII & Chr(Application.WorksheetFunction.Bin2Dec(currentByte)) 
      Range("C" & curSamplePos).Value = "This is the last bit of the byte. Current Byte in BIN:" & currentByte & ", in HEX:" & hexByte & ", in ASCII:" & Chr(Application.WorksheetFunction.Bin2Dec(currentByte)) 

      'cerchiamo il bit di stop 
      MachineStatus = "searchStopBit" 
     End If 

    Case "searchStopBit" 
     'spostati al centro del bit di stop, che dovrebbe essere il prossimo 
     curSamplePos = curSamplePos + oneBitDurationSamples - 1 
     If (Val(Range("B" & curSamplePos).Value) > signalThreshold) Then 
      'trovato stop bit 
      Range("C" & curSamplePos).Value = "Wait..Status:middle Of Stop bit" 

     Else 

      'found 0 but the stop bit should be 1 
      'report error 
      Range("C" & curSamplePos).Value = "Error: Stop bit not found" 
     End If 

     'quindi ora possiamo ricominciare tutto daccapo. 
     curSamplePos = curSamplePos + 1 
     MachineStatus = "searchBegin" 

    Case Else 
End Select 
DoEvents 
Next 
    DoEvents 
    Range("C" & curSamplePos).Value = "Data ended. currently processing data: Current Byte in BIN:" & currentByte & ", in HEX:" & hexByte & ", in ASCII:" & Chr(Application.WorksheetFunction.Bin2Dec(currentByte)) 
    Range("C" & curSamplePos + 1).Value = "HEX Decoded Data: " & completeDecodedHex 
    Range("C" & curSamplePos + 2).Value = "ASCII Decoded Data: " & cleanString(CompleteDecodedASCII) 

    MsgBox "Done" 

End Sub 

Function cleanString(str As String) As String 
    Dim outstr As String 
    For i = 1 To Len(str) 

    If (Mid(str, i, 1) = Chr(0)) Then 
    outstr = outstr & " " 
    Else 
    outstr = outstr & Mid(str, i, 1) 
    End If 
Next 

cleanString = outstr 
End Function 

は、これがマクロが実行された後にExcelファイルです:あなたはどういう

http://www.tr3ma.com/Dati/C3lightbridge00003.xls http://www.tr3ma.com/Dati/C3lightbridge00004.xls http://www.tr3ma.com/Dati/C3lightbridge00005.xls

+1

ああ!あなたの質問から、デジタル化された波形をビットに、ビットをバイトに、バイトをASCIIに変換することを検討していたことは明らかでした。非常に素晴らしい解決策! – Tim

関連する問題