Exemplo n.º 1
0
 //获取SerialDriver对象
 static public SerialDriver GetDriver()
 {
     if (onlyone == null)
     {
         onlyone = new SerialDriver();
     }
     return(onlyone);
 }
Exemplo n.º 2
0
 //销毁端口
 public void destroy()
 {
     if (ComDevice != null && ComDevice.IsOpen)
     {
         ComDevice.Close();
     }
     ComDevice = null;
     onlyone   = null;
 }
Exemplo n.º 3
0
        private void correct_tick_hdlr(object sender, EventArgs e)
        {
            Console.WriteLine("ticker start");
            int index = (int)(((DispatcherTimer)sender).Tag);

            if (correctitems[index].retrycount > correctstrategy_retry)  //超出重发次数,停止定时器
            {
                if (((DispatcherTimer)(sender)).IsEnabled)
                {
                    ((DispatcherTimer)(sender)).Stop();
                }
                correctitems[index].status = correctstatus.FAIL;
            }

            SerialDriver.GetDriver().Send(makeCommandPct(index));
            correctitems[index].retrycount++;
        }
Exemplo n.º 4
0
        private void timeCycle(object sender, EventArgs e)
        {
            tickcount++;
            string line = "";
            string ch;

            //Console.WriteLine("tick " + tickcount.ToString());
            try
            {
                //line = sr.ReadLine();
                if (sr.EndOfStream == false)
                {
                    ch    = sr.ReadLine();
                    line += ch;
                }
                else
                {
                    //clear context
                    aFile.Close();
                    aFile = null;
                    sr.Close();
                    sr = null;
                    readDataTimer.Stop();
                    readDataTimer.IsEnabled = false;
                    readDataTimer           = null;
                    Console.WriteLine("end of file. env released !");
                }

                // Read data in line by line.
                if (line != null)
                {
                    //Console.WriteLine("write to serial:" + line);
                    //line = line + "\r\n";
                    //byte[] b = { 1, 2, 3, 4, 5 };
                    if (SerialDriver.GetDriver().isOpen())
                    {
                        SerialDriver.GetDriver().Send(Encoding.Default.GetBytes(line));
                    }
                    ;
                    //line = sr.ReadLine();
                }
                else
                {
                    //clear context
                    aFile.Close();
                    aFile = null;
                    sr.Close();
                    sr = null;
                    readDataTimer.Stop();
                    readDataTimer.IsEnabled = false;
                    readDataTimer           = null;
                    Console.WriteLine("env released !");
                }
            }
            catch (Exception error)
            {
                //clear context
                aFile.Close();
                aFile = null;
                sr.Close();
                sr = null;
                readDataTimer.Stop();
                readDataTimer.IsEnabled = false;
                readDataTimer           = null;
                Console.WriteLine("error occure, env is released !");
            }
            ;
        }