AutoSMTRXY.cs 17 KB

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