Use C # to call traditional 32-bit API to implement serial operation, the entire structure is particularly simple. Receiving data only needs to define data reception events.
I won't be uploaded from the source code, please contact me (Dyj057@gmail.com) if you need the source code. You can also teach me how to upload the source code.
Using system; using system.runtime.interopservices;
/// /// (c) 2003-2005 C2217 Studio reserved all rights // / // file name: ibmsserialport.cs /// file ID: /// file description: /// package dynamic link The function of library IBMSSERIALIALPORT.DLL is provided in the .NET environment /// serial port asynchronous reception and sends data. /// /// Current Version: 1.0 /// /// Author: Deng Yang are /// Creation Date: 2005-2-2 /// Last Modified: 2005-2-2 /// /// Historical modification record: /// /// summary>
Namespace IBMS.TOOL.IO {/// // When the serial port receives data, an event is generated. /// SpRecvdataArgs is the parameters of the event, and the Recvdata in the parameter contains the received data. /// How to use: /// summary> public class spRecvdataaargs: Eventargs {/// /// The received data consists of byte array /// summary> private bote [] recvdata;
/// /// Constructor requires a BYTE [] as an initialization parameter to instantiate spRecvdataaargs /// summary> /// received data < / param> public sprecvdataaargs (byte [] recvdata) {if (recvdata == null) {throw (new argumentnullexception ());
THIS.RECVDATA = Recvdata;
/// /// Returns the received data content /// summary> public byte [] recvdata {get {return rvdata;}}}
/// /// Package Dynamic Link Library IBMSSerialPort.dll's feature, providing asynchronous /// serial port reception and sending functions in the .NET environment. It is especially true that the asynchronous pass signal automatically receives data. /// summary> public class ibmsserialport: idisposable {
#Region platform call declaration code
/// /// Declaring IBMS_Openport.dll's IBMS_Openport function /// summary> /// serial number param> /// Baud rate param> /// returns> [DLLIMPORT ("IBMSSerialport.dll")] Public Static Extern INTPTR IBMS_OpenPort (int Nport, int nrate); /// /////// Declare IBMS_CLOSE Functions /// summary> [DLLIMPORT ("IBMSSerialport.dll")] Public Static Extern Void IBMS_Close (INTPTR Port);
/// /// Declaring IBMS_SENDDATA function /// summary> /// param> /// < / param> /// returns> [DLLIMPORT ("IBMSSerialport.dll")] Public Static Extern Bool IBMS_SENDDATA (INTPTR Port, Byte [] Data, Int NDataSize; /// ////////////////// /////// statement of Ibms_SetFuncHandle IbmsSerialPort.dll function /// summary> /// param> [DllImport ( "IbmsSerialPort.dll")] public static extern void Ibms_SetFuncHandle (IntPtr port, HandleFunc handDataFunc );
#ndregion #Region definition field
/// /// Define data processing commission, function pointer to the API Passing Dynamic Link Library /// Summary> Public Delegate Void Handlefunc (INTPTR PDATA, INT NDATASIZE);
/// /// Defines the prototype /// summary> Public Delegate Void Recvdata (Object Sender, Sprecvdataargs E);
/// /// Define data receiving event /// summary> public event recvdata onRecvdata;
/// /// serial port processing Receive /// summary> private handlefunc_handledatafunc;
/// /// serial port number, from 1st, the maximum 255 ///////// /// The baud rate supported by the serial port must It is one of the standard baud rates /// summary> private standerdrate rate;
/// /// serial port current open status /// summary> private bool openstatus = false;
/// /// serial handle /// summary> private INTPTR porthandle;
#Region Defines the standard serial port.
/// /// standard baud rate /// summary> public enum standardrate {R50 = 50, R75 = 75, R110 = 110, R150 = 150, R300 = 300, R600 = 600, R1200 = 1200, R2400 = 2400, R9600 = 9600, R19200 = 19200, R38400 = 38400, R76800 = 76800, R115200 = 115200}; #ENDREGION
#ndregion
#Region definition method
/// /// Constructor /// summary> public ibmsserialport () {porthandle = (intptr) 0;
_handledatafunc = new handlefunc (onDllrecvdata);
/// /// Open serial port /// ///// Serial number param> /// baud rate < / param> /// /// Throw an exception, replacement error Description exception> Public void Open (int nport, standerdrate nrate) {
IF (Nport> 255 || NPORT <0) {throw (new argumentoutofrangeexception ());
Port = nport; rate;
PortHandle = IBMS_OpenPort (port, (int));
IF ((INTPTR) 0 == PortHandle) {throw ("Open Serial Port Failed"));} // Register Function Pointer IBMS_SETFUNCHANDLE (porthandle, _handaDatafunc);
OpenSTATUS = TRUE;
}
/// /// Close serial port /// summary> public void close () {if (openstatus) {ibms_close (porthandle);
OpenSTATUS = FALSE;
}
/// /// Send data /// summary> /// data content param> /// Throw an application Abnormal, replacement error Description exception> Public void senddata (byte [] data) {if (! Openstatus) {throw ("" Serial port is not open, send data failed "));}
IF (! ibms_senddata (porthandle, data.length) {throw ("Serial Port Send Data Failure"));}}}}} /// /// Processing received serial port data /// < / summary> /// serial data receiving buffer first address param> /// data size, general data size does not exceed 2K param> unsafe Protected void ONDTLRECVDATA (INT NDATASIZE) {Int DataSize = NDataSize; Byte * PDATA = (Byte *) punhanda;
Byte [] data = new byte [DataSize];
// Copy data to the Byte array for (int i = 0; i
// Excited event onRecvdata (this, new sprecvdataargs (data));
}
#ndregion
#REGION Define Properties
/// /// Returns the current serial number /// summary> public int port {get {return port;}}
/// /// Returns the baud rate //////////////////////////////////////////>}}}}}
/// /// Returns the status of the current serial port /// summary> Public Bool OpenStatus {Get {Return OpenStatus;}}
#ndregion
#Region Release /// //// ///////////////////////////////////////////////////> You must implement the idisposable interface /// When using the class, you must remember to call Dispose. ), Recovery system resources /// // / ////// {/// SerialPort port = new serialport (); /// ... // // In Try-catch- FINALY FINALY Release Resources //// Port.dispose (); ///} /// /// Method 2 /// USING (SerialPort Port = New SerialPort ()) /// {//// ... ///} /// The variable will automatically call its Dispose () method //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////> }
Protected Virtual Void Dispose (Bool Disposing) {if (Disposing) {// Clean up the host}
// Clean the unmanaged resource close ();
#Region IDisposable member
Public void dispose () {// Todo: Add SerialPort.Dispose implementation Dispose (TRUE);
Gc.suppressfinalize (this);
# endregion # endRegion
}