My question is:
Can I access serial ports (COM1 or COM2) or USB ports
from .NET application (particularly C#) and how?
Thank you
.NET 1.0 has no support for legacy ports (COM/LPT).
There are plans to add support for serial ports in a future version:
http://groups.google.com/groups?&selm=O%23vQLn9bCHA.392%40tkmsftngp09
"this sample is very similar to what we will be adding" :
http://www.gotdotnet.com/userarea/filedetails.aspx?FileName=SerialPort.zip
(note, project built with beta VS.NET, read gotdotnet comment)
With 1.0 you have to use PInvoke or Interop :
First understand the Win32 API as described here (C++):
http://msdn.microsoft.com/library/en-us/dnfiles/html/msdn_serial.asp
MSDN article for .NET (mostly C#):
http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/
PInvoke samples for C#:
http://www.gotdotnet.com/userarea/filedetails.aspx?FileName=SerialPort.zip
http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/
http://www.gotdotnet.com/userarea/keywordsrch.aspx?keyword=SerialStream
or for VB.NET:
http://msdn.microsoft.com/library/en-us/dnvssamp/html/vbcs_usingthecomportinvbnet.asp
http://www.gotdotnet.com/userarea/keywordsrch.aspx?keyword=rs232
http://www.allapi.net/classlib/class.php?id=15
or you can use the "Managed Extensions for C++" and write wrappers.
http://msdn.microsoft.com/vstudio/techinfo/articles/upgrade/managedext.asp
http://www.gotdotnet.com/team/cplusplus/
on your VS.NET path:
...\VC7\managedextensionsspec.doc
...\VC7\migration_guide.doc
MC++ Sample:
http://www.codeproject.com/managedcpp/howtocomport.asp
or reusing the VB6 MSComm ActiveX is easy, but it has some 'problems' (license)
http://support.microsoft.com/?kbid=318597
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320
http://ourworld.compuserve.com/homepages/richard_grier/NETCommOCX.htm
commercial:
http://www.sax.net/dotnet/communications/
for Compact Framework (Windows CE)
http://ourworld.compuserve.com/homepages/richard_grier/CFSerial.htm
for Interop, use newsgroup:
microsoft.public.dotnet.framework.interop
--
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/
USB is a hardware bus and as such not directly exposed
to user-mode (Win32) applications (or even .NET).
Only Windows device-drivers do control the USB:
http://msdn.microsoft.com/library/en-us/buses/hh/buses/usbsystem_7non.asp
or get a driver from manufacturer of your USB hardware.
So if you connect a printer with USB, use the Win32/.NET printer-API.
Or if you connect a modem with USB, use the Win32 serial-API.
Or if you connect an imaging device (camera/scanner), use TWAIN/WIA.
If you want to replace simple, generic communication, better use Ethernet
and TCP/IP.
If you want to design your own external, USB compliant hardware,
you have to understand the USB bus protocol
and implement a controller chip.
Then on Windows side, you have to write your own
device driver with the Windows DDK.
Sure there are OEM kits making all this easier.
Please visit sites like:
http://www.usb.org/
http://www.microsoft.com/hwdev/bus/USB/
http://www.lvr.com/usb.htm
http://www.beyondlogic.org
and learn the technology.