AutoSMTRXY.cs 15 KB

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