Mintoris Forum

Author Topic: Arduino Serial Snippet  (Read 1917 times)

Operator

  • Full Member
  • ***
  • Posts: 14
Arduino Serial Snippet
« on: Sep 11, 2014, 10:58 PM »
Read Serial Print Snippet with Arduino…

Code: [Select]

' Test-Snippet USB-Serial-Print/Arduino: will print all received data
' Tested with: Arduino Uno R3, Leonardo R3, Micro R3 and
' Nano (Clone : Deek Robot)
' Mod of USB Diagnostics Demo by Chuck
' by Operator/ MBasic 6.4 / Moto G / Android 4.4.4
'
' Make sure your phone/tablet has host, USB OTG capabilities
' Make sure your OTG cable is connected first to the
' phone and then followed by your extension or
' adapter cable to the Arduino board
' Load Arduino with AnalogReadSerial sample sketch included in the ide
' and increase the delay in the sketch to 200
' (avoids filling too much the serial buffer)

device$ = UsbGetFirstDevice$()
'devices$() = UsbGetDevices$()
'list devices$(), device$

if device$ = "" Then
  Print "No device connected"
Endif

UsbOpen 1, device$, 9600, 8, 1, 0

Print "State: "; UsbGetState(1)
Print "Error: "; UsbGetError$(1)

Print "Vendor ID: ";  UsbGetVendorID(device$)
Print "Product ID: "; UsbGetProductID(device$)
driver$ = UsbGetDriverName$(device$)

if driver$ = "" then
  Print "No driver - Device not supported"
 Else
  Print "Driver: "; driver$
Endif

if UsbGetState(1) <> 1 then
  Print "Connection failed"
Endif

Print
Print "Here is the (A0) read data…."

'Set RTS to 1 to enable Micro/Leonardo to
'send Data, Uno and Nano won't need this
'but work also with…Thx Chuck
UsbSetRTS 1,1


T0 = Time()
Looop:
records = records + 1
if UsbGetState(1) = 1 then
  size = UsbGetSize(1)
  'print "Size: "; size
  if size > 1 Then
    UsbRead 1, data$, size
    'dT --> timestep
    dT = Time()-T0
    T0 = Time()
    'Print "dT: ",dT, "Dsize: ",size, "Data: ", data$
    Print data$;
  Else
    Print "data size is 0"
    'Print UsbGetState(1)
  Endif
Else
  Print UsbGetError$(1)
  Print UsbGetState(1)
  Print "State <> 1"
Endif

'adjust loop speed
wait 500

'clear the printed data to avoid program slowing down
If records > 50 Then
  CLS
  records = 0
Endif
Goto Looop
« Last Edit: Sep 11, 2014, 11:03 PM by Operator »