C # asynchronous data receiving serial operation class

xiaoxiao2021-03-06  14

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: /// ///

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: /// public class spRecvdataaargs: Eventargs {/// /// The received data consists of byte array /// private bote [] recvdata;

///

/// Constructor requires a BYTE [] as an initialization parameter to instantiate spRecvdataaargs /// /// received data < / param> public sprecvdataaargs (byte [] recvdata) {if (recvdata == null) {throw (new argumentnullexception ());

THIS.RECVDATA = Recvdata;

///

/// Returns the received data content /// 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. /// public class ibmsserialport: idisposable {

#Region platform call declaration code

///

/// Declaring IBMS_Openport.dll's IBMS_Openport function /// /// serial number /// Baud rate /// [DLLIMPORT ("IBMSSerialport.dll")] Public Static Extern INTPTR IBMS_OpenPort (int Nport, int nrate); /// /////// Declare IBMS_CLOSE Functions /// [DLLIMPORT ("IBMSSerialport.dll")] Public Static Extern Void IBMS_Close (INTPTR Port);

///

/// Declaring IBMS_SENDDATA function /// /// /// < / param> /// [DLLIMPORT ("IBMSSerialport.dll")] Public Static Extern Bool IBMS_SENDDATA (INTPTR Port, Byte [] Data, Int NDataSize; /// ////////////////// /////// statement of Ibms_SetFuncHandle IbmsSerialPort.dll function /// /// [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 /// Public Delegate Void Handlefunc (INTPTR PDATA, INT NDATASIZE);

///

/// Defines the prototype /// Public Delegate Void Recvdata (Object Sender, Sprecvdataargs E);

///

/// Define data receiving event /// public event recvdata onRecvdata;

///

/// serial port processing Receive /// 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 /// private standerdrate rate;

///

/// serial port current open status /// private bool openstatus = false;

///

/// serial handle /// private INTPTR porthandle;

#Region Defines the standard serial port.

///

/// standard baud rate /// 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 /// public ibmsserialport () {porthandle = (intptr) 0;

_handledatafunc = new handlefunc (onDllrecvdata);

///

/// Open serial port /// Serial number /// baud rate < / param> /// /// Throw an exception, replacement error Description 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 /// public void close () {if (openstatus) {ibms_close (porthandle);

OpenSTATUS = FALSE;

}

///

/// Send data /// /// data content /// Throw an application Abnormal, replacement error Description 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 /// data size, general data size does not exceed 2K 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 /// public int port {get {return port;}}

///

/// Returns the baud rate //////////////////////////////////////////>}}}}}

///

/// Returns the status of the current serial port /// 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

}

转载请注明原文地址:https://www.9cbs.com/read-49305.html

New Post(0)