Go Down

Topic: Serial to VBA Best solution (Read 3139 times) previous topic - next topic

skept

Hey Everyone,

I have just started using arduino and one of the first things i wanted to do was link my knowledge of VBA with arduino.
I spent hours and hours researching serial communications from VBA.
The more i researched the harder the task seemed.

Firstly, I just googled VBA serial comm and found pricey "shareware" solutions.. lol next

Secondly, i tried the seemingly common method of MSCOMM32.OCX
It was a actually hard to find an install process but i found it,
http://www.youtube.com/watch?v=Fhh5pOFrt48
The next issue with MSCOMM was how to use it.
I could not use it in a normal module and i beleive i had to make a form with the correct activex form buttons (which i could not find).
ie i could not get it to work.
I went around in cirlces and thought there has to be a better way.

Thirdly, I found a huge opensource VB code that supposedly used API??
I dont fully understand what was happening there and i did not want to update/ try and understand the huge code.

The last thing i did was find the solution :P.
BINARY ACCESS FILES!!!

Arduino Code:
This creates a sin curve when something is recevied and then "STOP"
Code: [Select]
int x = 0;
int row = 0;
String sReceived = "";
String STOP = "STOP";


void setup() {
  Serial.begin(9600);
}

void loop() {
  if((Serial.available()))
  {
    Serial.print("x,sin(x),");
    while(!sReceived.equals(STOP))
    {
      Serial.print(x);
      Serial.print(",");
      Serial.print(sin(x*PI/180));
      Serial.print(",");
      row++;
      x++;
      delay(100);
      if(Serial.available() > 0) sReceived = Serial.readString();
     
    }
    row = 0;
    x = 0;
    sReceived = "";
  }
}



VBA CODE:

Code: [Select]

Sub FETCHARDUINO()
Dim sGetChar As String
Dim sGetString As String
Dim x As Integer
Dim y As Integer
Dim xNum As Integer
Dim yNum As Integer


sendVal = "1"        'A string to send

Open "COM3" For Binary Access Read Write As #1 ' The magic
Put #1, , sendVal                               'Put sends data
cGetChar = Input(1, #1)
x = 0
y = 0
xNum = 2
yNum = 150
While (x <> yNum)
    y = 0
    x = x + 1
    While (y <> xNum)
        While (cGetChar <> ",")
       
        cGetString = cGetString + cGetChar
        cGetChar = Input(1, #1)        'get the next character
        Wend
    y = y + 1
    Cells(x, y) = cGetString         'put response in cell
    cGetString = ""
    cGetChar = ""
    Wend
Wend
Put #1, , "STOP"
Close #1


My coding is a little ADHOC and im open for suggestions (still a newb)
I havnt played with the delay or the baud rate
Enjoy

sonnyyu

You will find home at Windows PowerShell Script.

Quote
$port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one #opens serial port - adjust parameters accordingly
$port.open() #opens serial connection
$port.Write("Hello World") #writes your content to the serial connection
$port.Close() #closes serial connection


http://newsofthewolf.wordpress.com/2009/09/25/powershell-and-arduino/

http://blogs.msdn.com/b/powershell/archive/2006/08/31/writing-and-reading-info-from-serial-ports.aspx

Go Up