Go Down

Topic: VB 2010 & Arduino Uno - Serial Communication (Read 10479 times) previous topic - next topic

PaulS

Code: [Select]
SerialPort1.Open()
SerialPort1.Write("ABC")
SerialPort1.Close()

Open the serial port, resetting the Arduino. While it is booting up, send it some data. Then, close the port, resetting the Arduino.

So, what do you then expect to see in the Serial Monitor?
The art of getting good answers lies in asking good questions.

quangdo1700

Hello HugoPT,
Thanks for detailed help. I tried as you suggested. Still nothing on serial monitor.
Well, I give up.
Thanks, again.

quangdo1700

Hello HugoPT,

By accident, I open the serial monitor and type in ABC then hit SEND.
Your suggestion works.

The question is now how i send "ABC" in VB to Arduino.
Here are my steps:
1. Open and run VB to send "ABC" to "COM4" (i.e. Open -> Write -.Close)
2. Upload sketch. Wait till load is done. Open serial monitor. Monitor is blank.
3. While the serial monitor is still on, I run VB again and receive a message saying that port "COM4" is busy.

Any idea ?



Robin2

Did you look at the Python program I gave a link to earlier. Your VB program will have to work in a similar way.

First upload our sketch to the Arduino and leave the Arduino connected to the PC.

Then start your VB program ensuring that it uses the correct Com port.

When the VB program opens the Com port it will cause the Arduino to reset - which takes a little time.

The VB program must wait until the Arduino has completed resetting before it tries to communicate with the Arduino.

A good way to do that is to have the Arduino send a short message at the end of setup() and make the VB program wait until it receives that message before doing anything else.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

hugo007

The reason is very simple.You can't open serial port at VB and simultaneously on arduino IDE!
Serial port becomes busy after gets opened.For you could see the result of sending the "ABC" from VB, just put a textbox in VB a make arduino code to send it back to you and fill the textbox with incoming data from the Serial.Like an eco you send ABC to arduino, arduino reads it and send it back to you showing on the VB textbox.
If this is more trouble for you to do then simply try to highlight onboard LED placed on pin 13 when the incoming string matches.
Some thing like this will work:
Code: [Select]

//You need space for the null terminator, since you want 3 characters you need an extra to the null terminator 3+1
#define STR 4
char buffer[STR];

//--------------------------------------------------
void setup(){
 Serial.begin(9600);  
pinMode(13,OUTPUT);  
digitalWrite(13,LOW);
}
//---------------------------------------------------
void loop(){
 if (Serial.available() <0) {   //Ok if you dont have nothing print like crazy ...
   Serial.print("Empty");
   delay(1000);
 }
 if (Serial.available() >=3) {//It will need 3 bytes to run, this way you insure it's safe to read the 3 bytes in a while loop
//Serial comunication are very slow, if you try to read the 3 bytes when you detect the first one you will not get the next because the second one hasn't arrived yet
   int i = 0;
   while (i <= 2) {
     Serial.print(i);
     buffer[i++] = Serial.read();//Use i and then increment it after
   }
   buffer[3] = '\0';//In order to print it below like a string you need to terminate the char array with a 0
   Serial.print("OK I received");
   if (strcmp(buffer, "ABC") == 0 ){//If the string match turn LED on about 2 seconds and turn it off again
     digitalWrite(13,HIGH);//
     delay(2000);
     digitalWrite(13,LOW);
   }
   Serial.println(buffer);    //Here you should get the string correctly
 }
}
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Robin2


The reason is very simple.You can't open serial port at VB and simultaneously on arduino IDE!


This true, but doesn't seem to be the OPs problem is he has accurately stated his process in Reply #17

There is no difficulty (on my Linux netbook) having the Arduino IDE open and able to upload sketches while at the same time running a JRuby or Python program that communicates with the Arduino. Of course I must complete the upload (or close the Arduino Serial Monitor) before trying to use the JRuby program, and vice versa.

As I have tried to show in Reply #18 I think the OP needs to reorganize the order in which things happen.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

quangdo1700

Gentlemen,
From your responses, I have learned a lot,. It means that I should be patient
Will try again.
Thank you all for taking time to looking into it.
Have a nice day.

quangdo1700


VB.NET

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Dim stringtosend As String
    Private MySerialPort As New SerialPort("COM4", 9600, Parity.None, 8, StopBits.One)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
        MySerialPort.Handshake = Handshake.None
        MySerialPort.Encoding = System.Text.Encoding.Default
    End Sub
    ''------------------------------------------------------
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim TextSend As String = "ABC;"
        MySerialPort.Open()
        MySerialPort.WriteTimeout = 2000
        MySerialPort.Write(TextSend)
        MySerialPort.Close()
        TextBox1.Text = "Sending: " + TextSend
    End Sub
End Class

SKETCH:

String Str;
//--------------------------------------------------
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(12, LOW);
}
//---------------------------------------------------
void loop(){
//---------------------------------
//blink LED while buffer is empty
  if (Serial.available() <= 0) {
    digitalWrite(13,HIGH);
    delay(500);
     digitalWrite(13,LOW);     }
  Serial.println("Buffer is Empty");
  delay(2000);
// read text string until ";" is found
  if (Serial.available() > 0) {
    digitalWrite(13,HIGH);
    Str = Serial.readStringUntil(';');
    delay(10000);
    Serial.println(Str);
    digitalWrite(13,LOW);
  }
}
----------------------------------------
My problem is:
Sketch works as intended with Sketch serial monitor
LED is on during receiving text string from VB.NET
However, nothing shown on Sketch serial monitor.

please help.
Thanks


PaulS

Was there some part of Reply #15 that confused you?
The art of getting good answers lies in asking good questions.

quangdo1700

Hello PaulS,
When VB and Sketch tested with sending a character as in LED_On/OFF program. There is no problem there.
However, sending a text string is a problem.
Thanks

Robin2


Hello PaulS,
When VB and Sketch tested with sending a character as in LED_On/OFF program. There is no problem there.
However, sending a text string is a problem.
Thanks


What has this to do with Reply #15 or Reply #18?

As far as I can see you have already been given all the information needed to resolve the issue.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up