Exemplo n.º 1
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            AbstractRecordInfo info = RecordInfoFactory.CreateInfo((int)comboBox3.SelectedValue);
            int no = Convert.ToInt32(numericUpDown1.Value * 10 + numericUpDown2.Value);

            info.MakeSendBuffer();
            info.SetReadNo(no);
            arInfo = info;
            this.btnRead.Enabled = false;
            tbsend.Text          = "";
            tbread.Clear();
            try
            {
                foreach (byte b in info.SendBuffer)
                {
                    tbsend.Text += b.ToString("X2") + " ";
                }
                serialPort1.PortName = comboBox1.Text;
                serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);

                serialPort1.Open();
                serialPort1.DiscardOutBuffer();
                serialPort1.DiscardInBuffer();
                serialPort1.Write(info.SendBuffer, 0, info.SendBuffer.Length);
                bWaitread = true;
                int     count     = 0;
                Boolean istimeout = false;
                while (bWaitread)
                {
                    if (count > 200)
                    {
                        istimeout = true;
                        break;
                    }
                    else
                    {
                        Thread.Sleep(100);
                        Application.DoEvents();
                    }
                    count++;
                }
                serialPort1.DiscardInBuffer();
                serialPort1.Close();
                if (istimeout)
                {
                    //MessageForm.Show("读取超时",new Exception());//MessageBox.Show("读取超时!");
                    this.Close();
                    throw new Exception("读取超时");
                }
                else
                {
                    int toread = Convert.ToInt32((int)(arInfo.DataBuffer[2] << 8) + (int)arInfo.DataBuffer[3]);
                    foreach (byte b in arInfo.DataBuffer)
                    {
                        tbread.AppendText(b.ToString("X2") + " ");
                    }
                    if (arInfo.DataBuffer.Length >= (toread + 6))
                    {
                        int len = arInfo.DataBuffer.Length;
                        if (len > toread + 6)
                        {
                            len = toread + 6;
                        }
                        byte[] tocrc = new byte[len - 2];
                        Array.Copy(arInfo.DataBuffer, tocrc, len - 2);
                        byte[] crc = UtilTools.CRCCalc(tocrc);
                        if (true)//crc[0] == arInfo.DataBuffer[len - 2] && crc[1] == arInfo.DataBuffer[len - 1])
                        {
                            info.ProcBufferWhenReadEnd(len);
                            info.ReadFinish();
                            isReaded = true;
                            MessageBox.Show("读取完成!");
                            this.Close();
                        }
                        else
                        {
                            isReaded = false;
                            MessageBox.Show("校验失败!");
                        }
                    }
                    else
                    {
                        isReaded = false;
                        MessageBox.Show("读取失败!");
                    }
                }
                this.btnRead.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageForm.Show(ex.Message, ex);//MessageBox.Show(ex.Message);
                this.btnRead.Enabled = true;
            }
        }