Serial communication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • InduGokul
    New Member
    • Jul 2009
    • 1

    Serial communication

    i want to perform serial communication on selecting

    .Net Framework2.0--->Visual Basic--->Device application

    presently i am using this code

    Code:
    Imports System.IO.Ports
    Imports System.IO.Ports.Serialport
    
    If Button1.Text Is "open port" then
        SerialPort1.Open()
        Button1.Text="close port"
        Button2.Enabled=True
    
    Else If Button1.Text Is "close port" then
        SerialPort1.Close()
        Button1.Text="open port"
        Button2.Enabled=False
    End If
    
    //Inside button2 i am writing this code
    SerialPort1.WriteLine(Textbox1.Text)
    Listbox1.Items.Add("sent:" +TextBox1.Text)
    
    //Inside serialport's property in DataReceived,i hav written this statement
    ListBox1.Items.Add("Received" +Serialport1.ReadLine())
    but i am getting error as

    "An unhandled exception of type System.InvalidO perationExcepti on occurred in System.dll
    Additional Information: The basestream is only available when the port is open "

    so plz anyone help me in this issue ...

    Thanking U in advance.....
    expecting ur valuable results earlier.....

    Regards
    Indu
    Last edited by tlhintoq; Aug 11 '09, 04:19 PM. Reason: [CODE] ...your code goes here... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Additional Information: The basestream is only available when the port is open "
    When you are sending the text to the port, the port is not open.
    Just because you issued a command to open the port you cannot assume that it worked. Maybe there is a problem with the port. Maybe the port number you specified is not valid. Maybe some other application or the OS closed the port after you opened it. Maybe your attempt to opent he port failed because you didn't specify everything correctly that you needed to.

    Coding is easy in a perfect world. Too bad we don't live in one. I have found that for every line of code there are 10 more to handle probable error conditions.

    Where you open the port
    Code:
    If Button1.Text Is "open port" then
        SerialPort1.Open()
        Button1.Text="close port"
        Button2.Enabled=True
    You never check to see if it really opened. You just assume it did and activate your send button.

    In your send method, do you check if the port is open before you try to shove text through it?

    Comment

    Working...