.net下除了用api,就是用mscomm控件了

解决方案 »

  1.   

    有mscomm和NetSerial,一个是COM控件,另外一个是.net封装api的
      

  2.   

    mscomm效果很好了。就是一点,不定的什么时候oncomm事件就突然没效果了。
      

  3.   

    建议你下载这个用:这个不涉及版权问题,而且做得不错,考虑了许多。
    http://msdn.microsoft.com/msdnmag/issues/02/10/NETSerialComm/default.aspx
    下载,继承之。
    我用了很久了,最长曾连续运行22天。很稳定的。
      

  4.   

    NetSerial的版权好像有问题,也不太好用的
      

  5.   

    对不起,我弄错了,刚才说的“NetSerial的版权好像有问题,也不太好用的”是错的,我搞错了两个相近的名字。请原谅。
      

  6.   

    我只想要Mscomm串口类。我写了一个好象不太好用。接受方面!强大家帮忙
      

  7.   

    其实现有的很多都很好。上面题到的那个,c#写的,微软网站的,效率凑合,我见的最好的还是MSCOMM,注册一下,自己通过继承,重新包装一下,还是很好的控件。
      

  8.   

    顶一下,我也遇到同样的问题。我用mscomm控件的oncomm事件,可是一定时间后就没有响应了,高手能不能给个解决办法,成功了我一定另开贴送分。
      

  9.   

    串口类我都是用MFC做的,C#还没做过
      

  10.   

    mscomm很好,我做的一个可以运行不断,没响应一般是代码的问题。
      

  11.   

    楼上的能不能把你那个MSCOMM类贡献一下!谢谢
      

  12.   

    to: jimh(jimmy)
    能不能把你的贡献出来呀
      

  13.   

    using System; 
    using System.Runtime.InteropServices; namespace 通信调试 

    /// <summary> 
    /// CommPort 的摘要说明。 
    /// </summary> 
    public class CommPort 

    public int PortNum;  //端口号
    public int BaudRate; //波特率
    public byte ByteSize; //字节节长度
    public byte Parity; // 0-4=no,odd,even,,space ,奇偶校验 0-4,无校验,奇校验,偶校验,标致位
    public byte StopBits; // 0,1,2 = 1, 1.5, 2  //停止位 分别表示1,1.5.2位
    public int ReadTimeout; //读超时
           
    //comm port win32 file handle 
    public int hComm = -1; 
    public bool hCommRead = false; 
           
    public bool Opened = false; 
      
    //这些是windows定义好的常数
    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)] 
    private struct DCB  

    //taken from c struct in platform sdk  
    public int DCBlength;           // sizeof(DCB)  
    public int BaudRate;            // current baud rate  
    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 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,,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  
    public char ErrorChar;          // error replacement character  
    public char EofChar;            // end of input character  
    public char EvtChar;            // received event character  
    public ushort wReserved1;         // reserved; do not use  
    }  [StructLayout(LayoutKind.Sequential)] 
    private struct COMMTIMEOUTS  
    {   
    public int ReadIntervalTimeout;  
    public int ReadTotalTimeoutMultiplier;  
    public int ReadTotalTimeoutConstant;  
    public int WriteTotalTimeoutMultiplier;  
    public int WriteTotalTimeoutConstant;  
    }      [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 int CreateFile( 
    string lpFileName,                         // file name 文件名,如果是通读可以是com1...
    uint dwDesiredAccess,                      // access mode 访问模式,是读还是写
    int dwShareMode,                          // share mode 是否共享 0表是独占
    int lpSecurityAttributes,                  // SD 安全属性,在串口操作时没有可以写null或0
    int dwCreationDisposition,                // how to create 在打开这个文件时是否要求文件存在,在这里要存在(就是常数3)
    int dwFlagsAndAttributes,                 // file attributes 是否是可以重复用,在这里写0
    int hTemplateFile                        // handle to template file ,打开文件时模板文件,在这里没有是null或0
    ); 
    [DllImport("kernel32")] 
    private static extern bool GetCommState( 
    int hFile,  // handle to communications device 
    ref DCB lpDCB    // device-control block 
    );    
    [DllImport("kernel32")] 
    private static extern bool BuildCommDCB( 
    string lpDef,  // device-control string 
    ref DCB lpDCB     // device-control block 
    ); 
    [DllImport("kernel32")] 
    private static extern bool SetCommState( 
    int hFile,  // handle to communications device 
    ref DCB lpDCB    // device-control block 
    ); 
      

  14.   

    [DllImport("kernel32")] 
    private static extern bool GetCommTimeouts( 
    int hFile,                  // handle to comm device 
    ref COMMTIMEOUTS lpCommTimeouts  // time-out values 
    );    
    [DllImport("kernel32")]    
    private static extern bool SetCommTimeouts( 
    int hFile,                  // handle to comm device 
    ref COMMTIMEOUTS lpCommTimeouts  // time-out values 
    ); 
    [DllImport( "kernel32")] 
    private static extern bool ReadFile( 
    int hFile,                // handle to file 
    byte[] lpBuffer,             // data buffer 
    int nNumberOfBytesToRead,  // number of bytes to read 
    ref int lpNumberOfBytesRead, // number of bytes read 
    ref OVERLAPPED lpOverlapped    // overlapped buffer 
    ); 
    [DllImport("kernel32")] 
    private static extern bool WriteFile( 
    int hFile,                    // handle to file 
    byte[] lpBuffer,                // data buffer 
    int nNumberOfBytesToWrite,     // number of bytes to write 
    ref int lpNumberOfBytesWritten,  // number of bytes written 
    ref OVERLAPPED lpOverlapped        // overlapped buffer 
    ); 
    [DllImport("kernel32")] 
    private static extern bool CloseHandle( 
    int hObject   // handle to object 
    ); 
           
    public void Open()  

            
    DCB dcbCommPort = new DCB(); 
    COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();    
            
            
    // OPEN THE COMM PORT.        
    hComm = CreateFile("COM" + 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("Comm Port Can Not Be Opened")); 

           
    // SET THE COMM TIMEOUTS. 
              
    GetCommTimeouts(hComm,ref ctoCommPort); 
    ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout; 
    ctoCommPort.ReadTotalTimeoutMultiplier = 0; 
    ctoCommPort.WriteTotalTimeoutMultiplier = 0; 
    ctoCommPort.WriteTotalTimeoutConstant = 0;   
    SetCommTimeouts(hComm,ref ctoCommPort); 
           
    // SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS. 
    // THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST. 
    // IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER 
    // THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING. 
    // ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING. 
           
    dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort); 
    GetCommState(hComm, ref dcbCommPort); 
    dcbCommPort.BaudRate=BaudRate; 
    dcbCommPort.Parity=Parity; 
    dcbCommPort.ByteSize=ByteSize; 
    dcbCommPort.StopBits=StopBits; 
    SetCommState(hComm, ref dcbCommPort); 
             
    Opened = true; 
             

           
    public void Close()  

    if (hComm!=INVALID_HANDLE_value)  

    CloseHandle(hComm); 


           
    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; 
    hCommRead=ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort); 
    OutBytes = new byte[BytesRead]; 
    Array.Copy(BufBytes,OutBytes,BytesRead); 
                 
    }  
    else  

    throw(new ApplicationException("Comm Port Not Open")); 

    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("Comm Port Not Open")); 
    }       

    }  class HexCon  

    //converter hex string to byte and byte to hex string 
    public static string ByteToString(byte[] InBytes)  

    string StringOut=""; 
    foreach (byte InByte in InBytes)  

    StringOut=StringOut + String.Format("{0:X2} ",InByte); 

    return StringOut;  

    public static byte[] StringToByte(string InString)  

    string[] ByteStrings; 
    // char [] Byte; 
    ByteStrings = InString.Split(" ".ToCharArray()); 
    byte[] ByteOut; 
    ByteOut = new byte[ByteStrings.Length-1]; 
    for (int i = 0;i==ByteStrings.Length-1;i++)  
    {    
    //  ByteStrings.to 
    ByteOut[i] = System.Convert.ToByte(("0x" + ByteStrings[i]));  }  
    return ByteOut; 
    }     

      

  15.   

    晕,这个类我有了,我说了,要的是MSCOMM的类。
      

  16.   

    mscomm效果很好了。就是一点,不定的什么时候oncomm事件就突然没效果了。我的做法时,每隔一段时间(3分钟)就监控一下mscomm的最近接收字符串,如果没有变化就关掉程序,然后再重启程序。
      

  17.   

    我的意思是如何把MSCOMM写成类!!!!!!!!!!!!!!!!!!!!!!!!!