Example #1
0
        /// <summary>
        /// 后台线程提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == -1)
            {
                byte[] sendbuffer = e.UserState as byte[];
                Console.WriteLine("TX: " + BitConverter.ToString(sendbuffer, 0).Replace("-", " "));
                try
                {
                    sw = File.AppendText(logfile);
                    sw.WriteLine(DateTime.Now.ToString() + " TX: " + BitConverter.ToString(sendbuffer, 0).Replace("-", " "));
                    sw.Close();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            else if (e.ProgressPercentage > 0)
            {
                byte[] receive = new byte[e.ProgressPercentage];
                Array.Copy(e.UserState as byte[], receive, e.ProgressPercentage);

                Console.WriteLine("RX: " + BitConverter.ToString(receive, 0).Replace("-", " "));
                try
                {
                    sw = File.AppendText(logfile);
                    sw.WriteLine(DateTime.Now.ToString() + " RX: " + BitConverter.ToString(receive, 0).Replace("-", " "));
                    sw.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                try
                {
                    byte[] btemp = new byte[2] { receive[5], receive[4] };
                    int bufferlen = BitConverter.ToInt16(btemp, 0) + 6;
                    if (bufferlen == receive.Length)//报文长度正确
                    {
                        TCP tcptemp = new TCP(receive, m_len, m_startAddr, dataType);

                        if ((tcptemp.Responseread != null) && (tcptemp.Responseread.ByteNum != 0))
                        {

                            Console.WriteLine("RX: FC:{0} ", tcptemp.Responseread.FC);
                            try
                            {
                                sw = File.AppendText(logfile);
                                sw.WriteLine(DateTime.Now.ToString() + " RX: FC:{0} ", tcptemp.Responseread.FC);
                                sw.Close();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }

                            var datas = tcptemp.GetData();
                            foreach (var data in datas)
                            {
                                //if (data.Addr == 0) continue;
                                Console.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                    "data:" + data.Data.ToString());
                                try
                                {
                                    sw = File.AppendText(logfile);
                                    sw.WriteLine("RX: " + "addr:" + data.Addr.ToString() + " " +
                                                        "data:" + data.Data.ToString());
                                    sw.Close();
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.ToString());
                                }

                                numInf numtemp = new numInf();
                                if (find.TryGetValue(data.Addr, out numtemp))
                                {
                                    numtemp.val = Convert.ToSingle(data.Data);
                                    numtemp.dtm = DateTime.Now;

                                    find.Remove(data.Addr);
                                    find.Add(data.Addr, numtemp);
                                }
                                else
                                {
                                    numtemp.val = Convert.ToSingle(data.Data);
                                    numtemp.dtm = DateTime.Now;
                                    find.Add(data.Addr, numtemp);
                                }
                            }
                            sendFlag = true;
                            Console.WriteLine("\n");
                            try
                            {
                                sw = File.AppendText(logfile);
                                sw.WriteLine("\r\n");
                                sw.Close();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                        }//end if ((tcptemp.Responseread != null) && (tcptemp.Responseread.ByteNum != 0))
                    }//end if (bufferlen == buffer.Length)
                }// end try
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    //System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            }
        }