System.UnauthorizedAccessException:'访问被拒绝的端口'COM5'。

时间:2019-03-12 04:32:14

标签: port

我正在尝试使用RFID标签进行写操作,但有时会出现该错误。有人知道如何解决此问题,或者我需要修改什么?

    namespace WindowsFormsApplication2
{
    public partial class escpaciente : Form
    {
        String medico;
        public escpaciente(string user)
        {
            InitializeComponent();
            this.medico = user;
            CheckForIllegalCrossThreadCalls = false;
            serialPort1.Open();
        }


        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            string Lectura = serialPort1.ReadExisting();
            idpac.Text = Lectura;
        }


        public void loginpac()
        {
            try
            {
                string cnn = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
                using (SqlConnection conexion = new SqlConnection(cnn))
                {
                    conexion.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM t_pacientes WHERE id_paciente='" + idpac.Text + "'", conexion))
                    {
                        SqlDataReader dr = cmd.ExecuteReader();

                        if (dr.Read())
                        {
                            prinpac prinpa = new prinpac(idpac.Text,medico);
                            prinpa.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("Número de paciente incorrecto");
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void escpaciente_Load(object sender, EventArgs e)
        {

        }

        private void idpac_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            loginpac();
        }

        private void logout_Click(object sender, EventArgs e)
        {
            inicio prin = new inicio(medico);
            prin.Show();
            this.Hide();
        }
    }
}

0 个答案:

没有答案