Skip to content

Instantly share code, notes, and snippets.

@dvsseed
Created October 18, 2017 00:11
Show Gist options
  • Save dvsseed/aac172259a6c8822eae314e53bf6a5b1 to your computer and use it in GitHub Desktop.
Save dvsseed/aac172259a6c8822eae314e53bf6a5b1 to your computer and use it in GitHub Desktop.
Excel and Micro:Bit through USB port
' Option Explicit
Dim StopReading As Boolean
Sub StartBtn_Click()
'
' StartBtn - Starts reading from the Micro:Bit into the grid
'
Dim COM_Byte As Byte
StopReading = False
Cells(1, 1) = "Reading..."
' Debug.Print "Reading..."
On Error GoTo ErrorHandler
Open "COM3:115200,N,8,1" For Random As #1 Len = 1 ' Open the com port(must be modified)
PrevRow = 0
Row = 0
While (StopReading = False)
EOL = False
Data$ = ""
While (Not EOL And Not StopReading)
Get #1, , COM_Byte
If COM_Byte = 13 Then
EOL = True
ElseIf (COM_Byte <> 10) And (COM_Byte <> 0) Then
Data$ = Data$ & Chr(COM_Byte)
End If
Wend
If Not StopReading And Left(Data$, 2) = "D:" Then
Row = (Row + 1) Mod 30
Cells(PrevRow + 2, 4) = "" ' Clear previous location indicator
Cells(Row + 2, 4) = "'==>>" ' Display new location indicator
Cells(Row + 2, 5) = "'" + Right(Data$, Len(Data$) - 2) ' Enter data into the grid as a string
PrevRow = Row
DoEvents
DoEvents ' Hack - Apparently, two DoEvents are required to get the charts to update live
End If
Wend
Cells(1, 1) = "Stopped!"
GoTo CloseFile
ErrorHandler:
Select Case Err.Number
Case 62 ' Nothing to read
Err.Clear
Resume Next
End Select
Cells(1, 1) = "Error :" + Err.Description
CloseFile:
Close #1
'
End Sub
Sub StopBtn_Click()
StopReading = True
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment