终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

458 lines
16 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.Message;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Text;
  6. namespace BPASmartClient.JakaRobot
  7. {
  8. public class JaKaHelper
  9. {
  10. #region 果汁机设备
  11. /*public const string SENCE_取咖啡杯 = "10000";
  12. public const string SENCE_取果汁杯 = "10001";
  13. public const string SENCE_取茶水杯 = "10002";
  14. public const string SENCE_取水杯 = "10003";
  15. public const string SENCE_取咖啡杯检测 = "11000";
  16. public const string SENCE_取果汁杯检测 = "11001";
  17. public const string SENCE_取茶水杯检测 = "11002";
  18. public const string SENCE_取水杯检测 = "11003";
  19. public const string SENCE_接咖啡 = "12000";
  20. public const string SENCE_接果汁1 = "12001";
  21. public const string SENCE_接果汁2 = "12002";
  22. public const string SENCE_接果汁3 = "12003";
  23. public const string SENCE_接果汁4 = "12004";
  24. public const string SENCE_接茶 = "12005";
  25. public const string SENCE_接水 = "12006";
  26. public const string SENCE_放咖啡杯 = "13000";
  27. public const string SENCE_放果汁杯1 = "13001";
  28. public const string SENCE_放果汁杯2 = "13002";
  29. public const string SENCE_放果汁杯3 = "13003";
  30. public const string SENCE_放果汁杯4 = "13004";
  31. public const string SENCE_放茶水杯 = "13005";
  32. public const string SENCE_放水杯 = "13006";*/
  33. //public const string SENCE_取杯 = "10000";
  34. //public const string SENCE_放杯位检测 = "10100";
  35. //public const string SENCE_取杯检测 = "11000";
  36. //public const string SENCE_接咖啡 = "12000";
  37. //public const string SENCE_接果汁1 = "12001";
  38. //public const string SENCE_接果汁2 = "12002";
  39. //public const string SENCE_接果汁3 = "12003";
  40. //public const string SENCE_接果汁4 = "12004";
  41. //public const string SENCE_接茶 = "12005";
  42. //public const string SENCE_接水 = "12006";
  43. //public const string SENCE_接茶_接水 = "12007";
  44. //public const string SENCE_放咖啡杯 = "13000";//接饮料位---放饮料过渡位
  45. //public const string SENCE_放果汁杯1 = "13001";
  46. //public const string SENCE_放果汁杯2 = "13002";
  47. //public const string SENCE_放果汁杯3 = "13003";
  48. //public const string SENCE_放果汁杯4 = "13004";
  49. //public const string SENCE_放茶水杯 = "13005";
  50. //public const string SENCE_放杯 = "14000";//放杯过渡位到-放杯
  51. //public const string SENCE_放杯检测 = "15000";
  52. //public const string SENCE_初始位 = "20000";
  53. #endregion
  54. public int rshd = -1;
  55. private bool login = false;
  56. /// <summary>
  57. /// 机器人的状态
  58. /// </summary>
  59. private JKTYPE.RobotStatus robot_Status;
  60. private JKTYPE.ProgramState program_status;
  61. //private volatile static JaKaHelper _Instance;
  62. //public static JaKaHelper GetInstance => _Instance ?? (_Instance = new JaKaHelper());
  63. //private JaKaHelper() { }
  64. public bool IsIdle { get; set; } = false;
  65. public bool IsConnected { get { return login; } }
  66. public bool IsDeviceFirstInit { get; set; }
  67. private string IpAddress { get; set; }
  68. private bool IsPingOk { get; set; }
  69. public void Conn(string ip)
  70. {
  71. jakaAPI.create_handler(ip.ToCharArray(), ref rshd);
  72. login = true;
  73. }
  74. public void Connect(string ip)
  75. {
  76. MessageLog.GetInstance.Show("节卡机器人连接中。。。。。");
  77. IpAddress = ip;
  78. Task.Factory.StartNew(() =>
  79. {
  80. while (!IsPingOk)
  81. {
  82. Ping myPing = new Ping();
  83. myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
  84. myPing.SendAsync(IpAddress, 1000, null);
  85. Thread.Sleep(3000);
  86. }
  87. });
  88. //bool ErrorFlag = false;
  89. //ThreadManage.GetInstance().StartLong(new Action(() =>
  90. //{
  91. // if (rshd == -1 || login == false)
  92. // {
  93. // try
  94. // {
  95. // jakaAPI.create_handler(ip.ToCharArray(), ref rshd);
  96. // login = true;
  97. // }
  98. // catch (Exception ex)
  99. // {
  100. // if (!ErrorFlag)
  101. // {
  102. // MessageLog.GetInstance.ShowEx(ex.ToString());
  103. // ErrorFlag = true;
  104. // login = false;
  105. // }
  106. // Thread.Sleep(3000);
  107. // }
  108. // }
  109. // if (login && IsDeviceFirstInit == false)
  110. // {
  111. // try
  112. // {
  113. // Power_On();//打开机器人电源
  114. // Thread.Sleep(1000);
  115. // Enable_robot();//机器人上使能
  116. // IsDeviceFirstInit = true;
  117. // MessageLog.GetInstance.Show("机器人已上电使能");
  118. // }
  119. // catch (Exception ex)
  120. // {
  121. // MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
  122. // }
  123. // }
  124. // Thread.Sleep(5000);
  125. //}), $"节卡机器人连接", true);
  126. }
  127. private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
  128. {
  129. if (e.Reply != null && e.Reply.Status == IPStatus.Success)
  130. {
  131. IsPingOk = true;
  132. MessageLog.GetInstance.Show("节卡机器人物理网络准备就绪");
  133. Thread.Sleep(3000);
  134. bool ErrorFlag = false;
  135. ThreadManage.GetInstance().StartLong(new Action(() =>
  136. {
  137. if (rshd == -1 || login == false)
  138. {
  139. try
  140. {
  141. int result = jakaAPI.create_handler(IpAddress.ToCharArray(), ref rshd);
  142. login = true;
  143. MessageLog.GetInstance.Show($"节卡机器人创建连接成功,句柄:{rshd},返回值:{result}");
  144. }
  145. catch (Exception ex)
  146. {
  147. if (!ErrorFlag)
  148. {
  149. MessageLog.GetInstance.ShowEx(ex.ToString());
  150. ErrorFlag = true;
  151. login = false;
  152. }
  153. Thread.Sleep(3000);
  154. }
  155. }
  156. if (login && IsDeviceFirstInit == false)
  157. {
  158. try
  159. {
  160. MessageLog.GetInstance.Show("节卡机器人打开电源准备");
  161. Thread.Sleep(2000);
  162. Power_On();//打开机器人电源
  163. Thread.Sleep(1000);
  164. MessageLog.GetInstance.Show("节卡机器人上使能准备");
  165. Enable_robot();//机器人上使能
  166. IsDeviceFirstInit = true;
  167. MessageLog.GetInstance.Show("机器人已上电使能");
  168. }
  169. catch (Exception ex)
  170. {
  171. MessageLog.GetInstance.ShowEx("机器人未完成上电和使能");
  172. }
  173. }
  174. Thread.Sleep(5000);
  175. }), $"节卡机器人连接", true);
  176. }
  177. }
  178. public void Power_On()
  179. {
  180. if (login)
  181. {
  182. try
  183. {
  184. jakaAPI.power_on(ref rshd);
  185. }
  186. catch (System.AccessViolationException ave)
  187. {
  188. MessageLog.GetInstance.ShowEx(ave.ToString());
  189. }
  190. catch (Exception ex)
  191. {
  192. MessageLog.GetInstance.ShowEx(ex.ToString());
  193. }
  194. }
  195. else
  196. {
  197. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  198. }
  199. }
  200. public void Power_Off()
  201. {
  202. if (login)
  203. {
  204. try
  205. {
  206. jakaAPI.power_off(ref rshd);
  207. }
  208. catch (Exception ex)
  209. {
  210. }
  211. }
  212. else
  213. {
  214. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  215. }
  216. }
  217. public void Enable_robot()
  218. {
  219. if (!(rshd == -1))
  220. {
  221. try
  222. {
  223. jakaAPI.enable_robot(ref rshd);
  224. }
  225. catch (Exception ex)
  226. {
  227. }
  228. }
  229. else
  230. {
  231. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  232. }
  233. }
  234. public void disEnable_robot()
  235. {
  236. if (!(rshd == -1))
  237. {
  238. try
  239. {
  240. jakaAPI.disable_robot(ref rshd);
  241. }
  242. catch (Exception ex)
  243. {
  244. }
  245. }
  246. else
  247. {
  248. MessageLog.GetInstance.Show("jaka机器人未连接成功!");
  249. }
  250. }
  251. /// <summary>
  252. /// 设置机器人的DO值
  253. /// </summary>
  254. /// <param name="DOIndex"></param>
  255. /// <param name="value"></param>
  256. /// <returns></returns>
  257. public bool Set_RobotDO(int DOIndex, bool value)
  258. {
  259. try
  260. {
  261. jakaAPI.set_digital_output(ref rshd, JKTYPE.IOType.IO_CABINET, DOIndex, value);
  262. return value;
  263. }
  264. catch (Exception ex)
  265. {
  266. return false;
  267. }
  268. }
  269. /// <summary>
  270. /// 设置机器人的AO值
  271. /// </summary>
  272. /// <param name="value">设置值</param>
  273. /// <returns></returns>
  274. public int Set_RobotAO1(int Value)
  275. {
  276. try
  277. {
  278. return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, Value);
  279. }
  280. catch (Exception ex)
  281. {
  282. return 0;
  283. }
  284. }
  285. /// <summary>
  286. /// 设置机器人的AO值
  287. /// </summary>
  288. /// <param name="Index">索引值</param>
  289. /// <param name="value">设置值</param>
  290. /// <returns></returns>
  291. public int Set_RobotAO(int Index, int Value)
  292. {
  293. try
  294. {
  295. return jakaAPI.set_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, Value);
  296. }
  297. catch (Exception ex)
  298. {
  299. return 0;
  300. }
  301. }
  302. /// <summary>
  303. /// 获取机器人的AO值
  304. /// </summary>
  305. /// <param name="Index">索引值</param>
  306. /// <param name="value">设置值</param>
  307. /// <returns></returns>
  308. public float Get_RobotAO(int Index, int value)
  309. {
  310. try
  311. {
  312. float bResult = 0;
  313. jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, Index, ref bResult);
  314. return bResult;
  315. }
  316. catch (Exception ex)
  317. {
  318. return 0;
  319. }
  320. }
  321. /// <summary>
  322. /// 获取机器人的AO值
  323. /// </summary>
  324. /// <param name="Index">索引值</param>
  325. /// <param name="value">设置值</param>
  326. /// <returns></returns>
  327. public float Get_RobotAO1()
  328. {
  329. try
  330. {
  331. float bResult = 0;
  332. jakaAPI.get_analog_output(ref rshd, JKTYPE.IOType.IO_CABINET, 0, ref bResult);
  333. return bResult;
  334. }
  335. catch (Exception ex)
  336. {
  337. return 0;
  338. }
  339. }
  340. /// <summary>
  341. /// 获取机器人的DI输入
  342. /// </summary>
  343. /// <param name="index"></param>
  344. /// <returns></returns>
  345. public bool Get_RobotDI(int index)
  346. {
  347. bool bResult = false;
  348. jakaAPI.get_digital_input(ref rshd, JKTYPE.IOType.IO_CABINET, index, ref bResult);
  349. return bResult;
  350. }
  351. /// <summary>
  352. /// 获取机器人的状态
  353. /// </summary>
  354. public void GetRobotStatus()
  355. {
  356. jakaAPI.get_robot_status(ref rshd, ref robot_Status);
  357. }
  358. public JKTYPE.ProgramState GetProgramStatus()
  359. {
  360. jakaAPI.get_program_state(ref rshd, ref program_status);
  361. return program_status;
  362. }
  363. StringBuilder jakafile;
  364. JKTYPE.ProgramState status;
  365. char[] file;
  366. public void JaKaProgramName(String Programname)
  367. {
  368. try
  369. {
  370. //加载
  371. p: file = Programname.ToCharArray();
  372. MessageLog.GetInstance.Show($"调用文件名:{Programname}");
  373. status = new JKTYPE.ProgramState();
  374. jakaAPI.get_program_state(ref rshd, ref status);
  375. if (status != JKTYPE.ProgramState.PROGRAM_IDLE)
  376. {
  377. MessageLog.GetInstance.Show($"程序运行中,无法加载程序!!!");
  378. goto p;
  379. }
  380. else
  381. {
  382. if (jakaAPI.program_load(ref rshd, file) == 0)
  383. {
  384. MessageLog.GetInstance.Show($"加载程序完成");
  385. }
  386. else
  387. {
  388. MessageLog.GetInstance.Show($"加载程序失败!!!");
  389. goto p;
  390. }
  391. }
  392. if (status == JKTYPE.ProgramState.PROGRAM_RUNNING)
  393. {
  394. MessageLog.GetInstance.Show($"程序处于已处于启动状态!!!");
  395. goto p;
  396. }
  397. else if (status == JKTYPE.ProgramState.PROGRAM_IDLE)
  398. {
  399. jakafile = new StringBuilder();
  400. jakaAPI.get_loaded_program(ref rshd, jakafile);
  401. if (jakafile.Length == 0)
  402. {
  403. MessageLog.GetInstance.Show($"未加载程序,无法运行!!!");
  404. goto p;
  405. }
  406. else
  407. {
  408. MessageLog.GetInstance.Show($"当前加载程序为:" + jakafile);
  409. jakaAPI.program_run(ref rshd);
  410. MessageLog.GetInstance.Show($"程序启动");
  411. }
  412. }
  413. else
  414. {
  415. MessageLog.GetInstance.Show($"程序处于暂停状态,无法重新启动程序!!!");
  416. }
  417. }
  418. catch (Exception ex)
  419. {
  420. MessageLog.GetInstance.ShowEx(ex.ToString());
  421. }
  422. }
  423. }
  424. }