参见如下代码:
public static bool InitComm(string com)
{
Win32Com.DCB PortDCB = new Win32Com.DCB();
Win32Com.COMMTIMEOUTS CommTimeouts = new Win32Com.COMMTIMEOUTS();
Win32Com.OVERLAPPED wo = new Win32Com.OVERLAPPED();
IntPtr hPort;
hPort = Win32Com.CreateFile(com, Win32Com.GENERIC_READ | Win32Com.GENERIC_WRITE, 0, IntPtr.Zero,
Win32Com.OPEN_EXISTING, Win32Com.FILE_FLAG_OVERLAPPED, IntPtr.Zero);
if (hPort == (IntPtr)Win32Com.INVALID_HANDLE_VALUE)
{
return false;
} //JH1.1: Changed from 0 to "magic number" to give instant return on ReadFile:
CommTimeouts.ReadIntervalTimeout = Win32Com.MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutMultiplier=1000;
CommTimeouts.WriteTotalTimeoutConstant = 0;
    Win32Com.GetCommState (hPort,ref PortDCB);

PortDCB.BaudRate = 9600;
PortDCB.ByteSize = 8;
PortDCB.Parity = (byte)0;
PortDCB.StopBits = (byte)0;

if (!Win32Com.SetCommState(hPort, ref PortDCB))
{
return false;
}
if (!Win32Com.SetCommTimeouts(hPort, ref CommTimeouts))
{
return false;
}
Win32Com.CancelIo (hPort);
Win32Com.CloseHandle (hPort);
return true;
}
System.IntPtr  pHandle;
int handle;
//opens the existing file...
handle = CreateFile(fileName,
GENERIC_WRITE,
0, 
0,
OPEN_EXISTING,
0,
0);
    
pHandle=new System .IntPtr (handle);
FileStream fs=new FileStream (pHandle,FileAccess.Write );
string s;




byte[] byteOut; 
Int32 dwCount = s.Length;
byteOut = new byte[dwCount]; 
// for (int i = 0;i<dwCount;i++)  
// {    
// //  ByteStrings.to 
// byteOut[i] = System.Convert.ToByte( s[i]); 
//
// }  
byteOut=Encoding.ASCII .GetBytes (s);

// How many characters are in the string?


fs.Write (byteOut,0,dwCount);
fs.Flush ();
fs.Close ();
Win32Com.CloseHandle (pHandle);
return true;
}