static void Main(string[] args) { Protocol protocol; string ip; int port; V2DLE dle; System.Net.Sockets.TcpClient tcp; protocol = new Protocol(); protocol.Parse(System.IO.File.ReadAllText(Protocol.CPath(AppDomain.CurrentDomain.BaseDirectory+"TIME.txt")),false); ip = protocol.ip; port = protocol.port; while (true) { try { bool isCommErr = false; tcp = new System.Net.Sockets.TcpClient(); tcp = ConnectTask(ip, port); dle = new V2DLE("DigitTimer", tcp.GetStream()); dle.OnCommError+=(s,a)=> { isCommErr = true; dle.Close(); }; while (!isCommErr) { System.Data.DataSet ds = protocol.GetSendDataSet("report_system_time"); SendPackage pkg = protocol.GetSendPackage(ds, 0xffff); pkg.cls = CmdClass.A; dle.Send(pkg); if (pkg.result == CmdResult.ACK) { System.Data.DataSet retDs = protocol.GetReturnDsByTextPackage(pkg.ReturnTextPackage); int yr,mon,dy,hr,min,sec; yr=System.Convert.ToInt32(retDs.Tables[0].Rows[0]["year"]); mon = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["month"]); dy = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["day"]); hr = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["hour"]); min = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["minute"]); sec = System.Convert.ToInt32(retDs.Tables[0].Rows[0]["second"]); DateTime dt = new DateTime(yr, mon, dy, hr, min, sec); Console.WriteLine(dt.ToString()); RemoteInterface.Util.SetSysTime(dt); } else { Console.WriteLine(pkg.result.ToString()); } System.Threading.Thread.Sleep(1000 * 60 ); } } catch (Exception ex) { ; } } }
static void Tc_Test() { Comm.V2DLE dle; System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(); client.Connect("192.192.85.40", 1001); dle = new Comm.V2DLE("test", client.GetStream()); dle.OnReceiveText += new Comm.OnTextPackageEventHandler(dev_OnReceiveText); dle.OnReport += new Comm.OnTextPackageEventHandler(dle_OnReport); // dle.Send(new Comm.SendPackage(Comm.CmdType.CmdSet, Comm.CmdClass.A,0xffff,new byte[]{0x02,11,10,1})); Comm.SendPackage pkg; while (true) { try { Console.ReadKey(); byte[] data = new byte[] { // 03, 1,60,1,10 0x0b }; pkg = new Comm.SendPackage(Comm.CmdType.CmdQuery, Comm.CmdClass.A, 0xffff, data); dle.Send(pkg); Console.WriteLine(Comm.V2DLE.ToHexString(pkg.ReturnTextPackage.Text)); } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); } } }
private void Connect_Task() { try { if (InConnect_Task) return; InConnect_Task = true; while (true) { this.IsConnected = false; try { if (m_tcpclient != null) { try { m_tcpclient.Close(); } catch { ;} } if (m_device != null) { try { m_device.Close(); } catch { ;} } m_tcpclient = new System.Net.Sockets.TcpClient(); m_tcpclient.Connect(new System.Net.IPAddress(V2DLE.getIP(m_ip)), m_port); m_device = new V2DLE(this.DeviceName,m_tcpclient.GetStream()); // m_device.OnReport += new OnTextPackageEventHandler(m_device_OnReport); this.IsConnected = true; this.m_device.OnCommError += new OnCommErrHandler(m_device_OnCommError); Console.WriteLine(m_ip + " connected!"); //try //{ // byte[] senddata = new byte[] { 0x01 };//get hw_status // this.m_device.Send(new SendPackage(CmdType.CmdSet, CmdClass.A, 0xffff, senddata)); //} //catch (Exception ex) //{ // Console.WriteLine(ex.Message); //} // new System.Threading.Thread(ClientWork).Start(); break; } catch { Console.WriteLine(this + "connecting error!,retry..."); GC.Collect(); GC.WaitForPendingFinalizers(); for (int i = 0; i < 500; i++) System.Diagnostics.Debug.Print("collect"); System.Threading.Thread.Sleep(20000); } } } finally { InConnect_Task = false; } }
private void Connect_Task() { try { if (InConnect_Task) return; InConnect_Task = true; while (true) { System.Threading.Thread.Sleep(20000); this.IsConnected = false; try { if (m_tcpclient != null) { try { m_tcpclient.Close(); } catch { ;} } if (m_device != null) { try { m_device.Close(); } catch { ;} } m_tcpclient = new System.Net.Sockets.TcpClient(); m_tcpclient.Connect(new System.Net.IPAddress(V2DLE.getIP(m_ip)), m_port); m_device = new V2DLE(m_tcpclient.GetStream()); // m_device.OnReport += new OnTextPackageEventHandler(m_device_OnReport); this.IsConnected = true; this.m_device.OnCommError += new OnCommErrHandler(m_device_OnCommError); ConsoleServer.WriteLine(m_ip + " connected!"); this.TC_SetDateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); //try //{ // byte[] senddata = new byte[] { 0x01 };//get hw_status // this.m_device.Send(new SendPackage(CmdType.CmdSet, CmdClass.A, 0xffff, senddata)); //} //catch (Exception ex) //{ // ConsoleServer.WriteLine(ex.Message); //} // new System.Threading.Thread(ClientWork).Start(); break; } catch { ConsoleServer.WriteLine(this + "connecting error!,retry..."); Util.GC(); } } } finally { InConnect_Task = false; } }