Go Down

Topic: VB6 Communication - Serial (Read 1 time) previous topic - next topic


Well, since my keyboard has failed I am trying to send data to the Arduino Duemilanove through USB serial connection.

My receive LED lights up on the board when I use this code, but it doesn't run anything.

I am unable to use MSComm1.PortOpen = True in Visual Basic and Serial.begin(9600); in Arduino at the same time, even though everyone else claims to do this.

It says the port is already open in whichever program I start up second.

Here is my code, I'm a frustrated fella and just want something to work.  Thanks guys.

Private Sub Command1_Click()
    MSComm1.Output = "a"
End Sub

Private Sub Form_Load()
    MSComm1.CommPort = "5"
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.PortOpen = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
End Sub

int incomingByte = 0;   // for incoming serial data

void setup() {
   //Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps

void loop() {

   // send data only when you receive data:
   if (Serial.available() > 0) {
      // read the incoming byte:
      incomingByte = Serial.read();

                  digitalWrite(7, 1);
                  digitalWrite(7, 0);


Sep 16, 2011, 07:18 am Last Edit: Sep 16, 2011, 08:06 am by eddiea6987 Reason: 1
Nope, your serial can only be doing thing at a time either sending or receiving data ,
You also have to make sure the port your aduino is on is the same number your using on the VB side which probably is since your getting an "already open" error
you should not open the port on form load because then the port will be open and arduino will not get access to it only open your port when necessary and when arduino is NOT sending anything.

when VB is listening for a response from Arduino you should  have this inside a timer (note i use visual studio 2011)
so you might have to adjust the code a bit idk.
it checks to see if there is anything to read on the serial line.

If my_SerialPort.BytesToRead > 0 Then
         ''''here put what ever you want it to do with the incoming data
         '' also note you can either read byte by byte or read everything at once like so  
           Label2.Text = my_SerialPort.ReadExisting

          my_SerialPort.DiscardInBuffer() ' this clears the buffer

       End If

now when you are sending data  you should open your port and send the data then close it again
also remember that if you send data with println in arduino that adds a line terminator
which you cannot seee , but VB WILL see it so if you have for example an if statement in VB saying

if received data = A  
VB will not recognize it because its sees A and the line terminator so the If statement will not be true.. what you can do is add your own line terminator in arduino but use print instead of println like this
Serial.print("A#"); ' where # is the line terminator

and on the vb side you have to specify to your comm port what the line terminator is so
 my_SerialPort.NewLine = "#"   again check equivilant for VB6

if you have Arduino "printing" a million things a second your vb app will never get access to the comm port, think of it as a small street you can only travel a certain direction one car at a time. so if arduino is sending data vb has to be listening and if vb is sending data arduino cannot be also sending data..

you should get visual studio 2011 its free anyways!! that way i would just give you the app.
I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"


Well, I plan on only sending data to the arduino.

Thanks for the help, I'll edit up some code and see if it works and paste it here to see what happens.


well i found the problem during posting this response....

i didnt set my pin 8 as an output....

sorry guys <3

Go Up