AutoSMTRXY.cs 17 KB

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