本文介绍了使用C#通过串口连接的USB RFID读取标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经设法通过 USB 端口和控制台应用程序使用下面的代码连接到我的 RFID 设备
I have manage to connect to my RFID devices through USB port with a console application using the code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using Gtk;
namespace Testing1
{
public class Testing1
{
public static SerialPort iSerialPort = new SerialPort();
static int Main()
{
string strException = string.Empty;
string strComPort = "COM17";
int nBaudrate=Convert.ToInt32(9600);
int nRet = OpenCom(strComPort, nBaudrate, out strException);
if (nRet != 0)
{
string strLog = "Connect reader failed, due to: " + strException;
Console.WriteLine(strLog);
//return;
}
else
{
string strLog = "Reader connected " + strComPort + "@" + nBaudrate.ToString();
Console.WriteLine(strLog);
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
iSerialPort.Close();
return 0;
}
public static int OpenCom(string strPort, int nBaudrate, out string strException)
{
strException = string.Empty;
if (iSerialPort.IsOpen)
{
iSerialPort.Close();
}
try
{
iSerialPort.PortName = strPort;
iSerialPort.BaudRate = nBaudrate;
iSerialPort.ReadTimeout = 3000;
iSerialPort.DataBits = 8;
iSerialPort.Parity = Parity.None;
iSerialPort.StopBits = StopBits.One;
iSerialPort.Open();
}
catch (System.Exception ex)
{
strException = ex.Message;
return -1;
}
return 0;
}
}
}
但是,现在我想从 RFID 标签读取数据.是否可以通过控制台应用程序?还是我必须有一个普通的 GUI 应用程序?每次标签在读取范围内时,应用程序应允许读取多个标签.
However, now i would like to read data from a RFID tag. Is it possible through console application? Or do i have to have a normal GUI application? The application should allow multiple tag read every time a tag is within the reading range.
推荐答案
请看下面我的回答:
- 可以使用控制台应用程序读取数据
为了读取数据,您需要订阅 DataReceived 事件,如下所示:
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
在此处提供更多信息:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx
这篇关于使用C#通过串口连接的USB RFID读取标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!