我要通过com1口向硬件终端发送命令,然后读取终端返回的数据。在初始化时,用createfile获取串口句柄资源,设置dcb结构。问题是:每次打开计算机,程序都发不出命令。而用串口调试助手调试一下后,只要电脑不关机,程序每次打开都运行正常。而一旦重启计算机,运行程序又不能工作,还要再用串口调试助手调试一下才可以。这是我的代码!
我想使DCB设置的问题,那位高手帮忙解决一下???
万分感谢!public void Open() 
{
DCB dcbCommPort = new DCB();   
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
//   打开串口   OPEN   THE   COMM   PORT.   
hComm = CreateFile(PortNum,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);   
//   如果串口没有打开,就打开   IF   THE   PORT   CANNOT   BE   OPENED,   BAIL   OUT.   
if(hComm == INVALID_HANDLE_VALUE) 
{
throw(new ApplicationException("非法操作,不能打开串口!"));
}
 
//          DWORD   DCBlength;                       //   sizeof(DCB)     
//          DWORD   BaudRate;                         //   current   baud   rate     
//          DWORD   fBinary:   1;                     //   binary   mode,   no   EOF   check     
//          DWORD   fParity:   1;                     //   enable   parity   checking     
//          DWORD   fOutxCtsFlow:1;             //   CTS   output   flow   control     
//          DWORD   fOutxDsrFlow:1;             //   DSR   output   flow   control     
//          DWORD   fDtrControl:2;               //   DTR   flow   control   type     
//          DWORD   fDsrSensitivity:1;       //   DSR   sensitivity     
//          DWORD   fTXContinueOnXoff:1;   //   XOFF   continues   Tx     
//          DWORD   fOutX:   1;                         //   XON/XOFF   out   flow   control     
//          DWORD   fInX:   1;                           //   XON/XOFF   in   flow   control     
//          DWORD   fErrorChar:   1;               //   enable   error   replacement     
//          DWORD   fNull:   1;                         //   enable   null   stripping     
//          DWORD   fRtsControl:2;               //   RTS   flow   control     
//          DWORD   fAbortOnError:1;           //   abort   reads/writes   on   error     
//          DWORD   fDummy2:17;                     //   reserved     
//          WORD   wReserved;                         //   not   currently   used     
//          WORD   XonLim;                               //   transmit   XON   threshold     
//          WORD   XoffLim;                             //   transmit   XOFF   threshold     
//          BYTE   ByteSize;                           //   number   of   bits/byte,   4-8     
//  -----------------------------------------------------------------   
//          BYTE   Parity;                               //   0-4=no,odd,even,,space       |   
//  ----------------------------------------------------------------   
//          BYTE   StopBits;                           //   0,1,2   =   1,   1.5,   2     
//          char   XonChar;                             //   Tx   and   Rx   XON   character     
//          char   XoffChar;                           //   Tx   and   Rx   XOFF   character     
//          char   ErrorChar;                         //   error   replacement   character     
//          char   EofChar;                             //   end   of   input   character     
//          char   EvtChar;                             //   received   event   character     
//          WORD   wReserved1;                       //   reserved;   do   not   use     
//  }   DCB;    
//   设置串口   SET   BAUD   RATE,   PARITY,   WORD   SIZE,   AND   STOP   BITS.   
GetCommState(hComm,ref dcbCommPort); 
dcbCommPort.BaudRate=BaudRate;   
dcbCommPort.flags = 0;
dcbCommPort.ByteSize = 8;
//dcb.fBinary=1;
dcbCommPort.flags|=1;   
if   (Parity>0)   
{   
//dcb.fParity=1   
dcbCommPort.flags|=2;   
}   
dcbCommPort.Parity=Parity;   
dcbCommPort.ByteSize=ByteSize;   
dcbCommPort.StopBits=StopBits; dcbCommPort.fBinary = 1;                     //   binary   mode,   no   EOF   check     
dcbCommPort.fParity = 1;                     //   enable   parity   checking     
dcbCommPort.fOutxCtsFlow = 1;             //   CTS   output   flow   control     
dcbCommPort.fOutxDsrFlow = 1;             //   DSR   output   flow   control     
dcbCommPort.fDtrControl = 2;               //   DTR   flow   control   type     
dcbCommPort.fDsrSensitivity = 1;       //   DSR   sensitivity     
dcbCommPort.fTXContinueOnXoff = 1;   //   XOFF   continues   Tx     
dcbCommPort.fOutX = 1;                         //   XON/XOFF   out   flow   control     
dcbCommPort.fInX = 1;                           //   XON/XOFF   in   flow   control     
dcbCommPort.fErrorChar = 1;               //   enable   error   replacement     
dcbCommPort.fNull = 1;                         //   enable   null   stripping     
dcbCommPort.fRtsControl = 2;               //   RTS   flow   control     
dcbCommPort.fAbortOnError = 1;           //   abort   reads/writes   on   error     
dcbCommPort.fDummy2 = 17;                     //   reserved      //设置通信超时时间   SET   THE   COMM   TIMEOUTS.
GetCommTimeouts(hComm,ref ctoCommPort);   
ctoCommPort.ReadTotalTimeoutConstant   =   ReadTimeout;   
ctoCommPort.ReadTotalTimeoutMultiplier   =   0;   
ctoCommPort.WriteTotalTimeoutMultiplier   =   0;   
ctoCommPort.WriteTotalTimeoutConstant   =   0;     
SetCommTimeouts(hComm,ref   ctoCommPort);   
if(!SetCommState(hComm,ref dcbCommPort))   
{   
//uint   ErrorNum=GetLastError();   
throw(new   ApplicationException("非法操作,不能打开串口!"));   

COMMTIMEOUTS commTime = new COMMTIMEOUTS();
commTime.ReadIntervalTimeout = int.MaxValue;//MAXDWORD;
  
commTime.ReadTotalTimeoutConstant = 1000; 
commTime.ReadTotalTimeoutMultiplier = 0; 
commTime.WriteTotalTimeoutConstant = 5000; 
commTime.WriteTotalTimeoutMultiplier = 0;
SetCommTimeouts(hComm, ref commTime); 
//SetCommMask(hComm, EV_TXEMPTY | EV_RXCHAR); 
//PurgeComm(hComm, PURGE_TXCLEAR);// 要加上超时设置  //unre   to   see   if   setting   took   correctly   
//DCB   dcbCommPort2   =   new   DCB();   
//GetCommState(hComm,   ref   dcbCommPort2);   
Opened   =   true;   
}   
    
public void Close()   
{   
if   (hComm!=INVALID_HANDLE_VALUE)   
{   
CloseHandle(hComm);
this.Opened = false;

}   
}   
    
public byte[] Read(int NumBytes)   
{   
byte[]   BufBytes;   
byte[]   OutBytes;   
BufBytes   =   new   byte[NumBytes];   
if   (hComm!=INVALID_HANDLE_VALUE)   
{   
OVERLAPPED   ovlCommPort   =   new   OVERLAPPED();   
int   BytesRead=0;   
ReadFile(hComm,BufBytes,NumBytes,ref   BytesRead,ref   ovlCommPort);   
OutBytes   =   new   byte[BytesRead];   
Array.Copy(BufBytes,OutBytes,BytesRead);   
}     
else   
{   
throw(new   ApplicationException("串口未打开!"));   
}   
return   OutBytes;   
}   
    
public   void   Write(byte[]   WriteBytes)   
{   
if   (hComm!=INVALID_HANDLE_VALUE)   
{   
OVERLAPPED   ovlCommPort   =   new   OVERLAPPED();   
int   BytesWritten   =   0;   
WriteFile(hComm,WriteBytes,WriteBytes.Length,ref   BytesWritten,ref   ovlCommPort);   
}   
else   
{   
throw(new   ApplicationException("串口未打开!"));   
}   
}

解决方案 »

  1.   

    唉  彷佛想得了当初遇到此问题的情景  呵呵
    到我的blog下一个dll可以解决
    如果不明白 就QQ交流吧http://www.cnblogs.com/tuyile006/archive/2006/09/25/514327.html
      

  2.   

    .net 2.0提供了新的com类,SerialPort
      

  3.   

    tuyile006(小y) 
    哥们,http://www.cnblogs.com/tuyile006/archive/2006/09/25/514327.html
    这个里面没有解决方案啊!能把您的qq给我吗?
    非常感谢!
      

  4.   

    tuyile006(小y) 
    帮帮忙吧!
        我急着用。
      

  5.   

    唉  彷佛想得了当初遇到此问题的情景  呵呵
    到我的blog下一个dll可以解决
    如果不明白 就QQ交流吧http://www.cnblogs.com/tuyile006/archive/2006/09/25/514327.html大哥,
    我以前没有做过串口通信,想通过这个了解了解。
    既然您以前遇到过我类似的问题,还望指点一二。
    拜托了。
      

  6.   

    我上面都研究半天了,
    就快好了,现在让我去用mscomm实在是不甘心啊!
    顺便也想学习学习!
       哪位大侠能帮帮我啊?