未经授权的异常串口 [英] Unauthorized exception serialport

查看:83
本文介绍了未经授权的异常串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取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!!

推荐答案

一次只能有一个应用程序打开一个串口。



任何悬挂开放串口的应用都需要被杀死以释放端口。



当你创建端口时,是以关闭开始;所有的检查都是多余的。



所有空异常块都没用。不要使用空块,因为它会破坏默认;通过(最后)例外(基础)类报告。
Only one app can "open" a serial port at a time.

Any app that is "hung" with an open serial port needs to be killed to release the port.

And when you "create" the port, is starts as "closed"; all your "checking" is redundant.

And all your "empty" exception blocks are useless. Don't use "empty" blocks because it defeats the "default"; which is to report via the (last) "Exception" (base) class.


这篇关于未经授权的异常串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆