2

我应该从多个设备读取标签,但使用单个端口。我可以这样做,但是当我读取一个在其前面保留标签的设备时,除非我删除标签,否则另一台设备不会读入字符串。我需要同时从所有设备读取标签。它适用于超级终端,因此问题出在代码中

我的代码:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    //int bytes = serialPort1.BytesToRead;
    ////create a byte array to hold the awaiting data
    //byte[] comBuffer = new byte[bytes];
    ////read the data and store it
    //serialPort1.Read(comBuffer, 0, bytes);
    //MessageBox.Show(comBuffer[bytes]);
    ////display the data to the user
    ////DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
    //serialPort1.Dispose();
    //serialPort1.Close();
    //if (serialPort1.IsOpen)
    //{
    //    serialPort1.ReadExisting();
    //    serialPort1.Close();
    //}

    string data = serialPort1.ReadLine();
    data = data.Substring(2, 2);

    if (data == "03")
    {
        //while (data == "03")
        //{
        string data1 = serialPort1.ReadLine();
        data1 = data1.Substring(4, 10);

        for (int b = 0; b < dataGridView3.Rows.Count; b++)
        {
            string floorno = dataGridView3.Rows[b].Cells[0].Value.ToString();
            SqlConnection conn = new SqlConnection(dba.GetConnectionString());
            SqlCommand cmd = new SqlCommand("Select Floor,RfidTag,DeviceId from LiftMonitor where Floor='" + 
                                            floorno + "'and DeviceId='03'", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            ds = new DataSet();
            SqlCommandBuilder commandBuilder1 = new SqlCommandBuilder(da);
            da.Fill(ds, "LiftMonitor");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                DataRow dr = ds.Tables[0].Rows[j];
                string rfidtag = dr["RfidTag"].ToString();

                if (rfidtag == data1)
                {

                    dataGridView3.ClearSelection();
                    dataGridView3.Rows[b].Selected = true;
                    goto abc;
                    timer1.Start();
                    SqlConnection conn1 = new SqlConnection(dba.GetConnectionString());
                    SqlCommand cmd1 = new SqlCommand(" Select Timer from Setting ", conn1);
                    SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
                    DataSet ds1 = new DataSet();
                    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da1);
                    da1.Fill(ds1, "Timer");
                    for (int k = 0; k < ds1.Tables[0].Rows.Count; k++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[k];
                        string timer = dr1["Timer"].ToString();
                        int timer2 = Convert.ToInt32(timer);
                        timeConversion = timer2 * 1000;
                        if (timeConversion.Equals(timer1.Interval))
                        {
                            timer1.Tick += new System.EventHandler(timer1_Tick);
                        }
                    }
                }

                else
                {

                }
            }
        }
        //}
    }
    if (data == "04")
    {
        //while (data == "04")
        //{

        string data1 = serialPort1.ReadLine();
        //data1 = data1.Substring(1, 2);
        data1 = data1.Substring(4, 10);
        for (int b = 0; b < dataGridView4.Rows.Count; b++)
        {
            string floorno = dataGridView4.Rows[b].Cells[0].Value.ToString();
            SqlConnection conn = new SqlConnection(dba.GetConnectionString());
            SqlCommand cmd = new SqlCommand("Select Floor,RfidTag,DeviceId from LiftMonitor where Floor='" + 
                                            floorno + "' and DeviceId='04'", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            ds = new DataSet();
            SqlCommandBuilder commandBuilder1 = new SqlCommandBuilder(da);
            da.Fill(ds, "LiftMonitor");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                DataRow dr = ds.Tables[0].Rows[j];
                string rfidtag = dr["RfidTag"].ToString();

                if (rfidtag == data1)
                {

                    dataGridView4.ClearSelection();
                    dataGridView4.Rows[b].Selected = true;
                    goto abc;

                    timer1.Start();
                    SqlConnection conn1 = new SqlConnection(dba.GetConnectionString());
                    SqlCommand cmd1 = new SqlCommand(" Select Timer from Setting ", conn1);
                    SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
                    DataSet ds1 = new DataSet();
                    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da1);
                    da1.Fill(ds1, "Timer");
                    for (int k = 0; k < ds1.Tables[0].Rows.Count; k++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[k];
                        string timer = dr1["Timer"].ToString();
                        int timer2 = Convert.ToInt32(timer);
                        timeConversion = timer2 * 1000;
                        if (timeConversion.Equals(timer1.Interval))
                        {
                            timer1.Tick += new System.EventHandler(timer1_Tick);
                        }
                    }
                }
                else
                {

                }
            }
        }
        //}
    }

    if (data == "05")
    {
        //while (data == "05")
        //{
        string data1 = serialPort1.ReadLine();
        data1 = data1.Substring(4, 10);

        for (int b = 0; b < dataGridView5.Rows.Count; b++)
        {
            string floorno = dataGridView5.Rows[b].Cells[0].Value.ToString();
            SqlConnection conn = new SqlConnection(dba.GetConnectionString());
            SqlCommand cmd = new SqlCommand("Select Floor,RfidTag,DeviceId from LiftMonitor where Floor='" + 
                                            floorno + "'and DeviceId='05'", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            ds = new DataSet();
            SqlCommandBuilder commandBuilder1 = new SqlCommandBuilder(da);
            da.Fill(ds, "LiftMonitor");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                DataRow dr = ds.Tables[0].Rows[j];
                string rfidtag = dr["RfidTag"].ToString();

                if (rfidtag == data1)
                {
                    dataGridView5.ClearSelection();
                    dataGridView5.Rows[b].Selected = true;
                    //break;
                    goto abc;

                    timer1.Start();
                    SqlConnection conn1 = new SqlConnection(dba.GetConnectionString());
                    SqlCommand cmd1 = new SqlCommand(" Select Timer from Setting ", conn1);
                    SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
                    DataSet ds1 = new DataSet();
                    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da1);
                    da1.Fill(ds1, "Timer");
                    for (int k = 0; k < ds1.Tables[0].Rows.Count; k++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[k];
                        string timer = dr1["Timer"].ToString();
                        int timer2 = Convert.ToInt32(timer);
                        timeConversion = timer2 * 1000;
                        if (timeConversion.Equals(timer1.Interval))
                        {
                            timer1.Tick += new System.EventHandler(timer1_Tick);
                        }
                    }
                }
                //else
                //{
                //    break;
                //}
            }
        }
    }
}
4

0 回答 0