Visual Basic Program 'blocks' Arduinos reply.

Hi, at the moment i have a very simple program that turns a LED on and off using VB. Its the start of what will eventually become a much more complex program (once i get the hang of VB :P)

What i want to do:

The LED is toggled using a Tick Box.

When starting the program i want it to send the string ‘0,4,0,0,0’ over the serial.

the Arduino will then reply with either a ‘0’ (LED Off) or ‘1’ (LED On).

i’ve got the Program sending data to the Arduino perfectly. but the Arduino cant send anything. However it is able to send data when the Program is closed (i can see it using the Serial monitor).

Here is the whole code for VB (I’ve removed the code that i couldn’t get to work)

Public Class PtL

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With SerialPort1
            .PortName = "COM3"
            .BaudRate = 57600
        End With

        Try
            SerialPort1.Open()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If (SerialPort1.IsOpen()) Then
                SerialPort1.Close()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub SendComPortBytes(ByVal byte0 As Byte, ByVal byte1 As Byte, ByVal byte2 As Byte, ByVal byte3 As Byte, ByVal byte4 As Byte)
        Dim sendBuffer() As Byte = {0, 0, 0, 0, 0}

        sendBuffer(0) = byte0
        sendBuffer(1) = byte1
        sendBuffer(2) = byte2
        sendBuffer(3) = byte3
        sendBuffer(4) = byte4

        Try
            SerialPort1.Write(sendBuffer, 0, 5)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub CBoxLcdBL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBoxLcdBL.CheckedChanged

        If CBoxLcdBL.Checked = True Then
            SendComPortBytes(4, 0, 1, 0, 0)

        ElseIf CBoxLcdBL.Checked = False Then
            SendComPortBytes(4, 0, 0, 0, 0)
        End If

    End Sub
End Class

And the relevant Arduino Code

while (Serial.available() > 0) { // Loop until no more serial data is left
      for (int i=0; i <= 4; i++){
        serial_bucket[i] = Serial.read();

      }

**SNIP** (lots of comments)

if (serial_bucket[0] == 4){
        if (serial_bucket[1] == 0){
          if (serial_bucket[2] == 0){
            lcd_bl_set = 0;
          }
          if (serial_bucket[2] == 1){
            lcd_bl_set = 1;
            Serial.write(1);
          }
        }
      }

Thanks for the help, is there any other info that you need?

i've got the Program sending data to the Arduino perfectly. but the Arduino cant send anything.

The arduino is sending data as proven by the serial monitor. I know very little about VB, but in your VB code where do you read the serial port input buffer?

Hi zoomkat,

i have removed that part of the code because at the moment it is useless.

all the information for it is here http://msdn.microsoft.com/en-us/library/7ya7y41k.aspx

i forgot to mention that i used a port sniffer to see if the Arduino even sends anything to the PCs buffer, the computer doesn't get anything placed in its buffer while the program is running.

I looked at the MS site and the "language is not supported or no code example is available." indicates that maybe you can't read the serial input buffer. An easy way to test your VB program is to jumper the tx/rx pins on the arduino board, then have your VB program send a string to the arduino and see if it is received back by your VB program. To verify the test, I loaded the "blink" program into the arduino just to not have anything associated with the serial port running. Then jumpered the tx/rx pins, and what is entered in the serial monitor is echoed back. You can try the same with your VB program.

I have no problems using C#.

I simply use the DataReceived event on the SerialPort. Quick and easy.

VB can read data from Arduino without problem.

Show us the code that doesent work otherwise we are poking in the dark :-)

It's not clear from your post if you trying to use the serial monitor at the same time as running you VB app, but if you are that won't work. Only one program can have the serial port open at a time.

This code is suspicious:

while (Serial.available() > 0) { // Loop until no more serial data is left
      for (int i=0; i <= 4; i++){
        serial_bucket[i] = Serial.read();

      }

you check if there is something in the buffer (1 or more) and then read 4 (even if 4 characters aren’t there.)

Try this:

if(Serial.available()>3)
{
     for (int i=0; i <= 4; i++){
       serial_bucket[i] = Serial.read();
   
     // do work here, you have 4 characters.
}

I would also suggest a change. In your code, check for a ‘packet begin’ character. Wait for it at the arduino so you know the VB packet starts and you can then read 4 characters, such as:

if(Serial.available()>0)
   if(!Serial.read() == 'X')
      return;

// ok, packet begins - we wait now for 4 characters from VB.

if(Serial.available()>3)
{
     for (int i=0; i <= 4; i++){
       serial_bucket[i] = Serial.read();
   
     // do work here, you have 4 characters.
}

In the VB, send the X character, then your 4 characters. The X acts as a Sync so that the arduino doesn’t start reading half way through an already-transmitted packet, such as when VB was launched and started communicating before the arduino was ready.``

i found out what was wrong, when i set up the settings for the port i never told it what the read timeout should be.

At least i think that's what fixed it :p

Spinlock

you brought up some good points, ill look into it :)

Good morning

I already communicate with Arduino... using MSComm1.Output on VB6... The problem is when I try to receive info from Arduino using MSComm1_OnComm... appear me bad characters...

If MSComm1.CommEvent = comEvReceive Then InString = InString & MSComm1.Input ' Read comm port data End if

The arduino sends info to serial, as binary, hexadecimal, other??

Can you share with us the VB final solution?

Thanks on advance Best regards Pedro Ferrer

The arduino sends info to serial, as binary, hexadecimal, other??

Yes.

The answer depends on how you are telling the Arduino to send data. You haven't shared your code, so we can't tell you how YOUR code is doing it.

Hello PaulS

Thanks by your reply.

Here's the code

if (Serial.available()) { // Look for char in serial que and process if found command = Serial.read(); if (command == 84) { // If command = "T" then digitalWrite(13,LOW); Serial.print("U ARE THERE"); lcd.printIn("U ARE THERE"); delay(5000); Serial.print("U WERE THERE"); lcd.printIn("U WERE THERE"); digitalWrite(13,HIGH); } }

The problem is that I receive some strange characters on VB6...

Thanks on advance Best regards Pedro Ferrer

if (command == 84) {          // If command = "T" then

Why not make the code match the comment? Then, the comment won't be needed

if(command == 'T') {

From this code snippet, and the code snippet from the VB application, we can't see that you have the same baud rates set.

What kind of "bad characters" are you seeing in the VB application?

Hello Paul

Thanks by the tip.

On VB I have:

' Fire Rx Event Every Two Bytes MSComm1.RThreshold = 2 ' When Inputting Data, Input 2 Bytes at a time MSComm1.InputLen = 2 ' 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit MSComm1.Settings = "9600,N,8,1" ' Make sure DTR line is low to prevent Stamp reset MSComm1.DTREnable = False ' Open COM2 MSComm1.CommPort = 2 MSComm1.PortOpen = True

and on Arduino I have:

Serial.begin(9600);

about the bad characters, at the moment I'm at the office and can't reproduce them, but imagine a circle, with a line on top of the circle at 120º... "O\" more less...

Thanks Best regards Pedro Ferrer

' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 2

But the Arduino is only sending one byte values - each char is a byte. It looks like you are trying to read two byte sized characters.

I could be all wrong, though.