newbe question on VB.net. programming hanging

K

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
 
J

Jon Skeet [C# MVP]

ken said:
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.
 
G

google

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
 
K

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
 
J

Jon Skeet [C# MVP]

ken said:
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...
 
C

Cor Ligthert [MVP]

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
 
K

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top