Arduino to VB6 Communication Not Functioning as Expected

Below is the code I created in an attempt to transmit data from Arduino to Visual Basic 6:

VB Code:

Dim temp(2) As String

Dim Buffer As String

Private Sub Form_Load()
Arduino.CommPort = 4
Arduino.Settings = "9600,N,8,1"
Text1.MultiLine = True
End Sub

Private Sub Command1_Click()
Arduino.PortOpen = True
End Sub

Private Sub Arduino_OnComm()
If (Arduino.CommEvent = comEvReceive) Then
    Buffer = Arduino.Input
    temp = Split(Buffer, " ")
    Text1.Text = temp(0) + vbCrLf + temp(1)
    Arduino.InBufferCount = 0
    Arduino.Output = (" ")
End If
End Sub

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

Arduino Code:

[color=#CC6600]long[/color] randNum;

char chInput;
byte ctr;

void [color=#CC6600]setup/color{
  Serial.[color=#CC6600]begin/color;
  [color=#CC6600]randomSeed/color;
}
void [color=#CC6600]loop/color{
  for (ctr=1; ctr <= 2; ctr++){  //transmit two random integers from 0 - 99 as a test
    randNum = [color=#CC6600]random/color;
    if (randNum < 10){
      Serial.[color=#CC6600]print/color;
    }
    Serial.[color=#CC6600]print/color;
    Serial.print(" ");
  }
  Serial.println(" ");
  do {
    chInput = Serial.[color=#CC6600]read/color;
    [color=#CC6600]delay/color;
  } while (chInput != 32); //wait for spacebar
}

Although the Arduino sketch is running fine, I can't run the VB6 form properly because it always crashes as soon as I click the command button.

Could anyone tell me what's wrong with my code?

At least the Arduino code is wrong, here a modified verseion, give it a try ...

long randNum;
char chInput;
byte ctr;

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop()
{
  for (ctr=0; ctr < 2; ctr++)  // for loop C-style :)
  {
    randNum = random(100);
    if (randNum < 10)
    {
      Serial.print("0");
    }
    Serial.print(randNum);
    Serial.print(" ");
  }
  Serial.println(" ");
  
  do 
  {
    if (Serial.available() >0) 
    {
      chInput = Serial.read();
    }
  }
  while (chInput != 32); //wait for spacebar
}

This might be obvious :slight_smile:

Are you using the correct comm port in your VB code?

Is anyting else (Arduino IDE) using the port ?

So far, I'm certain that I'm using COM4 for my VB code, where I connected the Arduino. Also, nothing else is using it.

I'm trying to execute the VB code with the Arduino IDE closed, because I assume that the sketch is already stored in the MCU. I think something wrong is going on when VB is trying to open the com port, leading to a freeze.

Private Sub Command1_Click()
Arduino.PortOpen = True
End Sub

PortOpen is a read-only property. This is NOT how you open the port.

So, in what other way could I open the port? It doesn't show up at the Properties sidebar.

EDIT: Should I move the command to Form_Load()?

EDIT: Should I move the command to Form_Load()?

You think that maybe if you do something wrong in a different place, that might work better? I guess you could try that, but I'm not counting on it working. A read-only property is read-only, no matter where you try to write to it.

There is an Open method that you should be using, instead. When Open() successfully opens the serial port specified, it sets the variable that the PortOpen property exposes to true.

Only OnComm() is the available function for the MSComm control when I checked its drop-down menu.

Hi all.

I'm afraid I have to disagree with PaulS :cold_sweat:. 'com_object.PortOpen = True' is how you open a comm port with VB6 Comm control. I'm no expert, but its worked for me in the past.

I have a peice of code that may come in handy for you. It checks the comm ports avaiable on the machine from 1 to 20 and puts a list of available com ports in a Listbox. You could then choose a port from this list, set the com_object.CommPort from this. Then set the com_object.Settings

' ### You will need a listbox on your form called list1 to use this code.
Private Sub Form_Load()
' ### list com ports on machine
    For I = 1 To 20                   ' ### Change this for different ports. Starts at com1 and goes to com20
    On Error Resume Next
    com.CommPort = I
    On Error Resume Next
    com.PortOpen = True
    On Error Resume Next
    com.PortOpen = False

    ' # if port exists add to list
    If (Str(Err.Number) = 0) Then
        List1.AddItem (Str(I))             ' ### Adds port number to list1 as a string
    End If
    Next I
End Sub

' ### the command below will assign the comport from the selected port in the list box
' ### com.CommPort = Val(List1.text)

I currently don't have access to a PC with VB6 on it but this is code I have used in the past. I've just chopped out a couple of bits and pasted them together.

Hope this helps

btw The port list routine isn't my work, its from zeb here

I decided to test the code again: once without the Arduino plugged in, and once with it plugged in. This time, I replaced the Arduino code with "Hello World" and I rewrote the VB6 code to read "Hello World" from Arduino's serial output.

Clicking the command button with Arduino.PortOpen = True never triggered a freeze until I plugged the Arduino in.

Testing it once more, I decided to use Arduino to send a single character through the serial port, which flawlessly worked. Then I used it to send a string, which VB6 failed to handle no matter the value of Arduino.RThreshold.

Should I bypass this through sending each character of the string one-by-one? Also, since this page suggests that an extra character would be needed in a character array to accommodate a null character, where does the null character go? The only solution seems to be converting the output numbers into strings.

Atleast set these parameter in your comm object, most parameter can set in design time:

inputMode=1 'comInputModeBinary ; why you can set text aswell as special character.
NullDiscard=true
RThreshold=1
RTSEnable=true
settings=9600,n,8,1 ' baud as your choice
commport=x 'x= your port ID
SThreshold=1