0

I have some samples of a 232 UART digitized with an oscilloscope. I want to write a macro to decode the received data in ascii characters. here annexed the file. the signal moves from 0 to 3 volts.. the sampling frequency is 1MHZ. Where could I start from?

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

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

Gaucho
  • 1,328
  • 1
  • 17
  • 32
  • What do you mean 'decode the received data in ascii characters'? Do you want to get an ascii value for each character in the string? – Brian Aug 23 '16 at 12:35
  • It appears your lowest value is -0.0283892 and the highest I saw was 3.28621 (but other values may be higher). It appears your level of precision then will give you over 33 million potential values. Your first step will be to determine how you want to map ~33 million values to 26 letters. Once you have that, [read each line of text](http://stackoverflow.com/questions/11528694/read-parse-text-file-line-by-line-in-vba) for the value, and [return the corresponding letter](http://stackoverflow.com/questions/15746368/vba-if-then-or-statements). – Tim Aug 23 '16 at 13:15
  • they are samples of digitized analog signal. a signal digitized with a oscilloscope. It is a serial line data, oversampled with a oscilloscope. looking at the annexed this is quite clear. – Gaucho Aug 23 '16 at 15:00

1 Answers1

1

I solved. according to serial line 232 specification (you can use this as reference: https://wcscnet.com/tutorials/introduction-to-rs232-serial-communication/ ) , and assuming that data is formatted wihout crc and parity etc.etc, I should find, inside my sampled signal, a start bit, 8 data bit and a stop bit. My code should works fine even if you have 2 stop bits and even parity bit.

So, this is the macro that I wrote:

 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

And this is the Excel file after the macro has run:

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

Gaucho
  • 1,328
  • 1
  • 17
  • 32
  • 1
    Ah! It was not clear from your question that you were looking to convert the digitized waveform into bits, the bits into bytes, and the bytes into ASCII. Very nice solution! – Tim Aug 23 '16 at 20:37