Connecting Tech Pros Worldwide Help | Site Map

Serial communication

Newbie
 
Join Date: Jul 2009
Posts: 1
#1: Aug 11 '09
i want to perform serial communication on selecting

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

presently i am using this code

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO.Ports
  2. Imports System.IO.Ports.Serialport
  3.  
  4. If Button1.Text Is "open port" then
  5.     SerialPort1.Open()
  6.     Button1.Text="close port"
  7.     Button2.Enabled=True
  8.  
  9. Else If Button1.Text Is "close port" then
  10.     SerialPort1.Close()
  11.     Button1.Text="open port"
  12.     Button2.Enabled=False
  13. End If
  14.  
  15. //Inside button2 i am writing this code
  16. SerialPort1.WriteLine(Textbox1.Text)
  17. Listbox1.Items.Add("sent:" +TextBox1.Text)
  18.  
  19. //Inside serialport's property in DataReceived,i hav written this statement
  20. ListBox1.Items.Add("Received" +Serialport1.ReadLine())
but i am getting error as

"An unhandled exception of type System.InvalidOperationException 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
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Aug 11 '09

re: Serial communication


Quote:
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
Quote:
Expand|Select|Wrap|Line Numbers
  1. If Button1.Text Is "open port" then
  2.     SerialPort1.Open()
  3.     Button1.Text="close port"
  4.     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?
Reply