frmCraneScale.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using com.hnshituo.core.webapp.vo;
  2. using Common;
  3. using Infragistics.Win;
  4. using JC_MeasuringSystem;
  5. using MeterPlugInLibrary;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. namespace CraneScaleSystem
  19. {
  20. public partial class frmCraneScale : Form
  21. {
  22. public frmCraneScale()
  23. {
  24. InitializeComponent();
  25. ClsControlPack.ShowGridCaption<MeterWorkCraneActual>(ultraGridYb.DisplayLayout.Bands[0]);
  26. }
  27. CraneScaleCollectModel scModel1 = new CraneScaleCollectModel(); //静态1
  28. //实时表数据采集
  29. private CraneScaleControl collection = new CraneScaleControl();
  30. MeterWorkCraneActualService craneActualService = new MeterWorkCraneActualService();
  31. PreTrackScaleService trackScaleService = new PreTrackScaleService();
  32. private Log l = Log.GetInstance(); //日志
  33. Thread tkWgt = null;//采集线程
  34. bool wgtStart = false, bLock = false, btest = false;
  35. bool bMsgVisible = false;
  36. PreTrackScale scale = null;
  37. private CoreAppUser userinfo = null;
  38. private void frmCraneScale_Load(object sender, EventArgs e)
  39. {
  40. userinfo = ((ST_MainForm)(this.MdiParent)).AppUser;
  41. ClsControlPack.SetUltraGridRowFilter(ref ultraGridYb, true);
  42. ValueList vValid = new ValueList(); //状态(0=作废,1=有效,2=结净)
  43. vValid.ValueListItems.Add("2", "生成");
  44. vValid.ValueListItems.Add("1", "有效");
  45. vValid.ValueListItems.Add("0", "作废");
  46. ultraGridYb.DisplayLayout.Bands[0].Columns["valueFlag"].ValueList = vValid.Clone();
  47. ValueList vValidFst = new ValueList(); //状态(0=作废,1=有效,2=结净)
  48. vValidFst.ValueListItems.Add("2", "校磅");
  49. vValidFst.ValueListItems.Add("1", "皮重");
  50. vValidFst.ValueListItems.Add("0", "毛重");//weightType
  51. ultraGridYb.DisplayLayout.Bands[0].Columns["weightType"].ValueList = vValidFst.Clone();
  52. //LoadDb(); 通过moxic直接才的方式
  53. collection.EventDataCollectionArgs += new CraneScaleEventDataCollection(EventData);//数据采集1
  54. collection.Start("CAR18");
  55. }
  56. private void EventData(object o, CraneScaleCollectModel e)
  57. {
  58. //这里每隔0.5秒执行一次
  59. try
  60. {
  61. scModel1.mainWgt = e.mainWgt;
  62. scModel1.weightStatus = e.weightStatus;
  63. scModel1.datetime = e.datetime;
  64. if (!bLock)
  65. {
  66. if (!btest)
  67. {
  68. txtWeight.Invoke(new Action(() => { txtWeight.Text = scModel1.mainWgt + ""; }));
  69. }
  70. else
  71. {
  72. txtWeight.Invoke(new Action(() => { txtWeight.Text = "2000"; }));
  73. }
  74. }
  75. if (bMsgVisible)
  76. {
  77. lbMsgInfo.Invoke(new Action(() => { lbMsgInfo.Text = ""; }));
  78. }
  79. bMsgVisible = false;
  80. }
  81. catch (Exception ex)
  82. {
  83. if (!bMsgVisible)
  84. {
  85. lbMsgInfo.Invoke(new Action(() => { lbMsgInfo.Text = "数据采集异常!" + ex; }));
  86. bMsgVisible = true;
  87. }
  88. l.WriteLog(13, "frmCraneScale.EventData数据采集异常,请关闭界面并稍后打开,或联系管理员!\r\n异常原因:\r\n" + ex);
  89. }
  90. }
  91. #region 通过moxic直接才的方式
  92. /*
  93. Socket client = null;
  94. IPAddress ip = null;
  95. IPEndPoint point = null;
  96. DbEntiry db = null;
  97. private void LoadDb()
  98. {
  99. db = new DbEntiry
  100. {
  101. pointNo = "00001",
  102. pointName = "贵金属吊钩秤",
  103. moxicIp = "192.168.184.60",
  104. printName = @"\\172.31.170.14\7201h",
  105. endStr = 10,
  106. dbLength = 18,
  107. startLocation = 9,
  108. getLength = 6
  109. };
  110. ip = IPAddress.Parse(db.moxicIp);
  111. point = new IPEndPoint(ip, 4001);
  112. Start();
  113. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  114. //连接到服务器
  115. client.Connect(point);
  116. }
  117. private void Start()
  118. {
  119. tkWgt = new Thread(new ThreadStart(TaskWgtGet));
  120. tkWgt.Start();
  121. }
  122. private void Stop()
  123. {
  124. if (tkWgt != null)
  125. {
  126. tkWgt.Abort();
  127. tkWgt = null;
  128. }
  129. }
  130. private void TaskWgtGet()
  131. {
  132. while (wgtStart)
  133. {
  134. if (PbCache.collect_no == null) wgtStart = false;
  135. else
  136. {
  137. try
  138. {
  139. //这里每隔0.5秒执行一次
  140. if (!bLock)
  141. {
  142. if (btest)
  143. {
  144. txtWeight.Invoke(new Action(() => { txtWeight.Text = "2000"; }));
  145. continue;
  146. }
  147. }
  148. byte[] buffer = new byte[32 * 32];
  149. int n = client.Receive(buffer);
  150. if (db != null && db.isFz)
  151. {
  152. byte[] bf = buffer.Reverse().ToArray();
  153. int m = 0;
  154. foreach (byte b in bf)
  155. {
  156. if (b > 0) buffer[m++] = b;
  157. }
  158. }
  159. byte[] bt = new byte[db.dbLength];
  160. int iLast = 0, iCnt = 0;
  161. for (int i = n - 1; i > -1; i--)
  162. {
  163. //byte字节里面都是数值 0-15 0-12
  164. if (iLast == 0 && buffer[i] == db.endStr)
  165. {
  166. iLast = i;
  167. bt[iCnt] = buffer[i];
  168. }
  169. else if (iLast != 0)
  170. {
  171. if (iCnt < db.dbLength - 1)
  172. {
  173. bt[++iCnt] = buffer[i];
  174. }
  175. else
  176. {
  177. break;
  178. }
  179. }
  180. }
  181. Array.Reverse(bt);
  182. if (iCnt + 1 != db.dbLength) continue;
  183. byte[] btDb = new byte[db.getLength];
  184. int iC = 0;
  185. for (int i = db.startLocation; i < (db.startLocation + db.getLength); i++)
  186. {
  187. btDb[iC++] = bt[i - 1];
  188. }
  189. string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length);
  190. int iWgt = Convert.ToInt32(str);
  191. if (bMsgVisible)
  192. {
  193. lbMsgInfo.Invoke(new Action(() =>
  194. {
  195. lbMsgInfo.Visible = false;
  196. }));
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. lbMsgInfo.Invoke(new Action(() =>
  202. {
  203. lbMsgInfo.Text = "moxic取数解析失败!";
  204. }));
  205. bMsgVisible = true;
  206. l.WriteLog(23, $"moxic取数解析失败:{ex.Message}");
  207. }
  208. finally
  209. {
  210. Thread.Sleep(500);
  211. }
  212. }
  213. }
  214. try
  215. {
  216. //client.Shutdown();
  217. client.Disconnect(true);
  218. }
  219. catch { }
  220. }
  221. //*/
  222. #endregion
  223. //*
  224. /// <summary>
  225. ///完整终端地址:MOXAIP+MOXAPORT
  226. /// </summary>
  227. private IPEndPoint serverFullAddr;
  228. /// <summary>
  229. /// 连接套接字
  230. /// </summary>
  231. private Socket sock;
  232. //*/
  233. private void btnZero_Click(object sender, EventArgs e)
  234. {
  235. try
  236. {
  237. //*
  238. //设置IP和端口
  239. serverFullAddr = new IPEndPoint(IPAddress.Parse("192.168.184.60"), 4001);
  240. sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  241. //指定本地主机地址和端口号
  242. sock.Connect(serverFullAddr);
  243. //*/
  244. //发送固定的指令“Z“到串口服务器
  245. byte[] byteSend = System.Text.Encoding.Default.GetBytes("Z");
  246. byte[] message = new byte[1024];
  247. try
  248. {
  249. //发送数据
  250. sock.Send(byteSend);
  251. //client.Send(byteSend);
  252. l.WriteLog(23, "远程清零成功!");
  253. MessageBox.Show("远程清零下发指令下发成功,请关注重量变化!");
  254. }
  255. catch (Exception ex)
  256. {
  257. MessageBox.Show("远程清零下发指令出现错误,请联系管理员" + ex);
  258. }
  259. finally
  260. {
  261. if (!sock.Connected)
  262. {
  263. sock.Disconnect(true);
  264. }
  265. sock.Close();
  266. }
  267. }
  268. catch (Exception ee)
  269. {
  270. MessageBox.Show("服务器清零失败。。。请联系系统管理员" + ee);
  271. }
  272. }
  273. private void btnQuery_Click(object sender, EventArgs e)
  274. {
  275. scale = null;
  276. if (string.IsNullOrEmpty(txtQPredictionNo.Text.Trim()))
  277. {
  278. MessageBox.Show("请输入预报编号");
  279. txtQPredictionNo.Focus();
  280. return;
  281. }
  282. RESTfulResult<List<PreTrackScale>> resultPre = trackScaleService.doQueryWf(new PreTrackScale { predictionNo = txtQPredictionNo.Text.Trim() });
  283. if (resultPre.Succeed)
  284. {
  285. foreach (Control cn in gbEdit.Controls)
  286. {
  287. if (cn is TextBox)
  288. {
  289. cn.Text = "";
  290. }
  291. }
  292. cbIsTare.Checked = false;
  293. if (resultPre.Data == null || resultPre.Data.Count == 0)
  294. {
  295. MessageBox.Show("未找到预报信息");
  296. return;
  297. }
  298. else
  299. {
  300. txtPredictionNo.Text = resultPre.Data[0].predictionNo;
  301. txtCarrierUnitName.Text = resultPre.Data[0].carrierUnitName;
  302. txtForwardingUnitName.Text = resultPre.Data[0].forwardingUnitName;
  303. txtMatterName.Text = resultPre.Data[0].matterName;
  304. txtMemo.Text = resultPre.Data[0].memo;
  305. txtMeterTypeName.Text = resultPre.Data[0].meterTypeName;
  306. txtReceivingUnitName.Text = resultPre.Data[0].receivingUintName;
  307. txtCarNo.Text = resultPre.Data[0].carNo;
  308. scale = resultPre.Data[0];
  309. }
  310. }
  311. else
  312. {
  313. MessageBox.Show("查询失败:" + resultPre.ResultMessage);
  314. return;
  315. }
  316. Query();
  317. }
  318. private void btnRecover_Click(object sender, EventArgs e)
  319. {
  320. SaveDb("复磅");
  321. }
  322. private void btnSave_Click(object sender, EventArgs e)
  323. {
  324. SaveDb();
  325. }
  326. private void SaveDb(string sFb = default)
  327. {
  328. int iWgt = 0;
  329. int.TryParse(txtWeight.Text.Trim(), out iWgt);
  330. if (iWgt == 0)
  331. {
  332. MessageBox.Show("当前过磅重量为0,无法保存");
  333. return;
  334. }
  335. if (string.IsNullOrEmpty(sFb) && scale == null)
  336. {
  337. MessageBox.Show("请先输入预报编号进行查询");
  338. return;
  339. }
  340. bLock = true;
  341. MeterWorkCraneActual mwca = new MeterWorkCraneActual();
  342. if (scale != null)
  343. {
  344. //相同名称的均赋值一次
  345. EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
  346. mwca = entityBase.format<MeterWorkCraneActual>(scale, mwca);
  347. }
  348. mwca.weightType = "0";//毛重
  349. mwca.meterWeight = iWgt;
  350. mwca.valueFlag = "1";
  351. int seqNum = 1, meterNum = 1;
  352. int.TryParse(txtSeqNum.Text.Trim(), out seqNum);
  353. mwca.seqnum = seqNum;
  354. if (!string.IsNullOrEmpty(sFb))
  355. {
  356. cbIsTare.Checked = false;
  357. mwca.weightType = "2";//复磅
  358. mwca.seqnum = 1;
  359. mwca.predictionNo = null;
  360. }
  361. if (cbIsTare.Checked)
  362. {
  363. int.TryParse(txtMeterNum.Text.Trim(), out meterNum);
  364. if (meterNum < 1 || string.IsNullOrEmpty(txtMeterNum.Text.Trim()))
  365. {
  366. MessageBox.Show("计量皮重的时候,请输入大于0的件数");
  367. return;
  368. }
  369. mwca.meterNum = meterNum;
  370. mwca.weightType = "1";//皮重
  371. }
  372. mwca.createManNo = userinfo.userid;
  373. mwca.createManName = userinfo.username;
  374. mwca.createTime = DateTime.Now;
  375. mwca.actualFirstNo = DateTime.Now.ToString("yyyyMMddHHmmss");
  376. mwca.baseSpotNo = "00018";
  377. mwca.baseSpotName = "东区吊钩秤";
  378. RESTfulResult<MeterWorkCraneActual> result = craneActualService.doAddWf(mwca);
  379. bLock = false;
  380. if (result.Succeed)
  381. {
  382. MessageBox.Show("执行成功!");
  383. Query();
  384. }
  385. else
  386. {
  387. MessageBox.Show("保存失败:" + result.ResultMessage);
  388. }
  389. }
  390. private void button1_Click(object sender, EventArgs e)
  391. {
  392. btest = true;
  393. }
  394. private void Query()
  395. {
  396. MeterWorkCraneActual mwca = new MeterWorkCraneActual();
  397. mwca.predictionNo = txtQPredictionNo.Text.Trim();
  398. mwca.valueFlag = "1";
  399. RESTfulResult<List<MeterWorkCraneActual>> result = craneActualService.doQueryWf(mwca);
  400. if (result.Succeed)
  401. {
  402. if (result.Data == null || result.Data.Count == 0)
  403. {
  404. meterWorkCraneActualBindingSource.DataSource = new List<MeterWorkCraneActual>();
  405. txtSeqNum.Text = "1";
  406. }
  407. else
  408. {
  409. meterWorkCraneActualBindingSource.DataSource = result.Data;
  410. txtSeqNum.Text = (result.Data.Count + 1) + "";
  411. }
  412. ClsControlPack.RefreshAndAutoSize(ultraGridYb, true);
  413. }
  414. else
  415. {
  416. MessageBox.Show("查询失败:" + result.ResultMessage);
  417. }
  418. }
  419. private void frmCraneScale_FormClosing(object sender, FormClosingEventArgs e)
  420. {
  421. collection.Stop();
  422. }
  423. }
  424. }