原代码如下(这是从微软下的P/Invoke to Develop a .NET Base Class Library ):#region "重写发送串口数据"
private IntPtr hPort;
private IntPtr ptrUWO = IntPtr.Zero;
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
public int SetComdata(byte [] senddata)
{
if(hPort==ptrUWO) return(0);
uint sent=0;
writeCount=senddata.Length;
WriteFile(hPort,senddata,(uint)writeCount,out sent,IntPtr.Zero);
return(Convert.ToInt32(sent));
}
#endregion打开串口可以,也能返回hport
但对串口进行写时,WriteFile(hPort,senddata,(uint)writeCount,out sent,IntPtr.Zero);
返回的是false
其中sent一直是0,说明根本没有对串口进行写各位高手,到底怎么回事?我怀疑out UInt32 lpNumberOfBytesWritten这个参数的设置有问题,但又不知道用其他什么类型,很多都已经试过了!还是不行大家帮帮阿

解决方案 »

  1.   

    Have a try![DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean WriteFile( IntPtr fFile, 
       [MarshalAs( UnmanagedType.LPArray )]Byte[] lpBuffer, 
       UInt32 nNumberOfBytesToWrite,out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
      

  2.   

    Have a try!------Declare ----------------- 
      [StructLayout(LayoutKind.Sequential)]   
       private struct OVERLAPPED 
      { 
       public int  Internal; 
       public int  InternalHigh; 
       public int  Offset; 
       public int  OffsetHigh; 
       public int hEvent; 
      }  [DllImport("kernel32")]
      private static extern bool WriteFile(
       int hFile,                    // handle to file
       [MarshalAs( UnmanagedType.LPArray )] byte[] lpBuffer,                // data buffer
       int nNumberOfBytesToWrite,     // number of bytes to write
       ref int lpNumberOfBytesWritten,  // number of bytes written
       ref OVERLAPPED lpOverlapped        // overlapped buffer
       );
      

  3.   

    以下代码绝对可以用,我已经用了无数次了,分享一下。因为太长,只好分几次了 /// <summary>
    /// Opening Testing and Closing the Port Handle.
    /// </summary>
    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
    IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
    IntPtr hTemplateFile); //Constants for errors:
    internal const UInt32 ERROR_FILE_NOT_FOUND = 2;
    internal const UInt32 ERROR_INVALID_NAME = 123;
    internal const UInt32 ERROR_ACCESS_DENIED = 5;
    internal const UInt32 ERROR_IO_PENDING = 997; //Constants for return value:
    internal const Int32 INVALID_HANDLE_VALUE = -1; //Constants for dwFlagsAndAttributes:
    internal const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000; //Constants for dwCreationDisposition:
    internal const UInt32 OPEN_EXISTING = 3; //Constants for dwDesiredAccess:
    internal const UInt32 GENERIC_READ = 0x80000000;
    internal const UInt32 GENERIC_WRITE = 0x40000000; [DllImport("kernel32.dll")]
    internal static extern Boolean CloseHandle(IntPtr hObject); /// <summary>
    /// Manipulating the communications settings.
    /// </summary> [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommState(IntPtr hFile, ref DCB lpDCB); [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts); [DllImport("kernel32.dll")]
    internal static extern Boolean BuildCommDCBAndTimeouts(String lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts); [DllImport("kernel32.dll")]
    internal static extern Boolean SetCommState(IntPtr hFile, [In] ref DCB lpDCB); [DllImport("kernel32.dll")]
    internal static extern Boolean SetCommTimeouts(IntPtr hFile, [In] ref COMMTIMEOUTS lpCommTimeouts); [DllImport("kernel32.dll")]
    internal static extern Boolean SetupComm(IntPtr hFile, UInt32 dwInQueue, UInt32 dwOutQueue); [StructLayout( LayoutKind.Sequential )] internal struct COMMTIMEOUTS 
    {
    //JH1.1: Changed Int32 to UInt32 to allow setting to MAXDWORD
    internal UInt32 ReadIntervalTimeout;
    internal UInt32 ReadTotalTimeoutMultiplier;
    internal UInt32 ReadTotalTimeoutConstant;
    internal UInt32 WriteTotalTimeoutMultiplier;
    internal UInt32 WriteTotalTimeoutConstant;
    }
    //JH1.1: Added to enable use of "return immediately" timeout.
    internal const UInt32 MAXDWORD = 0xffffffff; [StructLayout( LayoutKind.Sequential )] internal struct DCB 
    {
    internal Int32 DCBlength;
    internal Int32 BaudRate;
    internal Int32 PackedValues;
    internal Int16 wReserved;
    internal Int16 XonLim;
    internal Int16 XoffLim;
    internal Byte  ByteSize;
    internal Byte  Parity;
    internal Byte  StopBits;
    internal Byte XonChar;
    internal Byte XoffChar;
    internal Byte ErrorChar;
    internal Byte EofChar;
    internal Byte EvtChar;
    internal Int16 wReserved1; internal void init(bool parity, bool outCTS, bool outDSR, int dtr, bool inDSR, bool txc, bool xOut,
    bool xIn, int rts)
    {
    DCBlength = 28; PackedValues = 0x8001;
    if (parity) PackedValues |= 0x0002;
    if (outCTS) PackedValues |= 0x0004;
    if (outDSR) PackedValues |= 0x0008;
    PackedValues |= ((dtr & 0x0003) << 4);
    if (inDSR) PackedValues |= 0x0040;
    if (txc) PackedValues |= 0x0080;
    if (xOut) PackedValues |= 0x0100;
    if (xIn) PackedValues |= 0x0200;
    PackedValues |= ((rts & 0x0003) << 12); }
    }
      

  4.   

    /// <summary>
    /// Reading and writing.
    /// </summary>
    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
    out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped); [StructLayout( LayoutKind.Sequential )] internal struct OVERLAPPED 
    {
    internal UIntPtr Internal;
    internal UIntPtr InternalHigh;
    internal UInt32 Offset;
    internal UInt32 OffsetHigh;
    internal IntPtr hEvent;
    } [DllImport("kernel32.dll")]
    internal static extern Boolean SetCommMask(IntPtr hFile, UInt32 dwEvtMask); // Constants for dwEvtMask:
    internal const UInt32 EV_RXCHAR = 0x0001;
    internal const UInt32 EV_RXFLAG = 0x0002;
    internal const UInt32 EV_TXEMPTY = 0x0004;
    internal const UInt32 EV_CTS = 0x0008;
    internal const UInt32 EV_DSR = 0x0010;
    internal const UInt32 EV_RLSD = 0x0020;
    internal const UInt32 EV_BREAK = 0x0040;
    internal const UInt32 EV_ERR = 0x0080;
    internal const UInt32 EV_RING = 0x0100;
    internal const UInt32 EV_PERR = 0x0200;
    internal const UInt32 EV_RX80FULL = 0x0400;
    internal const UInt32 EV_EVENT1 = 0x0800;
    internal const UInt32 EV_EVENT2 = 0x1000; [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean WaitCommEvent(IntPtr hFile, IntPtr lpEvtMask, IntPtr lpOverlapped); [DllImport("kernel32.dll")]
    internal static extern Boolean CancelIo(IntPtr hFile);

    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean ReadFile(IntPtr hFile, [Out] Byte[] lpBuffer, UInt32 nNumberOfBytesToRead,
    out UInt32 nNumberOfBytesRead, IntPtr lpOverlapped); [DllImport("kernel32.dll")]
    internal static extern Boolean TransmitCommChar(IntPtr hFile, Byte cChar); /// <summary>
    /// Control port functions.
    /// </summary>
    [DllImport("kernel32.dll")]
    internal static extern Boolean EscapeCommFunction(IntPtr hFile, UInt32 dwFunc); // Constants for dwFunc:
    internal const UInt32 SETXOFF = 1;
    internal const UInt32 SETXON = 2;
    internal const UInt32 SETRTS = 3;
    internal const UInt32 CLRRTS = 4;
    internal const UInt32 SETDTR = 5;
    internal const UInt32 CLRDTR = 6;
    internal const UInt32 RESETDEV = 7;
    internal const UInt32 SETBREAK = 8;
    internal const UInt32 CLRBREAK = 9;

    [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat); // Constants for lpModemStat:
    internal const UInt32 MS_CTS_ON = 0x0010;
    internal const UInt32 MS_DSR_ON = 0x0020;
    internal const UInt32 MS_RING_ON = 0x0040;
    internal const UInt32 MS_RLSD_ON = 0x0080; /// <summary>
    /// Status Functions.
    /// </summary>
    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean GetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped,
    out UInt32 nNumberOfBytesTransferred, Boolean bWait); [DllImport("kernel32.dll")]
    internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, IntPtr lpStat);
    [DllImport("kernel32.dll")]
    internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, out COMSTAT cs); //Constants for lpErrors:
    internal const UInt32 CE_RXOVER = 0x0001;
    internal const UInt32 CE_OVERRUN = 0x0002;
    internal const UInt32 CE_RXPARITY = 0x0004;
    internal const UInt32 CE_FRAME = 0x0008;
    internal const UInt32 CE_BREAK = 0x0010;
    internal const UInt32 CE_TXFULL = 0x0100;
    internal const UInt32 CE_PTO = 0x0200;
    internal const UInt32 CE_IOE = 0x0400;
    internal const UInt32 CE_DNS = 0x0800;
    internal const UInt32 CE_OOP = 0x1000;
    internal const UInt32 CE_MODE = 0x8000; [StructLayout( LayoutKind.Sequential )] internal struct COMSTAT 
    {
    internal const uint fCtsHold = 0x1;
    internal const uint fDsrHold = 0x2;
    internal const uint fRlsdHold = 0x4;
    internal const uint fXoffHold = 0x8;
    internal const uint fXoffSent = 0x10;
    internal const uint fEof = 0x20;
    internal const uint fTxim = 0x40;
    internal UInt32 Flags;
    internal UInt32 cbInQue;
    internal UInt32 cbOutQue;
    }
    [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommProperties(IntPtr hFile, out COMMPROP cp); [StructLayout( LayoutKind.Sequential )] internal struct COMMPROP
    {
    internal UInt16 wPacketLength; 
    internal UInt16 wPacketVersion; 
    internal UInt32 dwServiceMask; 
    internal UInt32 dwReserved1; 
    internal UInt32 dwMaxTxQueue; 
    internal UInt32 dwMaxRxQueue; 
    internal UInt32 dwMaxBaud; 
    internal UInt32 dwProvSubType; 
    internal UInt32 dwProvCapabilities; 
    internal UInt32 dwSettableParams; 
    internal UInt32 dwSettableBaud; 
    internal UInt16 wSettableData; 
    internal UInt16 wSettableStopParity; 
    internal UInt32 dwCurrentTxQueue; 
    internal UInt32 dwCurrentRxQueue; 
    internal UInt32 dwProvSpec1; 
    internal UInt32 dwProvSpec2; 
    internal Byte wcProvChar; 
    }
      

  5.   

    貌似没有对串口进行初始化....
    平时都要先初始化下,不初始化的话能否进行数据传输不得而直.
    要初始化DCB COMSTAT 什么的,最起码的是也要CreateFile一下啊.或者CODE不全,全贴出来看再说吧....
      

  6.   

    谢谢楼上的几位 我都试了 还是不行啊
    以下是我的代码:
    第一部分:
    internal DCB PortDCB;
    internal DCB dcb;
    private IntPtr hPort;
    private OVERLAPPED overlp;
    private IntPtr ptrUWO = IntPtr.Zero;
    private int writeCount;
    public Csdn()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
    IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
    IntPtr hTemplateFile); //Constants for errors:
    internal const UInt32 ERROR_FILE_NOT_FOUND = 2;
    internal const UInt32 ERROR_INVALID_NAME = 123;
    internal const UInt32 ERROR_ACCESS_DENIED = 5;
    internal const UInt32 ERROR_IO_PENDING = 997; //Constants for return value:
    internal const Int32 INVALID_HANDLE_VALUE = -1; //Constants for dwFlagsAndAttributes:
    internal const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000; //Constants for dwCreationDisposition:
    internal const UInt32 OPEN_EXISTING = 3; //Constants for dwDesiredAccess:
    internal const UInt32 GENERIC_READ = 0x80000000;
    internal const UInt32 GENERIC_WRITE = 0x40000000; [DllImport("kernel32.dll")]
    internal static extern Boolean CloseHandle(IntPtr hObject); /// <summary>
    /// Manipulating the communications settings.
    /// </summary> [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommState(IntPtr hFile, ref DCB lpDCB); [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts); [DllImport("kernel32.dll")]
    internal static extern Boolean BuildCommDCBAndTimeouts(String lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts); [DllImport("kernel32.dll")]
    internal static extern Boolean SetCommState(IntPtr hFile, [In] ref DCB lpDCB); [DllImport("kernel32.dll")]
    internal static extern Boolean SetCommTimeouts(IntPtr hFile, [In] ref COMMTIMEOUTS lpCommTimeouts); [DllImport("kernel32.dll")]
    internal static extern Boolean SetupComm(IntPtr hFile, UInt32 dwInQueue, UInt32 dwOutQueue); [StructLayout( LayoutKind.Sequential )] internal struct COMMTIMEOUTS 
    {
    //JH1.1: Changed Int32 to UInt32 to allow setting to MAXDWORD
    internal UInt32 ReadIntervalTimeout;
    internal UInt32 ReadTotalTimeoutMultiplier;
    internal UInt32 ReadTotalTimeoutConstant;
    internal UInt32 WriteTotalTimeoutMultiplier;
    internal UInt32 WriteTotalTimeoutConstant;
    }
    //JH1.1: Added to enable use of "return immediately" timeout.
    internal const UInt32 MAXDWORD = 0xffffffff; [StructLayout( LayoutKind.Sequential )] internal struct DCB 
    {
    internal Int32 DCBlength;
    internal Int32 BaudRate;
    internal Int32 PackedValues;
    internal Int16 wReserved;
    internal Int16 XonLim;
    internal Int16 XoffLim;
    internal Byte  ByteSize;
    internal Byte  Parity;
    internal Byte  StopBits;
    internal Byte XonChar;
    internal Byte XoffChar;
    internal Byte ErrorChar;
    internal Byte EofChar;
    internal Byte EvtChar;
    internal Int16 wReserved1; internal void init(bool parity, bool outCTS, bool outDSR, int dtr, bool inDSR, bool txc, bool xOut,
    bool xIn, int rts)
    {
    DCBlength = 28; PackedValues = 0x8001;
    if (parity) PackedValues |= 0x0002;
    if (outCTS) PackedValues |= 0x0004;
    if (outDSR) PackedValues |= 0x0008;
    PackedValues |= ((dtr & 0x0003) << 4);
    if (inDSR) PackedValues |= 0x0040;
    if (txc) PackedValues |= 0x0080;
    if (xOut) PackedValues |= 0x0100;
    if (xIn) PackedValues |= 0x0200;
    PackedValues |= ((rts & 0x0003) << 12); }
    }
    [DllImport("kernel32.dll", SetLastError=true)]
    private static extern Boolean WriteFile(IntPtr fFile, [MarshalAs( UnmanagedType.LPArray )]Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
    ref UInt32 lpNumberOfBytesWritten,ref OVERLAPPED lpOverlapped); [StructLayout(LayoutKind.Sequential)]   
    private struct OVERLAPPED 

    public int  Internal; 
    public int  InternalHigh; 
    public int  Offset; 
    public int  OffsetHigh; 
    public int hEvent; 
    } [DllImport("kernel32.dll")]
    internal static extern Boolean SetCommMask(IntPtr hFile, UInt32 dwEvtMask); // Constants for dwEvtMask:
    internal const UInt32 EV_RXCHAR = 0x0001;
    internal const UInt32 EV_RXFLAG = 0x0002;
    internal const UInt32 EV_TXEMPTY = 0x0004;
    internal const UInt32 EV_CTS = 0x0008;
    internal const UInt32 EV_DSR = 0x0010;
    internal const UInt32 EV_RLSD = 0x0020;
    internal const UInt32 EV_BREAK = 0x0040;
    internal const UInt32 EV_ERR = 0x0080;
    internal const UInt32 EV_RING = 0x0100;
    internal const UInt32 EV_PERR = 0x0200;
    internal const UInt32 EV_RX80FULL = 0x0400;
    internal const UInt32 EV_EVENT1 = 0x0800;
    internal const UInt32 EV_EVENT2 = 0x1000;
      

  7.   

    第二部分:
    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean WaitCommEvent(IntPtr hFile, IntPtr lpEvtMask, IntPtr lpOverlapped); [DllImport("kernel32.dll")]
    internal static extern Boolean CancelIo(IntPtr hFile);

    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean ReadFile(IntPtr hFile, [Out] Byte[] lpBuffer, UInt32 nNumberOfBytesToRead,
    out UInt32 nNumberOfBytesRead, IntPtr lpOverlapped); [DllImport("kernel32.dll")]
    internal static extern Boolean TransmitCommChar(IntPtr hFile, Byte cChar); /// <summary>
    /// Control port functions.
    /// </summary>
    [DllImport("kernel32.dll")]
    internal static extern Boolean EscapeCommFunction(IntPtr hFile, UInt32 dwFunc); // Constants for dwFunc:
    internal const UInt32 SETXOFF = 1;
    internal const UInt32 SETXON = 2;
    internal const UInt32 SETRTS = 3;
    internal const UInt32 CLRRTS = 4;
    internal const UInt32 SETDTR = 5;
    internal const UInt32 CLRDTR = 6;
    internal const UInt32 RESETDEV = 7;
    internal const UInt32 SETBREAK = 8;
    internal const UInt32 CLRBREAK = 9;

    [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat); // Constants for lpModemStat:
    internal const UInt32 MS_CTS_ON = 0x0010;
    internal const UInt32 MS_DSR_ON = 0x0020;
    internal const UInt32 MS_RING_ON = 0x0040;
    internal const UInt32 MS_RLSD_ON = 0x0080; /// <summary>
    /// Status Functions.
    /// </summary>
    [DllImport("kernel32.dll", SetLastError=true)]
    internal static extern Boolean GetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped,
    out UInt32 nNumberOfBytesTransferred, Boolean bWait); [DllImport("kernel32.dll")]
    internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, IntPtr lpStat);
    [DllImport("kernel32.dll")]
    internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, out COMSTAT cs); //Constants for lpErrors:
    internal const UInt32 CE_RXOVER = 0x0001;
    internal const UInt32 CE_OVERRUN = 0x0002;
    internal const UInt32 CE_RXPARITY = 0x0004;
    internal const UInt32 CE_FRAME = 0x0008;
    internal const UInt32 CE_BREAK = 0x0010;
    internal const UInt32 CE_TXFULL = 0x0100;
    internal const UInt32 CE_PTO = 0x0200;
    internal const UInt32 CE_IOE = 0x0400;
    internal const UInt32 CE_DNS = 0x0800;
    internal const UInt32 CE_OOP = 0x1000;
    internal const UInt32 CE_MODE = 0x8000; [StructLayout( LayoutKind.Sequential )] internal struct COMSTAT 
    {
    internal const uint fCtsHold = 0x1;
    internal const uint fDsrHold = 0x2;
    internal const uint fRlsdHold = 0x4;
    internal const uint fXoffHold = 0x8;
    internal const uint fXoffSent = 0x10;
    internal const uint fEof = 0x20;
    internal const uint fTxim = 0x40;
    internal UInt32 Flags;
    internal UInt32 cbInQue;
    internal UInt32 cbOutQue;
    }
    [DllImport("kernel32.dll")]
    internal static extern Boolean GetCommProperties(IntPtr hFile, out COMMPROP cp); [StructLayout( LayoutKind.Sequential )] internal struct COMMPROP
    {
    internal UInt16 wPacketLength; 
    internal UInt16 wPacketVersion; 
    internal UInt32 dwServiceMask; 
    internal UInt32 dwReserved1; 
    internal UInt32 dwMaxTxQueue; 
    internal UInt32 dwMaxRxQueue; 
    internal UInt32 dwMaxBaud; 
    internal UInt32 dwProvSubType; 
    internal UInt32 dwProvCapabilities; 
    internal UInt32 dwSettableParams; 
    internal UInt32 dwSettableBaud; 
    internal UInt16 wSettableData; 
    internal UInt16 wSettableStopParity; 
    internal UInt32 dwCurrentTxQueue; 
    internal UInt32 dwCurrentRxQueue; 
    internal UInt32 dwProvSpec1; 
    internal UInt32 dwProvSpec2; 
    internal Byte wcProvChar; 
    }
    #region "重写打开端口"
    public bool opencomm()
    {
    bool lp=false;
    PortDCB = new DCB();
    string port="COM1";
    //COMMTIMEOUTS CommTimeouts = new COMMTIMEOUTS();
    //CommBaseSettings cs = CommSettings();;
    //OVERLAPPED wo = new OVERLAPPED();
    dcb=new DCB();
    hPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero,
    OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);
    if(hPort!=(IntPtr)INVALID_HANDLE_VALUE)
    {
    lp=true;
    GetCommState(hPort,ref dcb);
    dcb.BaudRate=9600;
    dcb.ByteSize=(byte)8;
    dcb.Parity=(byte)0;
    dcb.StopBits=(byte)1;
    if(!SetCommState(hPort, ref dcb))
    {
    lp=false;
    }
    }
    return(lp);
    }
    #endregion
    #region "读取串口数据,返回其字节数"
    public int GetComdata(byte [] read)
    {
    UInt32 gotbytes;
    gotbytes=0;
    if(hPort==ptrUWO) return(0);
    COMSTAT cs;
    UInt32 errs;
    ClearCommError(hPort,out errs,out cs);
    ReadFile(hPort,read,cs.cbInQue,out gotbytes,ptrUWO);
    return(Convert.ToInt32(cs.cbInQue));
    }
    #endregion
    #region "重写发送串口数据"
    public int SetComdata(byte [] senddata)
    {
    if(hPort==ptrUWO) return(0);
    uint sent=0;
    writeCount=senddata.Length;
    overlp=new OVERLAPPED();
    WriteFile(hPort,senddata,(uint)writeCount,ref sent,ref overlp);
    return(Convert.ToInt32(sent));
    }
    #endregion
    }
      

  8.   

    WriteFile如果成功会返回TRUE,你可以看看WriteFile的返回值.如果是FALSE,请调用GetLastError和GetOverlappedResult 分析失败原因.
    另外,既然是FILE_FLAG_OVERLAPPED,最好在进行creat,write,read时候动用临界区或互斥量,以避免出现乱七八糟的问题(同步,变量被更改等等异步问题)
      

  9.   

    我看你的问题跟我一样;我虽然已经找到问题所在;但目前没有解决.
    dcb.BaudRate=9600;
    dcb.ByteSize=(byte)8;
    dcb.Parity=(byte)0;
    dcb.StopBits=(byte)1;
    我用串口跟踪器看;ByteSize为7很奇怪;应该是8才对啊
      

  10.   

    反正C#操作串口很郁闷
    我最后用c++做的dll帮助初始化才解决问题
      

  11.   

    偶一般是用C++写DLL,DLL负责操作串口。然后C#引用这个DLL,使用回调函数