Read Serial Port in VB.Net 
Author Message
 Read Serial Port in VB.Net

I am trying to read a bytestream coming in from a serial port.  Heard that
in VB6 you could use something like "Open "LPT1" For Input As #1", but I
searched the web and saw that for .Net I would have to call the CreateFile()
Win32 API.  (Short using MSComm OCX, but that's in Visual Studio 6 and I
would like to avoid that.)

I am able to get a handle back from the CreateFile Win32 API call but I get
an overflow error when I use it to initialize a System.inPtr object.  I'm
stumped because the documentation shows that the constructor is overloaded
to take either an integer or a long.

I am using VB.Net Trial Edition 7.09492, .Net Framework 1.0.3705 if that
makes a difference.  (OS right now is Windows XP home edition.)   I just
installed VB.Net the first time a couple of hours ago--being that this is my
first VB.Net application, and I've never tried serial comms before, there is
a very good chance that I'm probably just doing something dumb.

Any ideas?  Here's the code:

Sub Main()
    Dim handle As Integer = CreateFile("COM1", GENERIC_READ, 0, 0,
OPEN_EXISTING, 0, 0)
    If handle <> -1 Then
        Dim h As New System.IntPtr(handle) 'GETTING OVERFLOW ERROR HERE
        Dim myStream As New System.IO.FileStream(h,
System.IO.FileAccess.ReadWrite, True)

        ' do stuff with the port
        Dim v As Integer = myStream.ReadByte()
        System.Console.WriteLine(v)
    End If
End Sub

Private Declare Function CreateFile Lib "kernel32" _
    Alias "CreateFileA" ( _
    ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    ByVal lpSecurityAttributes As Long, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) As Long

    ' Note: lpSecurityAttributes is really SECURITY_ATTRIBUTES ptr

    Private Const GENERIC_READ As Integer = &H80000000
    Private Const OPEN_EXISTING As Integer = 3



Fri, 04 Feb 2005 09:19:57 GMT  
 Read Serial Port in VB.Net
Hello,

I have a chapter on VB.NET in my book (see below).  It provides a
native VB.NET class, and various examples -- including ones that use ActiveX
controls.

You may download NETComm.ocx from my homepage (free), if you prefer to use
an ActiveX control.  Frankly, this is the way that I would suggest.  This
download includes some example code.

In addition, there is information on this subject on
www.msdn.microsoft.com/vbasic.

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.



Sat, 05 Feb 2005 00:30:34 GMT  
 Read Serial Port in VB.Net
I have your 2nd edition and have recommended it to several others
(they bought a copy, too). I have a similar concern with .NET. If I
may, I would like to post the URL of your information that was most
helpful to me:

http://ourworld.compuserve.com/homepages/richard_grier/NETCommOCX.htm

p.s., I will be buying the 3rd edition, now.  :)



Tue, 01 Mar 2005 01:20:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Read Serial port in VB.Net?

2. Reading a serial port from a VB.NET Windows Service

3. How to loop comm port control to read text files through serial port in VB3

4. Reading the serial port in VB

5. WANTED: VB MODULE TO READ DATA FROM SERIAL PORT

6. WANTED: VB MODULE TO READ DATA FROM SERIAL PORT

7. VB reading from serial ports

8. VB.NET newbie - help with programming serial port

9. serial port in vb .net

10. Serial Port in VB.NET

11. How to disable flow control on a serial port in VB.NET

12. OPENING SERIAL PORTS WITH VB.NET

 

 
Powered by phpBB® Forum Software