diff --git a/BPASmartClient.JakaRobot/JaKaHelper.cs b/BPASmartClient.JakaRobot/JaKaHelper.cs index 85c0ce2f..046ccc43 100644 --- a/BPASmartClient.JakaRobot/JaKaHelper.cs +++ b/BPASmartClient.JakaRobot/JaKaHelper.cs @@ -1,5 +1,7 @@ using BPASmartClient.Helper; using BPASmartClient.Message; +using System.Net; +using System.Net.NetworkInformation; using System.Text; namespace BPASmartClient.JakaRobot @@ -60,7 +62,7 @@ namespace BPASmartClient.JakaRobot //public const string SENCE_初始位 = "20000"; #endregion - private int rshd = -1; + public int rshd = -1; private bool login = false; /// @@ -74,49 +76,124 @@ namespace BPASmartClient.JakaRobot public bool IsIdle { get; set; } = false; public bool IsConnected { get { return login; } } public bool IsDeviceFirstInit { get; set; } + private string IpAddress { get; set; } + private bool IsPingOk { get; set; } + + public void Conn(string ip) + { + jakaAPI.create_handler(ip.ToCharArray(), ref rshd); + login = true; + } public void Connect(string ip) { - bool ErrorFlag = false; - ThreadManage.GetInstance().StartLong(new Action(() => + MessageLog.GetInstance.Show("节卡机器人连接中。。。。。"); + IpAddress = ip; + Task.Factory.StartNew(() => { - if (rshd == -1 || login == false) + while (!IsPingOk) { - try - { - jakaAPI.create_handler(ip.ToCharArray(), ref rshd); - login = true; - } - catch (Exception ex) - { - if (!ErrorFlag) - { - MessageLog.GetInstance.ShowEx(ex.ToString()); - ErrorFlag = true; - login = false; - } - Thread.Sleep(3000); - } + Ping myPing = new Ping(); + myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted); + myPing.SendAsync(IpAddress, 1000, null); + Thread.Sleep(3000); } - if (login && IsDeviceFirstInit==false) + }); + + + //bool ErrorFlag = false; + //ThreadManage.GetInstance().StartLong(new Action(() => + //{ + // if (rshd == -1 || login == false) + // { + // try + // { + // jakaAPI.create_handler(ip.ToCharArray(), ref rshd); + // login = true; + // } + // catch (Exception ex) + // { + // if (!ErrorFlag) + // { + // MessageLog.GetInstance.ShowEx(ex.ToString()); + // ErrorFlag = true; + // login = false; + // } + // Thread.Sleep(3000); + // } + // } + // if (login && IsDeviceFirstInit == false) + // { + // try + // { + // Power_On();//打开机器人电源 + // Thread.Sleep(1000); + // Enable_robot();//机器人上使能 + // IsDeviceFirstInit = true; + // MessageLog.GetInstance.Show("机器人已上电使能"); + // } + // catch (Exception ex) + // { + // MessageLog.GetInstance.ShowEx("机器人未完成上电和使能"); + // } + // } + // Thread.Sleep(5000); + //}), $"节卡机器人连接", true); + + } + + private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e) + { + if (e.Reply != null && e.Reply.Status == IPStatus.Success) + { + IsPingOk = true; + MessageLog.GetInstance.Show("节卡机器人物理网络准备就绪"); + Thread.Sleep(3000); + bool ErrorFlag = false; + ThreadManage.GetInstance().StartLong(new Action(() => { - try + if (rshd == -1 || login == false) { - Power_On();//打开机器人电源 - Thread.Sleep(1000); - Enable_robot();//机器人上使能 - IsDeviceFirstInit = true; - MessageLog.GetInstance.Show("机器人已上电使能"); + try + { + int result = jakaAPI.create_handler(IpAddress.ToCharArray(), ref rshd); + login = true; + MessageLog.GetInstance.Show($"节卡机器人创建连接成功,句柄:{rshd},返回值:{result}"); + } + catch (Exception ex) + { + if (!ErrorFlag) + { + MessageLog.GetInstance.ShowEx(ex.ToString()); + ErrorFlag = true; + login = false; + } + Thread.Sleep(3000); + } } - catch (Exception ex) + if (login && IsDeviceFirstInit == false) { - MessageLog.GetInstance.ShowEx("机器人未完成上电和使能"); + try + { + MessageLog.GetInstance.Show("节卡机器人打开电源准备"); + Thread.Sleep(2000); + Power_On();//打开机器人电源 + Thread.Sleep(1000); + MessageLog.GetInstance.Show("节卡机器人上使能准备"); + Enable_robot();//机器人上使能 + IsDeviceFirstInit = true; + MessageLog.GetInstance.Show("机器人已上电使能"); + } + catch (Exception ex) + { + MessageLog.GetInstance.ShowEx("机器人未完成上电和使能"); + } } - } - Thread.Sleep(5000); - }), $"节卡机器人连接", true); - + Thread.Sleep(5000); + }), $"节卡机器人连接", true); + } } + public void Power_On() { if (login) @@ -129,10 +206,10 @@ namespace BPASmartClient.JakaRobot { MessageLog.GetInstance.ShowEx(ave.ToString()); } - //catch (Exception ex) - //{ - // MessageLog.GetInstance.ShowEx(ex.ToString()); - //} + catch (Exception ex) + { + MessageLog.GetInstance.ShowEx(ex.ToString()); + } } else { @@ -324,8 +401,8 @@ namespace BPASmartClient.JakaRobot { try { - //加载 - p: file = Programname.ToCharArray(); + //加载 + p: file = Programname.ToCharArray(); MessageLog.GetInstance.Show($"调用文件名:{Programname}"); status = new JKTYPE.ProgramState(); jakaAPI.get_program_state(ref rshd, ref status); diff --git a/BPASmartClient/Control/FoodMenu.xaml b/BPASmartClient/Control/FoodMenu.xaml index 44e6465a..dc0e436c 100644 --- a/BPASmartClient/Control/FoodMenu.xaml +++ b/BPASmartClient/Control/FoodMenu.xaml @@ -1,12 +1,14 @@ - + @@ -14,19 +16,22 @@ - - - - - - + diff --git a/WpfApp1/App.xaml b/WpfApp1/App.xaml index 2e70522d..0c5af8ec 100644 --- a/WpfApp1/App.xaml +++ b/WpfApp1/App.xaml @@ -1,9 +1,8 @@ - - - - + + diff --git a/WpfApp1/MainWindow.xaml b/WpfApp1/MainWindow.xaml index cfada060..9e5f5e2b 100644 --- a/WpfApp1/MainWindow.xaml +++ b/WpfApp1/MainWindow.xaml @@ -1,12 +1,29 @@ - + - +