How to read data in visual basic 6.0 from usb port arduino.

I have made a stopwatch in visual basic 6.0. i want to turn it on and off when i get get signal from arduino board on serial port... i have attached the picture of stopwatch. the arduino code is.

int pin1 = 13;
int pin2 = 14;
void setup() {
  Serial.begin(9600);
  pinMode(pin1, INPUT);
  pinMode(pin2, INPUT);

}

void loop() {
if (pin1 = HIGH)
{
  Serial.write("PIN 1 is 1");
}
else
{
  Serial.write("PIN 1 is 0");
}
if (pin2 = HIGH)
{
  Serial.write("PIN 2 is 1");
}
else
{
  Serial.write("PIN 2 is 0");
}
}

Untitled.jpg

So what is your actual question? Are you asking for how to send the data from the Arduino, or what data to send, or how to receive it in Visual Basic? Or all three?

i have made arduino program to send data to usb port, my question is how can i program my visual basic GUI(shown in figure) to use that data in order to turn the timers on and off.

The first thing that you need to do is to get the Arduino code right
if (pin2 = HIGH)This code sets the pin2 variable to HIGH which is not what you want to do.

As to sending and interpreting a message, why complicate things ? Just send one character, perhaps 'H' or 'L', depending on the state of the input. This will be easy to parse in VB. Have you written any VB code to read the serial input and display what is received ?

@ hassaan_128,

Do you know how to handle Comm. port in VB 6.0 ?

i want to turn on the timer if i revieve 1 on usb port and stop it when i get 0.

Here is my Arduino Code

int pin1 = 13;
int pin2 = 14;
void setup() {
  Serial.begin(9600);
  pinMode(pin1, INPUT);
  pinMode(pin2, INPUT);

}

void loop() {
if (pin1 = HIGH)
{
  Serial.write("1");
  Serial.print('\n');
}
else
{
  Serial.write("0");
  Serial.print('\n');
}

delay (1000);
}

Here is the visual basic code

Private Sub Form_Load()
MSComm1.Handshaking = 0
    MSComm1.RThreshold = 0
    MSComm1.RTSEnable = False
    MSComm1.InputLen = 1
    MSComm1.CommPort = 4
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.SThreshold = 0
    MSComm1.PortOpen = True
    MSComm1.InputMode = 1
Timer1.Enabled = False
End Sub


Private Sub MSComm1_OnComm()
MSComm1.Enable = True
MyData = Form1.MSComm1.Input
If MyData = 1 Then
Timer1.Enabled = True
If MyData = 0 Then
Timer1.Enabled = False

End Sub


Private Sub Timer1_Timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
If Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End If
End Sub

The program running on the PC does not know or care what is on the other end of the com port, it just cares about the data it gets.

The Arduino forums are NOT the place to ask this kind of question! Had you read the instructions for this forum you would have known this!

Mark

hassaan_128:
Here is the visual basic code

It is a few years since I used VB but I can see you are sending the ASCII character '1' from your Arduino sketch but comparing it to a binary 1 (ordinal value) in your VB code.

ASCII '1' = Chr(49)

Private Sub Form_Load()
  With MSComm1
    .Handshaking = 0
    .RThreshold = 0
    .RTSEnable = False
    .SThreshold = 0
    .CommPort = 4
    .Settings = "9600,n,8,1"
    
    .InputLen = 1  

    .InputMode = comInputModeBinary    '1
    REM use predefined constants whenever possible
  End With

  MSComm1.PortOpen = True
  REM do not make changes to the port after it is opened

  Timer1.Enabled = True  'you should not disable the control

End Sub


Private Sub MSComm1_OnComm()

  ' MSComm1.Enable = True  
  REM The comm control must already be enabled 
  REM otherwise (this) OnComm event would not have fired.

  Dim MyData As Char

  With Form1.MSComm1

    REM the event may be fired with more than 1 byte in the input buffer
    While inBufferCount > 0

      REM literal type C must be specified with Option Strict On
      If MyData = "1"C Then
        Timer1.Enabled = True
      end if

      If MyData = "0"C Then
        Timer1.Enabled = False
      end if
    
      REM else character is discarded 

    Wend
    
End Sub

its still not working... :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

Private Sub Form_Load()
With MSComm1
    .Handshaking = 0
    .RThreshold = 0
    .RTSEnable = False
    .SThreshold = 0
    .CommPort = 4
    .Settings = "9600,n,8,1"
    
    .InputLen = 1

    .InputMode = comInputModeBinary    '1
    Rem use predefined constants whenever possible
  End With
   MSComm1.PortOpen = True
  Rem do not make changes to the port after it is opened

  Timer1.Enabled = False
End Sub

Private Sub MSComm1_OnComm()
 MSComm1.Enable = True
  'Rem The comm control must already be enabled
  Rem otherwise (this) OnComm event would not have fired.

  Dim MyData As String = MSComm1.ReadLine()

  With Form1.MSComm1

    Rem the event may be fired with more than 1 byte in the input buffer
    While InBufferCount > 0

      Rem literal type C must be specified with Option Strict On
      If MyData = "1" Then
        Timer1.Enabled = False
      End If

      If MyData = "0" Then
        Timer1.Enabled = True
      End If
    
      Rem else character is discarded

    Wend
    If Incoming Is Nothing Then
    Exit Do
End If
End Sub

Private Sub Timer1_Timer()
Label3.Caption = Val(Label3.Caption) + Val(1)
If Label3.Caption = 60 Then
Label2.Caption = Val(Label2.Caption) + Val(1)
Label3.Caption = 0
If Label2.Caption = 60 Then
Label1.Caption = Val(Label1.Caption) + Val(1)
Label2.Caption = 0
End If
End If
End Sub

How is it not working?

What fault finding have you done?

Have you at least proven you can communicate with the MSComm control at all?

as i run the program the timer starts, and then arduino can no longer show any data on its serial monitor means port is being accessed by VB.

then arduino can no longer show any data on its serial monitor means port is being accessed by VB.

Why would you want to display data on the Serial Monitor application? You are trying to talk to VB. Or, so you said.

i want to show on VB, i wast just checking on serial monitor what data am i sending to the com port, and then closed the serial monitor, as only one application can access the port at a time...

hassaan_128:
...as only one application can access the port at a time...

As you already know only one application can open the serial port, there would not be much value pointing it out to us.

You need to be a little more logical, methodical with your fault finding.

Rule #1. Read the documentation, thoroughly.
Rule #2. If it doesn't work how you expect, read the documentation again.
Rule #3. No, honestly, reading documentation is much quicker than not reading documentation.
:wink:

In your defence, I noticed M$ failed to provide an example of reading input from an OnComm() event handler, in the documentation. Anyhow, .RThreshold and .SThreshold can not be set to 0, when you are using the oncomm() event.