使用C#調(diào)用傳統(tǒng)32位API實現(xiàn)串口操作,整個結(jié)構(gòu)特別的簡單。接收數(shù)據(jù)只需要定義數(shù)據(jù)接收事件即可。
上傳源代碼我不會,需要源代碼的請與我(dyj057@gmail.com)聯(lián)系。你也可以教我怎么上傳源代碼。
using System;
using System.Runtime.InteropServices;
/// <summary>
/// (C)2003-2005 C2217 Studio 保留所有權(quán)利
///
/// 文件名稱: IbmsSerialPort.cs
/// 文件ID:
/// 文件說明:
/// 封裝動態(tài)鏈接庫IbmsSerialPort.dll的功能,提供在.NET環(huán)境中
/// 串口異步接收和發(fā)送數(shù)據(jù)的功能。
///
/// 當(dāng)前版本: 1.0
///
/// 作者: 鄧楊均
/// 創(chuàng)建日期: 2005-2-2
/// 最后修改日期: 2005-2-2
///
/// 歷史修改記錄:
///
/// </summary>
namespace Ibms.Tool.IO
{
/// <summary>
/// 當(dāng)串口接收到數(shù)據(jù)時,會產(chǎn)生一個事件。
/// SPRecvDataArgs就是該事件的參數(shù),參數(shù)中的RecvData包含接收到的數(shù)據(jù)。
/// 使用方法:
/// </summary>
public class SPRecvDataArgs:EventArgs
{
/// <summary>
/// 接收到的數(shù)據(jù)組成的字節(jié)數(shù)組
/// </summary>
private byte[] recvData;
/// <summary>
/// 構(gòu)造函數(shù),需要一個包含數(shù)據(jù)的byte[]作為初始化參數(shù)來實例化 SPRecvDataArgs
/// </summary>
/// <param name="recvData">接收到的數(shù)據(jù)</param>
public SPRecvDataArgs(byte[] recvData)
{
if( recvData == null)
{
throw(new ArgumentNullException());
}
this.recvData = recvData;
}
/// <summary>
/// 返回接收到的數(shù)據(jù)內(nèi)容
/// </summary>
public byte[] RecvData
{
get
{
return recvData;
}
}
}
/// <summary>
/// 封裝動態(tài)鏈接庫IbmsSerialPort.dll的功能,提供在.NET環(huán)境中異步
/// 串口接收和發(fā)送功能。特別實現(xiàn)的是異步通過信號自動接收數(shù)據(jù)的模式。
/// </summary>
public class IbmsSerialPort:IDisposable
{
#region 平臺調(diào)用聲明代碼
/// <summary>
/// 聲明IbmsSerialPort.dll的Ibms_OpenPort函數(shù)
/// </summary>
/// <param name="nPort">串口號</param>
/// <param name="nRate">波特率</param>
/// <returns></returns>
[DllImport("IbmsSerialPort.dll")]
public static extern IntPtr Ibms_OpenPort(int nPort, int nRate);
/// <summary>
/// 聲明IbmsSerialPort.dll的Ibms_Close函數(shù)
/// </summary>
[DllImport("IbmsSerialPort.dll")]
public static extern void Ibms_Close( IntPtr port);
/// <summary>
/// 聲明IbmsSerialPort.dll的Ibms_SendData函數(shù)
/// </summary>
/// <param name="data"></param>
/// <param name="nDataSize"></param>
/// <returns></returns>
[DllImport("IbmsSerialPort.dll")]
public static extern bool Ibms_SendData( IntPtr port, byte[] data,int nDataSize);
/// <summary>
/// 聲明IbmsSerialPort.dll的Ibms_SetFuncHandle函數(shù)
/// </summary>
/// <param name="handDataFunc"></param>
[DllImport("IbmsSerialPort.dll")]
public static extern void Ibms_SetFuncHandle( IntPtr port, HandleFunc handDataFunc);
#endregion
#region 定義字段
/// <summary>
/// 定義數(shù)據(jù)處理委托,作為API的函數(shù)指針傳入動態(tài)鏈接庫
/// </summary>
public delegate void HandleFunc(IntPtr pData, int nDataSize);
/// <summary>
/// 定義數(shù)據(jù)接收事件的原型
/// </summary>
public delegate void RecvData(object sender,SPRecvDataArgs e);
/// <summary>
/// 定義數(shù)據(jù)接收事件
/// </summary>
public event RecvData OnRecvData;
/// <summary>
/// 串口處理接收數(shù)據(jù)的委托
/// </summary>
private HandleFunc _handleDataFunc;
/// <summary>
/// 串口的編號,從1開始的整數(shù),最大255
/// </summary>
private int port;
/// <summary>
/// 串口所支持的波特率,必須是標(biāo)準(zhǔn)波特率之一
/// </summary>
private StanderdRate rate;
/// <summary>
/// 串口當(dāng)前的打開狀態(tài)
/// </summary>
private bool openStatus=false;
/// <summary>
/// 串口句柄
/// </summary>
private IntPtr portHandle;
#region 定義標(biāo)準(zhǔn)的串口波特率
/// <summary>
/// 標(biāo)準(zhǔn)的波特率
/// </summary>
public enum StanderdRate
{
R50=50,
R75=75,
R110=110,
R150=150,
R300=300,
R600=600,
R1200=1200,
R2400=2400,
R4800=4800,
R9600=9600,
R19200=19200,
R38400=38400,
R57600=57600,
R76800=76800,
R115200=115200
};
#endregion
#endregion
#region 定義方法
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public IbmsSerialPort()
{
portHandle = (IntPtr)0;
_handleDataFunc = new HandleFunc(OnDllRecvData);
}
/// <summary>
/// 打開串口
/// </summary>
/// <param name="nPort">串口號</param>
/// <param name="nRate">波特率</param>
/// /// <exception cref="ApplicationException">拋出應(yīng)用程序異常,包換錯誤描述</exception>
public void Open(int nPort, StanderdRate nRate)
{
if(nPort > 255 || nPort < 0)
{
throw(new ArgumentOutOfRangeException());
}
port = nPort;
rate = nRate;
portHandle = Ibms_OpenPort( port, (int)rate );
if( (IntPtr)0 == portHandle )
{
throw( new ApplicationException("打開串口失敗"));
}
//注冊函數(shù)指針
Ibms_SetFuncHandle( portHandle, _handleDataFunc );
openStatus = true;
}
/// <summary>
/// 關(guān)閉串口
/// </summary>
public void Close()
{
if( openStatus )
{
Ibms_Close( portHandle);
}
openStatus = false;
}
/// <summary>
/// 發(fā)送數(shù)據(jù)
/// </summary>
/// <param name="sendData">數(shù)據(jù)內(nèi)容</param>
/// <exception cref="ApplicationException">拋出應(yīng)用程序異常,包換錯誤描述</exception>
public void SendData( byte[] data )
{
if( !openStatus )
{
throw( new ApplicationException("串口沒有打開,發(fā)送數(shù)據(jù)失敗") );
}
if( !Ibms_SendData( portHandle, data, data.Length ) )
{
throw( new ApplicationException("串口發(fā)送數(shù)據(jù)失敗") );
}
}
/// <summary>
/// 處理接收到的串口數(shù)據(jù)
/// </summary>
/// <param name="pData">串口數(shù)據(jù)接收緩沖區(qū)首地址</param>
/// <param name="nDataSize">數(shù)據(jù)大小,一般數(shù)據(jù)大小不超過2K</param>
unsafe protected void OnDllRecvData(IntPtr pUnhandleData, int nDataSize)
{
int dataSize= nDataSize ;
byte * pData =(byte *) pUnhandleData;
byte[] data = new byte[dataSize];
//復(fù)制數(shù)據(jù)到byte數(shù)組
for(int i=0; i<dataSize; i++)
{
data[i]= pData[i];
}
//激發(fā)事件
OnRecvData( this, new SPRecvDataArgs(data) );
}
#endregion
#region 定義屬性
/// <summary>
/// 返回當(dāng)前的串口號
/// </summary>
public int Port
{
get
{
return port;
}
}
/// <summary>
/// 返回當(dāng)前串口的波特率
/// </summary>
public StanderdRate Rate
{
get
{
return