| 发表于:2007-06-06 17:29:198楼 得分:0 |
贴个串口读写类给你吧: using system; using system.runtime.interopservices; namespace busapp { /// <summary> /// /// </summary> public class mycom { public mycom() { // // todo: 在此处添加构造函数逻辑 // } public int portnum; //1,2,3,4 public int baudrate; //1200,2400,4800,9600 public byte bytesize; //8 bits public byte parity; // 0-4=no,odd,even,mark,space public byte stopbits; // 0,1,2 = 1, 1.5, 2 public int readtimeout; //10 //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)] 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,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 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.dll ")] private static extern int createfile( string lpfilename, // file name uint dwdesiredaccess, // access mode int dwsharemode, // share mode int lpsecurityattributes, // sd int dwcreationdisposition, // how to create int dwflagsandattributes, // file attributes int htemplatefile // handle to template file ); [dllimport( "kernel32.dll ")] private static extern bool getcommstate( int hfile, // handle to communications device ref dcb lpdcb // device-control block ); [dllimport( "kernel32.dll ")] private static extern bool buildcommdcb( string lpdef, // device-control string ref dcb lpdcb // device-control block ); [dllimport( "kernel32.dll ")] private static extern bool setcommstate( int hfile, // handle to communications device ref dcb lpdcb // device-control block ); [dllimport( "kernel32.dll ")] private static extern bool getcommtimeouts( int hfile, // handle to comm device ref commtimeouts lpcommtimeouts // time-out values ); [dllimport( "kernel32.dll ")] private static extern bool setcommtimeouts( int hfile, // handle to comm device ref commtimeouts lpcommtimeouts // time-out values ); [dllimport( "kernel32.dll ")] 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.dll ")] 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.dll ")] 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); 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( "comm port not open ")); } return outbytes; } public int write(byte[] writebytes) { int byteswritten = 0; if (hcomm!=invalid_handle_value) { overlapped ovlcommport = new overlapped(); writefile(hcomm,writebytes,writebytes.length,ref byteswritten,ref ovlcommport); } else { throw(new applicationexception( "comm port not open ")); } return byteswritten; } } } | | |
|