问题描述
我正在尝试读取RFID标签并将其显示在我的库存c#应用程序中。我通过Arduino应用程序访问它,当我通过RFID Keychain时,它在控制监视器上给出了正确的输出。但是当我试图通过visual studio访问它时,它只运行一次然后让我访问被拒绝!并且还停止在Arduino IDE上工作,说端口很忙!
我尝试过:
I am trying to read RFID tags and display it my inventory c# application. I have accessed it through Arduino application which gives me the right output on control monitor when I pass RFID Keychain. But when I tried to access it through visual studio, it works only once and then gives me access is denied!and also stopped working on Arduino IDE saying the port is busy!
What I have tried:
public products()
{
InitializeComponent();
initializeRFIDPort();
}
//Function to initialize the serial port
SerialPort RFIDPort
public SerialPort initializeRFIDPort()
{
try
{
RFIDPort = new SerialPort("COM4",9600,Parity.None,8, StopBits.One);
if (RFIDPort.IsOpen)
RFIDPort.Close();
if(!RFIDPort.IsOpen)
RFIDPort.Open();
}
catch (UnauthorizedAccessException ex) {MessageBox.Show( ex.Message); }
catch (Exception)
{
RFIDPort = null;
}
return RFIDPort;
}
我班上有两个扫描按钮:
I have two scan button in my class:
private void ScanButton_Click(object sender, EventArgs e)
{
try
{
scanButtonIsClicked = true;
if (RFIDPort.IsOpen)
{
RFIDPort.DataReceived += serialPort1_DataReceived;
textBox1.Text = "";
}
else
MessageBox.Show("RFID Reader is not connected!");
}
catch (IOException) { MessageBox.Show("Please reconnect your device "); }
catch (System.Exception)
{
MessageBox.Show("Please Try Again");
}
}
private void Searchbutton_Click(object sender, EventArgs e)
{
scansearchbtn = true;
scanButtonIsClicked = false;
try
{
if (RFIDPort.IsOpen)
{
RFIDPort.DataReceived += serialPort1_DataReceived;
textBox2.Text = "";
}
else { MessageBox.Show("RFID Reader is not connected!"); }
}
catch (IOException) { MessageBox.Show("Please reconnect your device ");}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
string line = RFIDPort.ReadLine();
if (scanButtonIsClicked == true)
this.BeginInvoke(new LineReceivedEvent(LineReceived), line);
else
this.BeginInvoke(new LineReceivedEvent(Line2Received), line);
}
catch (Exception){ MessageBox.Show("Can't Read from RFID Device.Please try again"); }
}
private delegate void LineReceivedEvent(string line);
private void LineReceived(string line)
{
textBox2.Text = line;
}
private void Line2Received(string line)
{
textBox1.Text = line;
}
然后我在Forum-Closing中关闭了串口。当我关闭包含RFIDPort.Close()的程序时,也是I / O中止的例外。
如果有人能引导我走正确的路。我已多次尝试!!
and then I closed serial port in Forum-Closing. when I close the program which contains RFIDPort.Close(), also an exception that I/O is aborted.
please If anyone can lead me to the right way. I have try many times!!
推荐答案
这篇关于未经授权的异常串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!