Serial Communication in .NET

  • Thread starter Costantino Pipero
  • Start date
C

Thread Starter

Costantino Pipero

The .NET framework is supposed to offer a namespace for stream and I/O handling, but I didn't find any documentation on serial port programming with the new namespace.

I even wrote to Mr Troelsen that wrote one of the best C# books around, but he's equally clueless.

Basically you should be able to use the System.IO namespace like you do when you handle a disk file (like the old CreateFile() in the platform SDK).

There's even an example on Microsoft website: it seems like you're supposed to open a file from the System.IO and pass the name "COM1" (for example) instead of a filepath.
It doesn't work nor returns a Filestream as it should. But no explanation, sysnopsis, detail has been found anywhere.

I would like to try to avoid a call to the good old kernel32.dll or a sad use of the mscomm32.ocx.

Any suggestion will be greatly appreciated.

Regards

Costantino
 
W
This is just a guess, but did you try the "\\.\COMn" form for the device name? I know that with COM ports above COM9, this was necessary in C++ and VB on NT and Win2k, maybe they standardized?
 
C

Costantino Pipero

Thanks for your hint, but it didn't work I've tried it before.
The System.IO call (from a Microsoft example, that they don't support anyway) looks like:

mCommFileStream = CType((New System.IO.FileInfo("COM1")).Open(FileMode.Open,
FileAccess.ReadWrite, FileShare.None), System.IO.FileStream)

in VB.NET: it doesn't return the FileStream expected...

Thanks for any comment
 
I am trying to write an application that can control a device connected to a serial port or parallel port. Does any one know where abouts in .NET i can find this type of Information. Preferably in VB.NET
 
B

belax(Russia)

First: handle1=createfile("com1",...) like in VB6
second: filestrem.handle=handle1
farther use filestream as you want.
 
Unfortunately Microsoft has not included yet any functionality for serial communication in .NET, therefore you cannot build a managed code for controlling serial communication process. You can either reference the MSComm control in your application, or you can use the API calls to control the serial port.

Microsoft has built a .NET class for working with RS232 port by using API calls. Unfortunately I can't remember the precise address in MSDN where you can find it. However, I have downloaded it and I must say it works fine. So, if you still need to control RS232 in .NET, give me a mail at

"[email protected]", mailto:[email protected]

Btw: the class is written in VB.NET, but it shouldn't be any problem in either converting it to C#, or using as is in your application.

Very best regards,

Oleg
 
S

Simon Luckenuik

Visual Basic .NET Code Sample: Using the Comm Port

This sample demonstrates how to control a communications port from .NET. Since the .NET Framework does not support direct control of the Comm Port, a separate class is used that provides direct Win32 API calls. (Note: Comm Port is synonymous with COM Port, Serial Port or RS232 port).

Featured Highlights
This tutorial demonstrates how to use the Comm Port to communicate. In particular, it discusses:

Testing to see if the Comm Port is available for use.
Using the Comm Port to communicate with a modem.
Use Win32 API calls to control communication with the Comm Port.

The link is:
"http://msdn.microsoft.com/library/d...dnvssamp/html/vbcs_usingthecomportinvbnet.asp":http://msdn.microsoft.com/library/d...dnvssamp/html/vbcs_usingthecomportinvbnet.asp

The sample can be found there:
"http://download.microsoft.com/downl.../1.0/NT45XP/EN-US/UsingtheCOMPortinVB.NET.exe":http://download.microsoft.com/downl.../1.0/NT45XP/EN-US/UsingtheCOMPortinVB.NET.exe
 
It is possible to use the FileStream object to communicate through the serial port but you still need to use the Win32 API call to CreateFile. Here's how in VB .Net:

First,

Create your api header function.

<DllImport("kernel32.dll")> Private Shared Function CreateFile( _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As IntPtr
End Function

Notice the return value, "IntPtr" this is important, an IntPtr is not the same as Integer in .Net.

Then make the api call to get the handle to the COM Port.

Dim hPort as IntPtr = CreateFile("COM1", _
GENERIC_READ Or GENERIC_WRITE, 0, 0, _
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)

Now you can create a FileStream and bind it to the COM Port. The FileStream must be Asyncronous.

Dim f As New FileStream(hPort, FileAccess.ReadWrite, True, 2000, True)

You can now write to and read from the COM port using the stream or create StreamReader Objects and bind them to your FileStream.

This method is easier than the Win32 API ReadFile and WriteFile functions, its also a little bit more powerful.
 
Top