AutoSMTRXY.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using System.Text;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.IO.Ports;
  11. using HslCommunication;
  12. using System.Diagnostics.PerformanceData;
  13. namespace FileWatcher
  14. {
  15. public partial class AutoSMTRXY : Form
  16. {
  17. DataHelper dh;
  18. DataTable dt;
  19. DataTable DB;
  20. /// <summary>
  21. /// 用户编号
  22. /// </summary>
  23. string iusercode;
  24. /// <summary>
  25. /// 岗位资源
  26. /// </summary>
  27. string isource;
  28. Thread InitDB;
  29. /// 当前工序
  30. /// </summary>
  31. string istepcode;
  32. StringBuilder sql = new StringBuilder();
  33. /// <summary>
  34. /// 缓存的文件
  35. /// </summary>
  36. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  37. /// <summary>
  38. /// 缓存的文件夹
  39. /// </summary>
  40. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  41. ftpOperater ftp = new ftpOperater();
  42. HslCommunication.Profinet.Panasonic.PanasonicMewtocol plc;
  43. public AutoSMTRXY()
  44. {
  45. InitializeComponent();
  46. StartPosition = FormStartPosition.CenterScreen;
  47. }
  48. DataTable Dbfind;
  49. string IPAddress = "";
  50. private void nr_rule_DBChange(object sender, EventArgs e)
  51. {
  52. Dbfind = li_code.ReturnData;
  53. BaseUtil.SetFormValue(this.Controls, Dbfind);
  54. }
  55. private void Form1_Load(object sender, EventArgs e)
  56. {
  57. CountType.Text = "PLC";
  58. CheckForIllegalCrossThreadCalls = false;
  59. FormBorderStyle = FormBorderStyle.FixedSingle;
  60. InitDB = new Thread(ConnectDB);
  61. //添加监控事件
  62. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  63. stw.StartPosition = FormStartPosition.CenterScreen;
  64. stw.ShowDialog();
  65. List<string> CacheInf = new List<string>();
  66. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  67. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  68. {
  69. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  70. IPAddress = IpEntry.AddressList[i].ToString();
  71. }
  72. serialPort1.DataReceived += SerialPort1_DataReceived;
  73. //获取缓存信息
  74. try
  75. {
  76. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  77. Type.Text = BaseUtil.GetCacheData("Type").ToString();
  78. li_code.Text = BaseUtil.GetCacheData("Line").ToString();
  79. Read.Text = BaseUtil.GetCacheData("Read").ToString();
  80. Write.Text = BaseUtil.GetCacheData("Write").ToString();
  81. ToZero.Text = BaseUtil.GetCacheData("ToZero").ToString();
  82. CountType.Text = BaseUtil.GetCacheData("CountType").ToString();
  83. UseScan.Text = BaseUtil.GetCacheData("UseScan").ToString();
  84. }
  85. catch (Exception ex) { OperateResult.AppendText(ex.Message); }
  86. RemindTimer.Start();
  87. this.WindowState = FormWindowState.Normal;
  88. this.Size = new System.Drawing.Size(900, 400); // 替换为你需要的尺寸
  89. this.CenterToScreen();
  90. }
  91. double lastqty = 0;
  92. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  93. {
  94. int len = serialPort1.BytesToRead;
  95. Byte[] readBuffer = new Byte[len];
  96. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  97. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  98. string error = "";
  99. if (data != "")
  100. {
  101. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  102. if (lastqty > qty)
  103. {
  104. lastqty = 0;
  105. }
  106. NowQTY.Text = qty.ToString();
  107. if (Type.Text == "投入")
  108. {
  109. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  110. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  111. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  112. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  113. }
  114. else
  115. {
  116. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  117. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  118. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  119. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  120. }
  121. if (Counter.Checked)
  122. {
  123. LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, "", qty.ToString(), li_code.Text, "OK", out error);
  124. }
  125. lastqty = qty;
  126. }
  127. }
  128. private void ConnectDB()
  129. {
  130. dh = new DataHelper();
  131. SystemInf.dh = dh;
  132. ma_code.TableName = "make";
  133. ma_code.SelectField = "ma_code # 工单号";
  134. ma_code.FormName = Name;
  135. ma_code.DBTitle = "工单查询";
  136. ma_code.SetValueField = new string[] { "ma_code" };
  137. ma_code.Condition = "";
  138. ma_code.DbChange += nr_rule_DBChange;
  139. li_code.TableName = "line";
  140. li_code.SelectField = "li_code # 线别编号,li_name # 线别名称";
  141. li_code.FormName = Name;
  142. li_code.DBTitle = "线别查询";
  143. li_code.SetValueField = new string[] { "li_code" };
  144. li_code.Condition = "";
  145. li_code.DbChange += nr_rule_DBChange;
  146. }
  147. SerialPort serialPort1 = new SerialPort();
  148. string nextLine;
  149. private void Clean_Click(object sender, EventArgs e)
  150. {
  151. OperateResult.Clear();
  152. }
  153. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  154. {
  155. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  156. if (ExitConfirm != "Yes")
  157. {
  158. WindowState = FormWindowState.Minimized;
  159. e.Cancel = true;
  160. }
  161. }
  162. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  163. {
  164. SetAutoRun();
  165. }
  166. //音频提示站位,料号,剩余数量信息
  167. private void Remind()
  168. {
  169. if (RemindVoice.Checked)
  170. {
  171. sql.Clear();
  172. sql.Append("SELECT 总需求数,余数,站位,料号 from(select bar_batchqty,dsl_barcode,dsl_prodcode 料号, " +
  173. "dsl_location 站位,dsl_baseqty 用量,(select sum(case when dsl_getqty=0 then 1 else dsl_getqty end)" +
  174. " from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null) " +
  175. "上料数, (select max(to_char(dsl_indate ,'MM-DD hh24:mi:ss')) from devsmtlocation T where " +
  176. "T.dsl_location=devsmtlocation.dsl_location and dsl_usable>0)接料时间,dsl_madeqty 料卷产出数, allmadeqty " +
  177. " 站位产出数,DSL_VALIDTIME, dsl_remainqty,(select sum(dsl_remainqty)from devsmtlocation T where " +
  178. "T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null)余数,dsl_needqty 总需求数 " +
  179. " from devsmtlocation left join (select DSL_LINECODE alllincode,Dsl_Location allLocation, " +
  180. " sum(dsl_madeqty)allmadeqty from devsmtlocation group by DSL_LINECODE,Dsl_Location)A on " +
  181. " alllincode=devsmtlocation.dsl_linecode and allLocation=dsl_location left join barcode " +
  182. " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1) ");
  183. System.Data.DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  184. for (int i = 0; i < dt.Rows.Count; i++)
  185. {
  186. string 站位 = dt.Rows[i]["站位"].ToString();
  187. string 余数 = dt.Rows[i]["余数"].ToString();
  188. string 料号 = dt.Rows[i]["料号"].ToString();
  189. BaseUtil.PlaySound("站位" + 站位 + "物料" + 料号 + "数量剩余" + 余数 + "请及时接料");
  190. }
  191. }
  192. }
  193. private void SetAutoRun()
  194. {
  195. if (AutoStart.Checked) //设置开机自启动
  196. {
  197. string path = Application.ExecutablePath;
  198. RegistryKey rk = Registry.LocalMachine;
  199. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  200. rk2.SetValue("FileWatcher.exe", path);
  201. rk2.Close();
  202. rk.Close();
  203. }
  204. else //取消开机自启动
  205. {
  206. string path = Application.ExecutablePath;
  207. RegistryKey rk = Registry.LocalMachine;
  208. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  209. rk2.DeleteValue("FileWatcher.exe", false);
  210. rk2.Close();
  211. rk.Close();
  212. }
  213. }
  214. private void ms_sncode_KeyDown(object sender, KeyEventArgs e)
  215. {
  216. if (e.KeyCode == Keys.Enter)
  217. {
  218. if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'"))
  219. {
  220. OperateResult.AppendText("工单号不存在\n");
  221. OperateResult.ScrollToCaret();
  222. return;
  223. }
  224. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  225. {
  226. OperateResult.AppendText("线别不存在\n");
  227. OperateResult.ScrollToCaret();
  228. return;
  229. }
  230. string error = "";
  231. if (SN.Checked)
  232. {
  233. OperateResult write = plc.Write(Write.Text, 1);
  234. if (write.IsSuccess)
  235. {
  236. OperateResult.AppendText("通过,发送指令" + Write.Text + "写入1\n");
  237. OperateResult.ScrollToCaret();
  238. }
  239. else
  240. {
  241. OperateResult.AppendText("写入失败\n");
  242. OperateResult.ScrollToCaret();
  243. }
  244. System.Data.DataTable dt = (System.Data.DataTable)dh.ExecuteSql("select MSL_MAINSN from makesnlist where msl_sncode='" + ms_sncode.Text + "'", "select");
  245. if (dt.Rows.Count > 0)
  246. {
  247. string MainSN = dt.Rows[0]["MSL_MAINSN"].ToString();
  248. if (LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, MainSN, "0", li_code.Text, "OK", out error))
  249. {
  250. OperateResult.AppendText("SN:" + MainSN + "采集成功\n");
  251. OperateResult.ScrollToCaret();
  252. ms_sncode.Clear();
  253. }
  254. else
  255. {
  256. OperateResult.AppendText(error + "\n");
  257. OperateResult.ScrollToCaret();
  258. ms_sncode.Clear();
  259. }
  260. }
  261. ms_sncode.Clear();
  262. }
  263. }
  264. }
  265. private void RemindTimer_Tick(object sender, EventArgs e)
  266. {
  267. Remind();
  268. }
  269. private void StartWatch_Click(object sender, EventArgs e)
  270. {
  271. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  272. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  273. BaseUtil.SetCacheData("Line", li_code.Text);
  274. BaseUtil.SetCacheData("Read", Read.Text);
  275. BaseUtil.SetCacheData("Write", Write.Text);
  276. BaseUtil.SetCacheData("ToZero", ToZero.Text);
  277. BaseUtil.SetCacheData("Type", Type.Text);
  278. BaseUtil.SetCacheData("CountType", CountType.Text);
  279. BaseUtil.SetCacheData("UseScan", UseScan.Text);
  280. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  281. {
  282. OperateResult.AppendText("工单号不能为空\n");
  283. return;
  284. }
  285. if (CountType.Text == "")
  286. {
  287. OperateResult.AppendText("请选择计数类型\n");
  288. return;
  289. }
  290. if (ComPort.Text == "")
  291. {
  292. OperateResult.AppendText("请选择串口\n");
  293. return;
  294. }
  295. if (Type.Text == "")
  296. {
  297. OperateResult.AppendText("请选择投入产出类型\n");
  298. return;
  299. }
  300. if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
  301. {
  302. OperateResult.AppendText("线别不能为空\n");
  303. return;
  304. }
  305. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  306. if (ExitConfirm != "Yes")
  307. {
  308. return;
  309. }
  310. //设置按钮不可点击
  311. StartWatch.Enabled = false;
  312. ma_code.Enabled = false;
  313. StopWatch.Enabled = true;
  314. try
  315. {
  316. if (CountType.Text == "PLC")
  317. {
  318. plc = new HslCommunication.Profinet.Panasonic.PanasonicMewtocol();
  319. plc.Station = 238;
  320. HslCommunication.Core.Pipe.PipeSerialPort pipe = new HslCommunication.Core.Pipe.PipeSerialPort();
  321. pipe.SerialPortInni(ComPort.Text, 9600, 8, StopBits.One, Parity.Odd);
  322. pipe.RtsEnable = false;
  323. pipe.DtrEnable = false;
  324. pipe.SleepTime = 20;
  325. pipe.ReceiveTimeOut = 5000;
  326. plc.CommunicationPipe = pipe;
  327. }
  328. else
  329. {
  330. serialPort1.PortName = ComPort.Text;
  331. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  332. serialPort1.Open();
  333. }
  334. Timer.Start();
  335. }
  336. catch (Exception mes)
  337. {
  338. if (BaudRate.Text == "" || BaudRate.Text == "")
  339. OperateResult.AppendText(">>请先维护波特率和串口\n");
  340. else
  341. OperateResult.AppendText(">>" + mes.Message + "\n");
  342. }
  343. OperateResult.AppendText("开始执行监控\n");
  344. }
  345. private void StopWatch_Click(object sender, EventArgs e)
  346. {
  347. StartWatch.Enabled = true;
  348. ma_code.Enabled = true;
  349. StopWatch.Enabled = false;
  350. OperateResult.AppendText("停止执行监控\n");
  351. }
  352. bool realwritedata = false;
  353. private void Timer_Tick(object sender, EventArgs e)
  354. {
  355. if (CountType.Text == "PLC")
  356. {
  357. string error = "";
  358. OperateResult<short> read = plc.ReadInt16(Read.Text);
  359. //OperateResult.AppendText("读取数据\n");
  360. if (read.IsSuccess)
  361. {
  362. if (read.Content != 0)
  363. {
  364. double qty = Convert.ToInt32(read.Content);
  365. if (lastqty > qty)
  366. {
  367. lastqty = 0;
  368. }
  369. else
  370. {
  371. if (qty - lastqty > 0)
  372. {
  373. OperateResult write = plc.Write(Write.Text, 1);
  374. if (write.IsSuccess)
  375. {
  376. OperateResult.AppendText("通过,发送指令" + Write.Text + "写入1\n");
  377. }
  378. else
  379. {
  380. OperateResult.AppendText("写入失败\n");
  381. }
  382. }
  383. }
  384. NowQTY.Text = qty.ToString();
  385. if (Type.Text == "投入")
  386. {
  387. string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  388. dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  389. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  390. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  391. }
  392. else
  393. {
  394. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  395. dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  396. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" +
  397. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert");
  398. }
  399. if (Counter.Checked)
  400. {
  401. LogicHandler.CS_SetResultRXY(ma_code.Text, li_code.Text, "", qty.ToString(), li_code.Text, "OK", out error);
  402. }
  403. lastqty = qty;
  404. }
  405. }
  406. else
  407. {
  408. OperateResult.AppendText("Read [D60] failed: " + read.Message);
  409. }
  410. }
  411. else
  412. {
  413. }
  414. }
  415. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  416. {
  417. string a = hs.Replace(" ", "");
  418. int bytelength = 0;
  419. if (a.Length % 2 == 0)
  420. {
  421. bytelength = a.Length / 2;
  422. }
  423. else
  424. {
  425. bytelength = a.Length / 2 + 1;
  426. }
  427. byte[] b = new byte[bytelength];
  428. //逐个字符变为16进制字节数据
  429. for (int i = 0; i < bytelength; i++)
  430. {
  431. if (i == bytelength - 1)
  432. {
  433. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  434. }
  435. else
  436. {
  437. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  438. }
  439. }
  440. //按照指定编码将字节数组变为字符串
  441. return b;
  442. }
  443. private void CleanData_Click(object sender, EventArgs e)
  444. {
  445. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  446. if (ExitConfirm != "Yes")
  447. {
  448. plc.Write(ToZero.Text, 1);
  449. }
  450. }
  451. private void Setting_Click(object sender, EventArgs e)
  452. {
  453. }
  454. }
  455. }