Sending numbers via Serial with VB.

For starters you guys have to know that I'm very new to the world of micro-controllers and programming, so be gentle!

I've been messing around with the Arduino Mega for a while. (Doing a bunch of the small tutorials and the project "Secret door knock detector", Google it for more info)

I understand how it hangs together, the programming and outputs, inputs and all, but I can't write anything of my own.

What I want to do is to use Visual Basic to make my own program that can send "a, b, c, d, or e" and so on by clicking on buttons.

Like they do here : arduino.cc/en/Tutorial/SwitchCase2 - only without Serial Monitor.

So if I click for example a button in VB one of the LED's light up so I can switch the LED's with Relay's and use it to controll other things with pictures in a exe. program !

Bearing with me? :stuck_out_tongue:

Tell me if this made no sence at all. I just need some help getting this done ! :smiley:

If you've got the SwitchCase2 tutorial sorted and you know how it works with the serial monitor, then your problem is not with the Arduino, but with VB.
I think you need to ask your question in a VB forum.

Which version of VB?

The version 6 and before is very different from the .net versions.

AWOL @
Yeah, I see that now. But there might be someone that can help me here too.
Could you link me to a good VB forum? :slight_smile:

MikMo@
I have Visual Basic 2008 Express. I can probably get the 2006 version.

There is some information on interfacing VB.NET with the Arduino here:
Arduino <> Firmata <> Visual Basic .NET
Might at least get you started, with the software that you already have.

You can download the newest version of VB express for free from Microsoft.

To send data over serial use the seril port class.

Just search this forum for VB and you should find what you need to get started.

At least for VB6 it wasn’t too difficult.

  1. Add the MSComm control through the list of “Components.” You’ll find that on one of the pull-down menus in VB.

  2. The MSComm control shows up on your toolbar. Add it to the form. Select it and set the options. (9600,n,8,1) You’ll also need to set the COM port. With my FTDI cable connected, it showed up as COM9. This is usually determined by how many USB connectors you have on your computer.

  3. Write your code to send data using MSComm.

Use a command like this to send data from VB → Microcontroller:
Form1.MSComm1.Output = “”

  1. Write your code to receive data using MSComm.

Use a command like this to receive what’s in VB’s serial buffer to a string.
MyData = Form1.MSComm1.Input

There isn’t a great way (at least in VB6) to retrieve the data in the receive buffer… much like on the microcontroller’s receive buffer.

You’re best off using Timer/interrupts to poll the receive buffer on both sides.

Remember that there is an ASCII conversion to BYTE you need to account for when sending data to the microcontroller.

Remember that you’ll need to play with the ‘packet’ size in order to get decent performance. Sending one byte at a time uses a lot of overhead. If you are able, buffer up 64 bytes or so before you send your commands / data.

Wow, thanks NKnight760 !

Ill try that as soon as I get VB6 because I can't find it in 2008 :S

The MSCOM control doesent exist in VB versions after 6.

In the .net versions you can use the serial class, i think it's in the IO namespace.

Do I use the same coding oor..? Could you write the code to send an "b" ? :smiley:

This is for .net versions only. Code for VB 6 and the mscom control is compleatly different.

In your app. put the following to declare and open the port:

Imports System.IO

Dim WithEvents serialPort As New IO.Ports.SerialPort


Public Sub serialPortOpen()

        'Configure and open the serial port

        'If the port is already open, close it first
        If serialPort.IsOpen Then
            serialPort.Close()
        End If

        Try
            With serialPort
                .PortName = "your port goes here"
                .BaudRate = 9600
                .Encoding = System.Text.Encoding.ASCII
                .NewLine = Chr(13) + Chr(10)
            End With

            'Open the port and clear any junck in the input buffer
            serialPort.Open()
            serialPort.DiscardInBuffer()
           
        Catch Ex As Exception
              'Handle any exceptions here
        End Try
    End Sub

And whenever your program need to send something simply use somethig like:

serialPortOpen()
serialPort.write("b")

Simple right ?

You might not need / want this line:
.Encoding = System.Text.Encoding.ASCII

Opening and closing the serial port causes the Arduino to reset, unless it has been modified not to. Opening and closing the serial port every time you want to send stuff is generally not a good idea.

Opening and closing the serial port causes the Arduino to reset, unless it has been modified not to. Opening and closing the serial port every time you want to send stuff is generally not a good idea.

This is true in most cases, but I've been playing around with Visual Basic alot lately, I don't get the Auto-reset every time I open / close the Serial port. I think it has something to do with VB creating a new thread to handle the serial port.

I generally don't close the port every time something is sent, but I'm sending data rather quickly.. but I've tried closing and opening the port multiple times, along with the program, it resets the first time you open the Program, then not until you reset it by hand. (Or resetting the computer, using another program such as Arduino IDE)

Paul is right.

The serialportOpen() function should only be called once, not as a suggested above, every time you send.

Ill just sit here, silently watching till you guys figure a conclution. =D

till you guys figure a conclution

"conclusion"

Enjoy your wait.