Another Arduino VB Serial question.

i cannot for the life of me get VB to read from the Serial with out it either say access is denied or just freezing and not doing anything.

First i did my research and came across this on the old forum
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1295434018

Tried it and yes i renamed everything accordingly but nothing.

1.I know my arduino is actually sending data because i press the serial monitor and i see my data that i want sent.
2. I can Send from VB to arduino just fine with this code

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       

     SendSerialData(txt1.Text) 'this here send whats inside a text box to Com4 and Arduino gets it all just fine

      
    End Sub

    Sub SendSerialData(ByVal data As String) 'this is the function that is sending my data from VB to Arduino
       
        Using com4 As IO.Ports.SerialPort =
                My.Computer.Ports.OpenSerialPort("COM4")
            com4.WriteLine(data)
           
        End Using
    End Sub

Now you would figure that something like “com4.ReadLine” would fetch my data but it does not
this is oneway of doing it without using the SerialPort control which i have also tried and nothing

One thing to Note and what i think may be the problem
If i send data from VB to Arduino to Blink an led it will do it just fine, SO LONG as i DO NOT have serial monitor open in Arduino because then it says access denied.
So with that little bug witch is fine i dont need serial monitor open to send or receive data but still when sending from Arduino to VB it does not work.
by the way im on an Arduino uno revision 2 and Ide version 0022 , and Visual Basic 2010 express.

on a totally random side note: I just got an accelerometer and Ping from parallax and oh man they are so beautiful <3

Here’s some VS 2008 code I’ve used before

Public Class Form1
    Dim stop_serial As Boolean = False


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim last_i As Integer = 255
        TextBox1.Clear()
        Do
            Try
                i = SerialPort1.ReadByte()
                If i <> last_i Then
                    TextBox1.Text = TextBox1.Text + Str(i) + "  " + Now + vbCrLf
                    TextBox1.Refresh()
                    last_i = i
                End If
            Catch ex As Exception
            End Try
            Application.DoEvents()
        Loop While Not stop_serial

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "Com39"
        SerialPort1.ReadTimeout = 10

        SerialPort1.Open()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        stop_serial = True
    End Sub
End Class

The DoEvents allows Button2’s click to be read which in turn allows the main loop to exit. Using a timeout also stops the serial call from hanging.


Rob

If i send data from VB to Arduino to Blink an led it will do it just fine, SO LONG as i DO NOT have serial monitor open in Arduino because then it says access denied.

Well, of course. There can be only two connections to a serial port at one time. The Arduino is on one end. The Serial Monitor OR the VB app can be on the other end, but not both.

but still when sending from Arduino to VB it does not work.

The code you posted doesn't read from the serial port. Where is your code for reading from the port?

Using com4 As IO.Ports.SerialPort =
                My.Computer.Ports.OpenSerialPort("COM4")
            com4.WriteLine(data)

Are you aware that opening the serial port resets the Arduino? Opening the port should be done once, not every time there is something to send.

very interesting, i was not aware that opening it resets the arduino, i will take note of that, how ever this is the code for receiving data , i got it from the MSDN library online http://msdn.microsoft.com/en-us/library/7ya7y41k.aspx

Function ReceiveSerialData() As String
        
        Dim returnStr As String = ""

        Dim com4 As IO.Ports.SerialPort = Nothing
        Try
            com4 = My.Computer.Ports.OpenSerialPort("COM4")
            com4.ReadTimeout = 10000
            Do
                Dim Incoming As String = com4.ReadLine()
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    returnStr &= Incoming & vbCrLf
                End If
            Loop
        Catch ex As TimeoutException
            returnStr = "Error: Serial Port read timed out."
        Finally
            If com4 IsNot Nothing Then com4.Close()
        End Try

        Return returnStr
    End Function

in response to what you said about not having to open the port every time im only pressing the button one time , One thing to note i noticed if i unplug my arduino and plug it back in via USB the Tx led will not blink which means its not sending my data? why not? if its connected to the computer? only time it send is if i upload the sketch again but never after it loses power, anyways thats a whole different issue , so i only test it after i upload a sketch to make sure VB is trying to read only while im actually transmitting data , what i think the problem is that Arduino is taking up both connection weather or not i have serial monitor open As far as i know to send data its just a matter of opening serial on arduino and matching baud rates and Serial.print or println

One thing to note i noticed if i unplug my arduino and plug it back in via USB the Tx led will not blink which means its not sending my data? why not?

Can you reasonably expect us to divine the answer to that question? You haven't posted any Arduino code.

only time it send is if i upload the sketch again but never after it loses power, anyways thats a whole different issue , so i only test it after i upload a sketch to make sure VB is trying to read only while im actually transmitting data , what i think the problem is that Arduino is taking up both connection weather or not i have serial monitor open

Unless you have a really strange setup, the Arduino can not possibly connect to both ends of the cable.

That code that you posted to read the serial data opens the port. That resets the Arduino. You really must revise the VB application to open the serial port ONCE and only once. Typically, that is done by putting a button on the form, "Connect", and then disabling that button after it is pressed, In the callback, open the port.

As far as i know to send data its just a matter of opening serial on arduino and matching baud rates and Serial.print or println

Or Serial.write().

Can you reasonably expect us to divine the answer to that question? You haven't posted any Arduino code.

void setup()
{
Serial.open (9600);

}
void loop()
{
Serial.print ("Hello world");
}

I could , and have, put code as simple as that and after unplugging the Arduino and plugging it back in to the USB or even the Power plug its does not transmit data.

And you guys where absolutely right , my code needed an over haul , I did what paul said and added a seprate button for connect and disconnect and just to make sure a disconnect when the window closes , i just need to clean it up a bit more because it works sometimes and sometimes freezes . this is the new form with a SerialPort component (my_SerialPort)

Public Class Form1

    Private Sub btn_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_connect.Click
        my_SerialPort.Open()

    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        my_SerialPort.Close()


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btn_ClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ClosePort.Click
        my_SerialPort.Close()
    End Sub

    Private Sub btn_Read_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Read.Click
        lbl1.Text = my_SerialPort.ReadLine
    End Sub

    Private Sub btn_Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Send.Click
        my_SerialPort.WriteLine(txt1.Text)
    End Sub
End Class

Serial.print doesn't send a CRLF, does my_SerialPort.ReadLine expect a CRLF before it returns a "line"? If so use Serial.println().

its does not transmit data

How do you know? You can't trust the VB prog yet so how do you know the Arduino is not transmitting?


Rob

How do you know? You can't trust the VB prog yet so how do you know the Arduino is not transmitting?

oh i know my code with VB is buggy i dont trust much but the Arduino not transmitting goes back since i was trying to Use some RF links once unplugged from usb after uploading a sketch to test the rf links range it worked no more, i also go by rx tx led, its blinks when transmitting right after i upload a sketch thats about it , only time i got the RF transmitter to work was when i used virtual wire thats the only time my arduinos can transmit data is through other pins not my designated rx tx pins , im wondering if i can use virtual wire to send to serial?

const char *msg = "hello"; vw_send((uint8_t *)msg, strlen(msg));

that right there is virtualWire's way of transmitting thorugh a digital pin , i guess its the equivilant of "serial.print" i wonder if used while on the computer without anything connected to the specified pin if it will just send through serial, perhaps ill try that but right now i have to go eat :)

update : just tried it , does not work , i didnt think it would