标准DLL,可以采用DllImport进行调用。例如:
代码如下 | 复制代码 |
[DllImport("KMY350X.dll")]
private static extern int OpenPort(int PortNum, int BaudRate); |
代码如下 | 复制代码 |
ConfigClass config = new ConfigClass();
comm.serialPort.PortName = config.ReadConfig("SendHealCard"); //波特率 comm.serialPort.BaudRate = 9600; //数据位 comm.serialPort.DataBits = 8; //两个停止位 comm.serialPort.StopBits = System.IO.Ports.StopBits.One; //无奇偶校验位 comm.serialPort.Parity = System.IO.Ports.Parity.None; comm.serialPort.ReadTimeout = 100; comm.serialPort.WriteTimeout = -1; |
代码如下 | 复制代码 |
编写Comm类:
public class Comm
{ public delegate void EventHandle(byte[] readBuffer); public event EventHandle DataReceived; public SerialPort serialPort; Thread thread; volatile bool _keepReading; public Comm() { serialPort = new SerialPort(); thread = null; _keepReading = false; } public bool IsOpen { get { return serialPort.IsOpen; } } private void StartReading() { if (!_keepReading) { _keepReading = true; thread = new Thread(new ThreadStart(ReadPort)); thread.Start(); } } private void StopReading() { if (_keepReading) { _keepReading = false; thread.Join(); thread = null; } } private void ReadPort() { while (_keepReading) { if (serialPort.IsOpen) { int count = serialPort.BytesToRead; if (count > 0) { byte[] readBuffer = new byte[count]; try { Application.DoEvents(); serialPort.Read(readBuffer, 0, count); if(DataReceived != null) DataReceived(readBuffer); Thread.Sleep(100); } catch (TimeoutException) { } } } } } public void Open() { Close(); serialPort.Open(); if (serialPort.IsOpen) { StartReading(); } else { MessageBox.Show("串口打开失败!"); } } public void Close() { StopReading(); serialPort.Close(); } public void WritePort(byte[] send, int offSet, int count) { if (IsOpen) { serialPort.Write(send, offSet, count); } } } |
代码如下 | 复制代码 |
Comm comm = new Comm();
ConfigClass config = new ConfigClass(); comm.serialPort.PortName = config.ReadConfig("SendHealCard"); //波特率 comm.serialPort.BaudRate = 9600; //数据位 comm.serialPort.DataBits = 8; //两个停止位 comm.serialPort.StopBits = System.IO.Ports.StopBits.One; //无奇偶校验位 comm.serialPort.Parity = System.IO.Ports.Parity.None; comm.serialPort.ReadTimeout = 100; comm.serialPort.WriteTimeout = -1; comm.Open(); if (comm.IsOpen)
{
comm.DataReceived += new Comm.EventHandle(comm_DataReceived);
}
发送指令:
///
/// 发卡到机口 /// private void SendCardToOut() { is_read_card = false; sendCardToOut = true; byte[] send = { 0x02, 0x46, 0x43, 0x34, 0x03, 0x30 }; if (comm.IsOpen) { comm.WritePort(send, 0, send.Length); } } 收到指令,并解析:
void comm_DataReceived(byte[] readBuffer1)
{ //log.Info(HexCon.ByteToString(readBuffer)); if (readBuffer1.Length == 1) { receive = HealCardClass.ByteToString(readBuffer1); string str = "06"; if (string.Equals(receive.Trim(), str, StringComparison.CurrentCultureIgnoreCase)) { try { if (is_read_card) { byte[] send = new byte[1]; send[0] = 0x05; comm.WritePort(send, 0, send.Length); Thread.Sleep(500); comm.DataReceived -= new Comm.EventHandle(comm_DataReceived); InitReadComm(); } if (sendCardToOut) { byte[] send = new byte[1]; send[0] = 0x05; comm.WritePort(send, 0, send.Length); readComm.DataReceived -= new Comm.EventHandle(readComm_DataReceived); readComm.Close(); log.Info("发卡完成!"); lblMsg.Text = "发卡成功!"; lblSendCardMsg.Text = "发卡完成,请收好卡!"; timer1.Tick -= new EventHandler(timer1_Tick); PlaySound(); this.btnOK.Enabled = true; } } catch (Exception ex) { log.Info(ex.ToString()); } } } } |
暗影猎手失落的世界 最新版本v30.118.6.0
下载格里姆瓦勒完全版 安卓版v1.2.0
下载忍者武士暗影格斗无限金币版 安卓版v1.82.1
下载忍者必须死34399账号登录版 最新版v1.0.138v2.0.72
下载创造与魔法免登录版 手机版v1.0.0760
创造与魔法是一款高度自由的沙盒冒险手游,该游戏采用了3D最新
创造与魔法gm版 安卓版v1.0.0760
创造与魔法是一款有英雄互娱运营的一款经营沙盒建造手游,该游戏
创造与魔法变态无敌版 v1.0.0760
创造与魔法是一款3DQ版的沙盒模拟建造手游,在这里玩家需要适
腾讯普通话小镇游戏 安卓版v2.2.5
普通话小镇是由腾讯游戏追梦计划推出发行的模拟经营类型普通学习
海岛村 最新版v1.0
海岛村是一款非常好玩的模拟经营类手游,玩家在游戏中将会有一个