By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

newbe question on VB.net. programming hanging

P: n/a
ken
Hi all,
I copied this code from the examples. "How to: Receive Strings From
Serial Ports in Visual Basic" When I call the function and using the
single step method it hangs at Dim Incoming As String =
com1.ReadLine(). I don't have anything connect to Comm1. Does anyone
know why this is happening? Thanks for the help.
Regards,
Ken
Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""

Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1")
Do
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
End Using

Return returnStr
End Function
Jan 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ken <so******@out.there> wrote:
I copied this code from the examples. "How to: Receive Strings From
Serial Ports in Visual Basic" When I call the function and using the
single step method it hangs at Dim Incoming As String =
com1.ReadLine(). I don't have anything connect to Comm1. Does anyone
know why this is happening?


Yes - it's waiting for a line of data to come into Comm1. If you
haven't got anything on Comm1, it's never going to receive a line of
data. Think of it as a call to Console.ReadLine on a computer without a
keyboard attached.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #2

P: n/a
Possibly solutions to this are peek() (although not sure if this works
in all circumstances) - or more advisably, although more difficult, is
to run another thread.

Ian

Jan 16 '06 #3

P: n/a
ken
I understand that but, Don't the next two lines of code address that?
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do
Ken
Jan 16 '06 #4

P: n/a
ken <so******@out.there> wrote:
I understand that but, Don't the next two lines of code address that?
Dim Incoming As String = com1.ReadLine()
If Incoming Is Nothing Then
Exit Do


No, they won'y address it if ReadLine returns Nothing if there's
nothing attached to the serial port. Does the documention suggest that
that's the case? I can't see anything to that effect.

I'd expect a serial port to act as a potentially never-ending stream of
data. Then again, I don't know much about serial port programming...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #5

P: n/a
Ken,

If you want, you can ask this question in the newsgroup

microsoft.public.dotnet.languages.vb

However, put Serial Port in the subject of your question

Dick Grier will probably answer you then. He has probably an automatic
search on that. I thought he has this group as well in that search so you
can sent it as well with a new subject to this, but I am not sure of that
he searches here as well.

I hope this helps,

Cor
Jan 16 '06 #6

P: n/a
ken
Thank you Jon and Cor for the reply. Being new to Vb I need all the
help I can get and I will poet my question to
microsoft.public.dotnet.languages.vb in the future.
Regards,
Ken
Jan 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.