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