Control Arduino with VB.Net/C# (The Code)

Since I'm unable to edit my other post about this subject, I just post a new one. (so people don't need to read the other post completely to get to the code...)

Here you can find the code of my current project:
http://www.gyps.nl/arduino

It's the code for controlling an Arduino via a .Net dll, so this can be used with all .Net languages.
The code consists of a VB.Net 2005 project and an Arduino sketch that can communicate over a serial connection.
It's my intention to use this code in my robotics project, but it's really meant as a common way of talking to my Arduino from within my .Net applications.

Greetings.

I found out that there was a problem with my project page.
(links didn't work right).
I fixed it now.

I added control of servos to the project.
Now it's possible to control the servos via commands in the Arduino .Net class.

I have to check that out one of these days :slight_smile:

New version available...
I added the DCMotor functions to use with the Adafruit motorshield.

Forgive my ignorance, but will this help to communicate with the Arduino via usb from my custom VB software? I am new to VB and Arduino and am trying to get the serial communication aspects figured out. Thanks.

Robbie,

This is a class (.dll) you can use from within your (.Net) application to communicate with the Arduino.
This transfers all (well, some) Arduino functionality to your program so you can control the Arduino via your vb program.
You have to 'include' the dll so you can use the functions from it.
This will also handle the serial (USB) communication.

If the functionality is not what your looking for you can at least have a look at the code to see how the serial data is handled if you have problems with that...

Hi there, Im new to arduino and I found your program very interesting. I was wondering if you could help me? Im using an arduino Mega(USB) and VB express. Ive modified the sketch(changed pin numbers to suit the Mega, and the pins that Im using for controlling servos) Im only interested in servos for the time being.
Iv also downloaded the source for the program, and changed the pins there too. The problem I have is when I run the program it doesnt connect the arduino. Theres an error message. Do I need to change a setting somewhere in the code to detect USB?

Gordon13,
I'm not sure if you can use the Arduino_Net dll with VB6.
Perhaps it needs to be COM-registered ,but at the moment the dll isn't compiled that way.
If you have Visual Studio 2005, you can edit the project to add an installer project that registers the dll via the (old) COM way.
If you can use the dll with VB6, it should use the comport you specify.
Can you tell me what error you get and what comport you specify?

I got it to work by changing to Com 1. But now I get another problem:

StartCommunication: Serial port is open
EnableServoPort: Port 22 is not allowed
EnableServoPort: Port 23 is not allowed
Connection is lost

So I guess i didnt define the pins properly. My servos are connected to pins 22 and 23, am I doing it right?
This is where I changed the pins for the servos:

VB

''enable servo pin 9(changed to pins 22 and 23)
            Arduino.EnableServoPort(22, True)

            ''enable servo pin 10
            Arduino.EnableServoPort(23, True)

Arduino

case 'Q':
      if (v == 1) {
        D[n] = 0;                       // disable digital pin when used for servo
        NServo[n-22].attach(n);          // servos start at pin 9(changed to pin 22)
      }
      else {
        NServo[n-22].detach();           // servos start at pin 9
      }
case 'O':
      NServo[n-23].write(v);              // write value to servo
      break;

Ok well I changed the code back to the way it was and connected the servos to pins 9 and 10. Looking back Im not sure what I was doing :stuck_out_tongue: I didnt understand the code, but now I think I do.
However Im still getting problems:

StartCommunication: Serial port is open
Connection is lost

Any thoughts?

Gordon13,
I think you use a wrong comport.
You need to look into your Windows Devices configuration what comport the USB connection is on.
When you installed the drivers for the Arduino, they created a virtual comport that you can see in the devices list.
I don't think that COM1 is used by the USB drivers, it should be a higher number.

The error you get is because the dll didn't receive a watchdog signal for some time, so it assumes that the connection isn't there.

Just make sure the comport you use is the right one.
(You should also see the right comport in the Arduino editor under Tools-Serial Port)

Ah great I got it working! he com port was wrong, I checked in device manager and found it :smiley:
Thanks a lot

Hey Vulture, thanks a million for developing this.
Unfortunately, since I'm such a dufuss I can't figure out how to use the dll. Where do I put it in the first place? when I put it there will it work in vb straight off or do I need to do something else first?
Please help.... I'm so desperate for a method to use vb with Arduino. :wink: