SerialPort.Read()方法混合来自两个答案

时间:2016-03-14 13:23:28

标签: c# asp.net winforms

我正在使用Serialport.Write()方法,然后在串口上处理收到的答案。 但有时在字节数组的开头接收到最后一个字节。 我在datareceived事件中使用以下代码

private void comport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    //System.Threading.Thread.Sleep(5);
    int i = 0;
    byte[] inputData;
    try
    {
        comport.DtrEnable = true;
        comport.RtsEnable = true;

        ByteToRead = comport.BytesToRead;

        //create array to store buffer data
        inputData = new byte[ByteToRead];
        inputData = new byte[ByteToRead];
        //read the data and store
        comport.Read(inputData, 0, ByteToRead);
        comport.ReadExisting();

        try
        {
            if (inputData != null)
            {
                int j = 0;
                foreach (byte b in inputData)
                {
                    valarray[j] = b.ToString("x2");
                    j = j + 1;
                    //textBox1.Text = textBox1.Text + valarray[j] + " ";
                }
            }
        }
        catch (Exception er)
        {
        }
    }
    catch (SystemException ex)
    {
        MessageBox.Show(ex.Message, "Data Received Event");
    }
}

我使用Serialport.Write()方法一个接一个地发送请求

和timer_tick()事件如下: -

private void timer1_Tick(object sender, EventArgs e)
{
    timer2.Enabled = false;
    //hetc1  


    int total3 = 0;
     byte[] buffer = new byte[12];
     try
     {
         buffer[0] = 0x2;
         buffer[1] = (byte)loggerid;
         buffer[2] = 0x10;
         buffer[3] = 0x1;
         buffer[4] = 0x0;
         buffer[5] = 0x0;
         buffer[6] = 0x0;
         buffer[7] = 0x0;
         buffer[8] = 0x0;
         buffer[9] = 0x0;
         buffer[10] = 0x0;

         total3 = 256 - (buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4] + buffer[5] + buffer[6] + buffer[7] + buffer[8] + buffer[9] + buffer[10]);

         int myInt3 = total3;
         //sbyte s = Convert.ToSByte(myInt3);
         byte u = (byte)myInt3;
         string myHex3 = u.ToString("X");  // gives you hex
         // int myNewInt = Convert.ToInt32(myHex, 16);
         int decValue3 = Convert.ToInt32(myHex3, 16);

         buffer[11] = Convert.ToByte(decValue3);

         comport.Write(buffer, 0, 12);
         System.Threading.Thread.Sleep(240);
         timer1.Enabled = false;
         //get_id1_values();

     }
     catch (Exception er)
     {
     }

     //get_id1_values();
     timer2.Enabled = true;
}

private void timer2_Tick(object sender, EventArgs e)
{
    get_id1_values();
    int total4 = 0;
    byte[] buffer1 = new byte[12];
    try
    {
        buffer1[0] = 0x2;
        buffer1[1] = (byte)loggerid;
        buffer1[2] = 0x10;
        buffer1[3] = 0x2;
        buffer1[4] = 0x0;
        buffer1[5] = 0x0;
        buffer1[6] = 0x0;
        buffer1[7] = 0x0;
        buffer1[8] = 0x0;
        buffer1[9] = 0x0;
        buffer1[10] = 0x0;

        total4 = 256 - (buffer1[0] + buffer1[1] + buffer1[2] + buffer1[3] + buffer1[4] + buffer1[5] + buffer1[6] + buffer1[7] + buffer1[8] + buffer1[9] + buffer1[10]);

        int myInt4 = total4;
        //sbyte s = Convert.ToSByte(myInt3);
        byte u1 = (byte)myInt4;
        string myHex4 = u1.ToString("X");  // gives you hex
        // int myNewInt = Convert.ToInt32(myHex, 16);
        int decValue4 = Convert.ToInt32(myHex4, 16);

        buffer1[11] = Convert.ToByte(decValue4);

        comport.Write(buffer1, 0, 12);
        System.Threading.Thread.Sleep(240);
    }
    catch (Exception er)
    {
    }
    timer2.Enabled = false;

    timer3.Enabled = true;
}


private void timer3_Tick(object sender, EventArgs e)
{
   // get_id2_values();
    //insertvalues();
    timer3.Enabled = false;
    timer1.Enabled = true;
    insert1 = 0;
    insert2 = 0;
}

0 个答案:

没有答案