none
access to the port 'com3' is denied RRS feed

  • Question

  • protected void readCOM3()
            {


                SerialPort sp3 = new SerialPort();
                string weight3;
                sp3.PortName = "COM3";
                sp3.BaudRate = 9600;
                sp3.DataBits = 8;
                if (!sp3.IsOpen == true)
                {
                    sp3.Close();
                }else
                {

                    sp3.Open();
                }
                weight3 = sp3.ReadLine().ToString();
                //label15.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                label15.Invoke((Action)(() => label15.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                sp3.Close();

                for (int k2 = 0; k2 < dataGridView1.Rows.Count; k2++)
                {
                    r1 = dataGridView1.Rows.Count;

                    if (Convert.ToBoolean(dataGridView1.Rows[k2].Cells["Export"].Value) == true)
                    {
                        dataGridView1.Rows[k2].Cells[5].Value = weiht.Text; sh = dataGridView1.Rows[k2].Cells[5].Value.ToString();

                        for (int k = 0; k < dataGridView1.Rows.Count; k++)
                        {
                            r1 = dataGridView1.Rows.Count;

                            if (Convert.ToBoolean(dataGridView1.Rows[k].Cells["Export"].Value) == true)
                            {
                                dataGridView1.Rows[k].Cells[5].Value = sh.ToString();
                                // label1.Text = sh.ToString();
                                label1.Invoke((Action)(() => label1.Text = sh.ToString()));
                                if (s >= 0 && s <= 300)
                                {
                                    // label2.Text = "0.10";
                                    label2.Invoke((Action)(() => label2.Text = "0.10"));
                                }
                                else
                                {
                                    if (s >= 301 && s <= 10000)
                                    {

                                        label2.Invoke((Action)(() => label2.Text = "0.04"));
                                    }
                                    else
                                    {
                                        if (s >= 4001 && s <= 40000)
                                        {
                                            label2.Invoke((Action)(() => label2.Text = "0.02"));
                                        }
                                        else
                                        {

                                        }
                                    }
                                }
                                loadt1();
                                Save();
                                scanrecode1();
                                //  loadgrid();
                                loadt11();
                            }
                        }
                    }
                }
            }



            int r1; string sh;
            protected void readCOM6()
            {
                SerialPort sp = new SerialPort();
                string weight6;
                sp.PortName = "COM6";
                sp.BaudRate = 9600;
                sp.DataBits = 8;
                if (!sp.IsOpen == true)
                {
                   sp.Close();
                }
                else
                {
                    sp.Open();
                }
                weight6 = sp.ReadLine().ToString();
                label15.Invoke((Action)(() => label15.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                weiht.Invoke((Action)(() => weiht.Text = weight6.ToString()));
                sp.Close();

                for (int k2 = 0; k2 < dataGridView1.Rows.Count; k2++)
                {
                    r1 = dataGridView1.Rows.Count;

                    if (Convert.ToBoolean(dataGridView1.Rows[k2].Cells["Export"].Value) == true)
                    {
                        dataGridView1.Rows[k2].Cells[5].Value = weiht.Text; sh = dataGridView1.Rows[k2].Cells[5].Value.ToString();

                        for (int k = 0; k < dataGridView1.Rows.Count; k++)
                        {
                            r1 = dataGridView1.Rows.Count;

                            if (Convert.ToBoolean(dataGridView1.Rows[k].Cells["Export"].Value) == true)
                            {
                                dataGridView1.Rows[k].Cells[5].Value = sh.ToString();
                                // label1.Text = sh.ToString();
                                label1.Invoke((Action)(() => label1.Text = sh.ToString()));
                                if (s >= 0 && s <= 300)
                                {
                                    // label2.Text = "0.10";
                                    label2.Invoke((Action)(() => label2.Text = "0.10"));
                                }
                                else
                                {
                                    if (s >= 301 && s <= 10000)
                                    {

                                        label2.Invoke((Action)(() => label2.Text = "0.04"));
                                    }
                                    else
                                    {
                                        if (s >= 4001 && s <= 40000)
                                        {
                                            label2.Invoke((Action)(() => label2.Text = "0.02"));
                                        }
                                        else
                                        {

                                        }
                                    }
                                }
                                loadt1();
                                Save();
                              scanrecode1();
                              //  loadgrid();
                                loadt11();
                            }
                        }
                    }
                }
            }

    protected void loadget()
            {
                Thread thr1 = new Thread(readCOM3); Thread thr2 = new Thread(readCOM6);
                thr1.Start(); thr2.Start();

                weighttime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }

     this data should come in datagridview first data will come next time it shows error

                  
    Tuesday, February 5, 2019 5:36 AM

All replies

  • first data will come next time it shows error

                  

     Look at these lines that you have written:

                if (!sp3.IsOpen == true)
                {
                    sp3.Close();
                }else
                {
                    sp3.Open();
                }

    It says "if the port is not open then close it. If it is opened then open it." This is probably not what you want. Most likely, you want something like the following:

                if (!sp3.IsOpen)
                {
                    sp3.Open();
                }

    Tuesday, February 5, 2019 7:32 AM
    Moderator
  • Hi madhuralakshmi,

    As Alberto Poblacion mentioned, if sp3 is close, we could execute the following code, I would suggest that you could jump out of the method.

    if (!sp3.IsOpen == true)
    {
        sp3.Close();
        //jump out of the method
        return;
    }else
    {
        sp3.Open();
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, February 13, 2019 2:33 AM
    Wednesday, February 6, 2019 3:03 AM
    Moderator