SerialPort 101 for a beginner

Hello,
I have spent a lot of time trying to use the serial port command in the toolbox but i was unsucessful. I looked at the samples posted online but i always get errrors.
Can anyone explains what are the steps needed to have a sucessful communication (read and write) from and to a serialport.If you can post the code it would be great...Please help

Thank you



Answer this question

SerialPort 101 for a beginner

  • Ruud van der Werf

    Ok can you help me get this thing working by just filling in the missing code if there is.Here is my updated code with the open serialport

    Public Class Form1
    Dim StoreReceived As String


    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    SerialPort1.Open()
    StoreReceived = SerialPort1.ReadChar




    End Sub

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

    MessageBox.Show(StoreReceived)

    End Sub


    End Class


  • Jeffyoung1234

    StoreReceived should store whatever string value captured from the StoreReceived = SerialPort1.ReadExisting.
    Also do i need to open/close the connection or it's done when i use the readExisting command.
    Please explain
    Thanks


  • Jan Byvaly

    yes you do need to open and close the connection when you want to access the serial port

    there may not be any data being recieved hence why you will get a blank message box or even the fact that you have not opened a connection and data will not be recieved



  • raviparthan

    take a look at this:

    http://msdn2.microsoft.com/en-us/library/30swa673.aspx

    does this help

    Basically you create an instance of the serialport class, set its properties, then open the connection and "write()" data to it.

    To read the data recieved, you need to implement the DataRecieved event:

    http://msdn2.microsoft.com/en-us/library/ed55271k.aspx



  • Rod at Work

    Yes! it did work. Thank you for being patient with me. At this point i am getting a string of numbers from the serial port, i am going to store all of them in an array. But the problem is that i don't know how many characters i will be receiving in that case how can i creat an array with unknown size
    Second when i open the serial port. How can i check if i am still receiving data
    Here is what i have so far..Thank you again.

    Public Class Form1
    Dim StoreReceived(5) As String
    Dim i As Integer

    Private Sub btn_OpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OpenPort.Click

    SerialPort1.Open()
    If SerialPort1.IsOpen Then
    MessageBox.Show("Serial Port Is Open")
    End If

    End Sub

    Private Sub btn_ShowReceivedData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ShowReceivedData.Click
    For i = 0 To 5

    StoreReceived(i) = SerialPort1.ReadByte

    TextBox1.Text = StoreReceived(i)



    Next


    End Sub

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

    SerialPort1.Close()

    End Sub


    End Class


  • Sledgehammer

    there are no shortcuts, things have been simplified! :)

    you have to write the code, you maybe able to set properties from the Windows Designer however to read/write to the ports you have to write code otherwise how is it going to open/close/read/write to ports This is what a software developer does ;-)



  • Carl Daniel

    Well you can use an arraylist and just "add" the data read, so globally you would have:

    Dim theDataItemsRecieved as new ArrayList()

    then in your DataRecieved event, just "add" the data recieved:

    Me.theDataItemsRecieved.Add(SerialPort1.ReadExisting())

    you dont need to "check" if you are recieving data, the DataRecieved event will automatically get fired/executed when there is data to be read from the port.



  • tasleemarif

    what you have done so far is correct

    now to implement the dataRecieved event handler you can do this programmatically or via the Form designer so:

    click on the SerialPort object/control you have added on the form, then look at the properties Window. Click on the lightning icon, which indicates Events and double click on "DataRecieved".

    This will not create the DataRecieved event in your application for the serial port

     

    To write to the serial port, just enter:

    theSerialPort.Write("hi")

     

    or whatever, the serial port class has the Write() method which you can use to send data

     

    to read from the serial port, the DataRecieved event will be fired when there is data being recieved automatically.

     

     

    does this help

     

     

     



  • Asim Zeeshan

    **can you help me figure this out.I am just following what you said.
    From the beginning. I openned a form in Vb Express and then I dragged the serialPort icon from the toolbox to the form and i defined all it's properties like baud rate, the com port and all that.

    next i created a button icon on the form and them i double clicked it so that i would get into the form1.vb[design] ( the code view window)
    The following code are generated:**


    Public Class Form1

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


    End Sub
    End Class


    **at this point i need to open the port to read and write**
    ** The tutorial That you sent me mentions that i need to declare the **data received event handler.
    ** i am not sure about the code below,where should i included it in my **form1 code and how should i read/write from to the port
    Public Event DataReceived As SerialDataReceivedEventHandler

    Dim instance As SerialPort
    Dim handler As SerialDataReceivedEventHandler

    AddHandler instance.DataReceived, handle


    Thank you much





  • Matthew Langley

    you are getting a blank message box because the variable "StoreRecieved" has nothing there.

  • Ben-TSK



    Hello,
    Here is what i have at this point. I am just trying to read from the serial port . Am i missing anything all i get is a blank messagebox.




    Public Class Form1
    Dim StoreReceived As String

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    StoreReceived = SerialPort1.ReadExisting


    End Sub

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

    MessageBox.Show(StoreReceived)

    End Sub


    End Class


  • nate-d-o-double-g

    So from what i understand is that you created an instance of class arraylist() and you called it
    the dataItemsReceived .
    3 questions:

    Now when you say Me.theDataItemsRecieved.Add(SerialPort1.ReadExisting()) you are essentially storing whatever is read from the serial port into the dataItemsReceived object

    Also don't you need a loop to for the
    Me.theDataItemsRecieved.Add(SerialPort1.ReadExisting()) to store all the serial port stream

    and last question how can you display the content of the
    theDataItemsRecieved array i got an error when i used the for loop to do that since theDataItemsRecieved is an object and it's missing the dot operator


  • Joel Martinez

    Thanks but i have looked all that up. Can i drag the serialport1 icon from the toolbox and then set it's property in the property window without writing that in code .
    I still need to open the port and read/write from it. I am looking here for shortcuts.
    Thanks


  • Gurpreet Singh Gill

    thats incorrect.

    place a button on the form. Name it "open connection"

    double click this button

    type:

    SerialPort1.Open()

    on the form, place another button, name this "close connection"

    double click this button

    type:

    SerialPort1.Close()

    in your dataRecieved event handler, type:

    MessageBox.Show(StoreRecieved)



  • SerialPort 101 for a beginner