作者chrisQQ (ChrisLiu)
看板C_Sharp
标题Re: [问题] 建立读卡机与电脑的连线 RFID serial Port
时间Wed May 5 05:25:56 2010
※ 引述《taso5566 (a)》之铭言:
: 请问各位大大,
: (一)如何利用winsock来开发Ethernet的应用程式
: 建立RS-232序列通讯介面的程式设计
: (二)建立读卡机与识别卡之间的连线
: 读卡机程式设计,读取RFID卡序号
: 感谢~
我只做过二,一般来说,读卡机应该会提供 API 让你用
如果没有的话,你用 serialport 去连线装置也可以
serial port 就对照到「装置管理员」中的 COM port
c# 中读取读卡机很方便(可能是我用的读卡机型号刚好很普遍)
首先 include 用到的 library
using System.IO.Ports;
接着宣告 SerialPort
private SerialPort comport = new SerialPort();
你可以另外写个 method 来初始化这个 comport
private void initSerialPort()
{
// If the port is open, close it.
if (comport.IsOpen) comport.Close();
else
{
comport.BaudRate = 9600;
comport.DataBits = 8;
comport.StopBits = System.IO.Ports.StopBits.One;
comport.Parity = Parity.None;
comport.PortName = "COM1"; // 请对照你的 COM port
comport.ReadTimeout = 500;
comport.WriteTimeout = 200;
// Open the port
try
{
comport.Open();
}
catch (IOException)
{
MessageBox.Show("找不到或无法开启指定的连接埠。");
}
}
}
接着 serialPort有提供 dataReceived 的 evnet
你可以加入 event
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
後这样去读取
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// 加 delay 是因为我们的读卡机会读太快,给他点时间读完卡号
Thread.Sleep(200);
// 读取 cardid
string readString = comport.ReadExisting();
}
记得关掉程式的时候,加上 event 去关闭 comport
comport.close();
大概就是这样…
--
虽然 google 就很多了…
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.145.202.66
1F:推 taso5566:感谢大大热心的回答~ 05/05 12:37
加一下标题的 keyword。for searching friendly
※ 编辑: chrisQQ 来自: 203.145.202.66 (05/15 15:38)