当前位置: 代码迷 >> Windows Mobile >> Windows Mobile 打开串口失败,该怎么解决
  详细解决方案

Windows Mobile 打开串口失败,该怎么解决

热度:55   发布时间:2016-04-25 07:44:48.0
Windows Mobile 打开串口失败
菜鸟提问:windows mobile6.1 打开串口的时候失败,用GetLastError看了一下返回值是6,百度到6对应的错误是:无效句柄。
有没有高手知道怎么回事。
代码如下:

调用:
  CommPort cp = new CommPort();
  cp.Open();


  /// <summary> 
  /// 这是一个关于调用串口的类 
  /// </summary> 
  public class CommPort
  {

  public int PortNum = 1;
  public int BaudRate = 9600;
  public byte ByteSize = 8;
  public byte Parity = 0; // 0-4=no,odd,even,mark,space  
  public byte StopBits = 0; // 0,1,2 = 1, 1.5, 2  
  public int ReadTimeout = 0;

  //comm port win32 file handle 
  private int hComm = -1;

  public bool Opened = false;

  //win32 api constants 
  private const uint GENERIC_READ = 0x80000000;
  private const uint GENERIC_WRITE = 0x40000000;
  private const int OPEN_EXISTING = 3;
  private const int INVALID_HANDLE_VALUE = -1;

  [StructLayout(LayoutKind.Sequential)]
  public struct DCB
  {
  //taken from c struct in platform sdk  
  public int DCBlength; // sizeof(DCB)  
  public int BaudRate; // current baud rate 
  /* these are the c struct bit fields, bit twiddle flag to set 
  public int fBinary; // binary mode, no EOF check  
  public int fParity; // enable parity checking  
  public int fOutxCtsFlow; // CTS output flow control  
  public int fOutxDsrFlow; // DSR output flow control  
  public int fDtrControl; // DTR flow control type  
  public int fDsrSensitivity; // DSR sensitivity  
  public int fTXContinueOnXoff; // XOFF continues Tx  
  public int fOutX; // XON/XOFF out flow control  
  public int fInX; // XON/XOFF in flow control  
  public int fErrorChar; // enable error replacement  
  public int fNull; // enable null stripping  
  public int fRtsControl; // RTS flow control  
  public int fAbortOnError; // abort on error  
  public int fDummy2; // reserved  
  */
  public uint flags;
  public ushort wReserved; // not currently used  
  public ushort XonLim; // transmit XON threshold  
  public ushort XoffLim; // transmit XOFF threshold  
  public byte ByteSize; // number of bits/byte, 4-8  
  public byte Parity; // 0-4=no,odd,even,mark,space  
  public byte StopBits; // 0,1,2 = 1, 1.5, 2  
  public char XonChar; // Tx and Rx XON character  
  public char XoffChar; // Tx and Rx XOFF character  
  相关解决方案