469,326 Members | 1,600 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Serial communication

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
Aug 11 '09 #1
1 1788
tlhintoq
3,525 Expert 2GB
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
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?
Aug 11 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andreas Horneff | last post: by
3 posts views Thread by carmen | last post: by
6 posts views Thread by Leandro Berti via DotNetMonster.com | last post: by
2 posts views Thread by Marco Trapanese | last post: by
4 posts views Thread by max_mont | last post: by
reply views Thread by Dhananjay | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.