Use of serial port in Visual Basic .NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there any way of simply using the COM port from Visual Basic? All the
solutions I've found seem to require the use of Visual Studio whereas I only
have the the 'basic' version. I'm not after anything too complicated as all I
want to do is read a serial stream of data.

Thanks in advance
 
Thanks Cor

Sorry if I'm being a bit slow - but the search turned up many entries - none
of which seemed to answer simple question - do you know of any simple guide
 
p/invoke the win32 CreateFile, WriteFile, ReadFile, CloseHandle from
coredll.dll (see below). Call CreateFile with "COM1:" for eample and hey
presto you have an IntPtr to an open com port. From there use this handle
with the other stuff.

You might wanna create a class to wrap all this stuff to make it more OO and
to implement IDisposable on it.

[DllImport("coredll.dll")]

private static extern bool CloseHandle(IntPtr hObject);

[DllImport("coredll.dll")]

private static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, Int32
nNumberOfBytesToWrite, ref Int32 lpNumberOfBytesWritten, IntPtr
lpOverlapped);

[DllImport("coredll.dll")]

private static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer, Int32
nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);


[DllImport("coredll.dll")]

private static extern IntPtr CreateFile( string lpFileName,

UInt32 dwDesiredAccess,

UInt32 dwShareMode,

IntPtr lpSecurityAttributes,

UInt32 dwCreationDisposition,

UInt32 dwFlagsAndAttributes,

IntPtr hTemplateFile);
 
don't know the syntax for pinvoking in VB

Stelrad Doulton said:
p/invoke the win32 CreateFile, WriteFile, ReadFile, CloseHandle from
coredll.dll (see below). Call CreateFile with "COM1:" for eample and hey
presto you have an IntPtr to an open com port. From there use this handle
with the other stuff.

You might wanna create a class to wrap all this stuff to make it more OO
and to implement IDisposable on it.

[DllImport("coredll.dll")]

private static extern bool CloseHandle(IntPtr hObject);

[DllImport("coredll.dll")]

private static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, Int32
nNumberOfBytesToWrite, ref Int32 lpNumberOfBytesWritten, IntPtr
lpOverlapped);

[DllImport("coredll.dll")]

private static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer, Int32
nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);


[DllImport("coredll.dll")]

private static extern IntPtr CreateFile( string lpFileName,

UInt32 dwDesiredAccess,

UInt32 dwShareMode,

IntPtr lpSecurityAttributes,

UInt32 dwCreationDisposition,

UInt32 dwFlagsAndAttributes,

IntPtr hTemplateFile);


IanHale said:
Thanks Cor

Sorry if I'm being a bit slow - but the search turned up many entries -
none
of which seemed to answer simple question - do you know of any simple
guide
on getting going. I even tried looking up Dick's book on Amazon - but
they
claim a 6-8 week delivery (if at all!)
 
Thanks Cor - I'll give it a go. The words 'where angels fear to tread....'
spring to mind!
 
Back
Top