如何使用C#中的串行端口从RFID读取和写入数据?
我正在尝试使用C#中的串行端口从RFID读取/写入数据。 我使用了下面的代码,但它不起作用。 我在手册中写了一个命令,但收到的数据完全不同:如何使用C#中的串行端口从RFID读取和写入数据?,c#,serial-port,rfid,C#,Serial Port,Rfid,我正在尝试使用C#中的串行端口从RFID读取/写入数据。 我使用了下面的代码,但它不起作用。 我在手册中写了一个命令,但收到的数据完全不同: 使用System.Text; 使用System.Threading.Tasks; 使用System.Windows.Forms; 使用System.IO.Ports; 命名空间标记检测器 { 公共部分类Form1:Form { 串行端口mySerialPort; 公共表格1() { 初始化组件(); } 私有void Form1\u加载(对象发送方、事件
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO.Ports;
命名空间标记检测器
{
公共部分类Form1:Form
{
串行端口mySerialPort;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
mySerialPort=新的串行端口(“COM1”,115200,奇偶校验。无,8,停止位。1);
mySerialPort.Open();
}
私有void btnStart_单击(对象发送方,事件参数e)
{
mySerialPort.Write('CSE');
mySerialPort.DataReceived+=mySerialPort_DataReceived;
}
void mySerialPort_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
系统.线程.线程.睡眠(500);
SerialPort sp=(SerialPort)发送方;
字符串indata=sp.ReadExisting();
控制台。写入(“接收到的数据:”);
控制台写入线(indata);
}
私有作废Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
mySerialPort.Close();
mySerialPort.Dispose();
}
}
}
我尝试用环境.NewLine
,\x0d\x0a
,\r\n
替换
,但仍然不起作用
我的应用程序收到的数据:7?8?V7?7?7?7?7?7?7?7?7?7?7?7?
就像一个回复。
每个字符之间为非打印字符:
我希望你能帮助我。
如果我的英语不好,请表示感谢和抱歉。尝试更改代码和RFID驱动程序中的奇偶校验位、数据位和停止位。设备设置不正确的可能性很大。
“'CSE'”
根据您链接的文档图片,CSE和“
之间只有5个字符,但您使用了6个空格。第二个表示(\n)和(\r),即\n\r
(不是\r\n
!!!)。希望这有点帮助;)我看到了配置并将协议从CR-16
更改为
,现在我得到:接收到的数据:7
每个7之间有两条线路馈送。停止位和数据位不能更改。我将奇偶校验改为奇数、无和偶数,得到了相同的结果。elgonzo:你说得对,但是我得到了与\n\r
和5个空格相同的回答。谢谢你的光临!
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace TagDetector
{
public partial class Form1 : Form
{
SerialPort mySerialPort;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
mySerialPort = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
mySerialPort.Open();
}
private void btnStart_Click(object sender, EventArgs e)
{
mySerialPort.Write("'CSE '<LF><CR>");
mySerialPort.DataReceived += mySerialPort_DataReceived;
}
void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
System.Threading.Thread.Sleep(500);
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.Write("Data Received:");
Console.WriteLine(indata);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
mySerialPort.Close();
mySerialPort.Dispose();
}
}
}