using Microsoft.Win32; using System; using System.Collections.Generic; using System.Data; using System.Threading; using System.Windows.Forms; using System.Text; using System.Net; using System.Net.Sockets; using System.IO.Ports; namespace FileWatcher { public partial class AutoSMTRXY : Form { DataHelper dh; DataTable dt; DataTable DB; /// /// 用户编号 /// string iusercode; /// /// 岗位资源 /// string isource; Thread InitDB; /// 当前工序 /// string istepcode; StringBuilder sql = new StringBuilder(); /// /// 缓存的文件 /// public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml"; /// /// 缓存的文件夹 /// public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/"; ftpOperater ftp = new ftpOperater(); public AutoSMTRXY() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; } DataTable Dbfind; string IPAddress = ""; private void nr_rule_DBChange(object sender, EventArgs e) { Dbfind = li_code.ReturnData; BaseUtil.SetFormValue(this.Controls, Dbfind); } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; FormBorderStyle = FormBorderStyle.FixedSingle; InitDB = new Thread(ConnectDB); //添加监控事件 SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序"); stw.StartPosition = FormStartPosition.CenterScreen; stw.ShowDialog(); List CacheInf = new List(); IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); for (int i = 0; i < IpEntry.AddressList.Length; i++) { if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) IPAddress = IpEntry.AddressList[i].ToString(); } serialPort1.DataReceived += SerialPort1_DataReceived; //获取缓存信息 try { Type.Text = BaseUtil.GetCacheData("Type").ToString(); li_code.Text = BaseUtil.GetCacheData("Line").ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message); } RemindTimer.Start(); } double lastqty = 0; private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { int len = serialPort1.BytesToRead; Byte[] readBuffer = new Byte[len]; serialPort1.Read(readBuffer, 0, len); //将数据读入缓存 string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", ""); string error = ""; if (data != "") { double qty = Convert.ToInt32(data.Substring(6, 8), 16); if (lastqty > qty) { lastqty = 0; } NowQTY.Text = qty.ToString(); if (Type.Text == "投入") { string ma_inqty = dh.getFieldDataByCondition("make", "nvl(ma_inqty,0)", "ma_code='" + ma_code.Text + "'").ToString(); dh.ExecuteSql("update make set ma_inqty=nvl(ma_inqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update"); dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" + "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert"); } else { string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString(); dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update"); dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_type,mhc_qty,mhc_linecode,mhc_pcbcount)" + "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + Type.Text + "','" + (qty - lastqty) + "','" + li_code.Text + "','" + pr_pcbacount.Value + "')", "insert"); } LogicHandler.CS_SetResult(ma_code.Text, li_code.Text, "", qty.ToString(), li_code.Text, "OK", out error); lastqty = qty; } } private void ConnectDB() { dh = new DataHelper(); SystemInf.dh = dh; ma_code.TableName = "make"; ma_code.SelectField = "ma_code # 工单号"; ma_code.FormName = Name; ma_code.DBTitle = "工单查询"; ma_code.SetValueField = new string[] { "ma_code" }; ma_code.Condition = ""; ma_code.DbChange += nr_rule_DBChange; li_code.TableName = "line"; li_code.SelectField = "li_code # 线别编号,li_name # 线别名称"; li_code.FormName = Name; li_code.DBTitle = "线别查询"; li_code.SetValueField = new string[] { "li_code" }; li_code.Condition = ""; li_code.DbChange += nr_rule_DBChange; } SerialPort serialPort1 = new SerialPort(); string nextLine; private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (ExitConfirm != "Yes") { WindowState = FormWindowState.Minimized; e.Cancel = true; } } private void AutoStart_CheckedChanged(object sender, EventArgs e) { SetAutoRun(); } //音频提示站位,料号,剩余数量信息 private void Remind() { if (RemindVoice.Checked) { sql.Clear(); sql.Append("SELECT 总需求数,余数,站位,料号 from(select bar_batchqty,dsl_barcode,dsl_prodcode 料号, " + "dsl_location 站位,dsl_baseqty 用量,(select sum(case when dsl_getqty=0 then 1 else dsl_getqty end)" + " from devsmtlocation T where T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null) " + "上料数, (select max(to_char(dsl_indate ,'MM-DD hh24:mi:ss')) from devsmtlocation T where " + "T.dsl_location=devsmtlocation.dsl_location and dsl_usable>0)接料时间,dsl_madeqty 料卷产出数, allmadeqty " + " 站位产出数,DSL_VALIDTIME, dsl_remainqty,(select sum(dsl_remainqty)from devsmtlocation T where " + "T.dsl_location=devsmtlocation.dsl_location and dsl_invalidtime is null)余数,dsl_needqty 总需求数 " + " from devsmtlocation left join (select DSL_LINECODE alllincode,Dsl_Location allLocation, " + " sum(dsl_madeqty)allmadeqty from devsmtlocation group by DSL_LINECODE,Dsl_Location)A on " + " alllincode=devsmtlocation.dsl_linecode and allLocation=dsl_location left join barcode " + " on dsl_barcode = bar_code where dsl_status=0 and dsl_usable=1) "); DataTable dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); for (int i = 0; i < dt.Rows.Count; i++) { string 站位 = dt.Rows[i]["站位"].ToString(); string 余数 = dt.Rows[i]["余数"].ToString(); string 料号 = dt.Rows[i]["料号"].ToString(); BaseUtil.PlaySound("站位" + 站位 + "物料" + 料号 + "数量剩余" + 余数 + "请及时接料"); } } } private void SetAutoRun() { if (AutoStart.Checked) //设置开机自启动 { string path = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.SetValue("FileWatcher.exe", path); rk2.Close(); rk.Close(); } else //取消开机自启动 { string path = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("FileWatcher.exe", false); rk2.Close(); rk.Close(); } } private void ms_sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'")) { OperateResult.AppendText("工单号不存在\n"); return; } if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'")) { OperateResult.AppendText("线别不存在\n"); return; } string error = ""; if (SN.Checked) { System.Data.DataTable dt = (System.Data.DataTable)dh.ExecuteSql("select MSL_MAINSN from makesnlist where msl_sncode='" + ms_sncode.Text + "'", "select"); if (dt.Rows.Count > 0) { string MainSN = dt.Rows[0]["MSL_MAINSN"].ToString(); if (LogicHandler.CS_SetResult(ma_code.Text, li_code.Text, MainSN, "0", li_code.Text, "OK", out error)) { OperateResult.AppendText("SN:" + MainSN + "采集成功\n"); } else { OperateResult.AppendText(error + "\n"); } } } } } private void RemindTimer_Tick(object sender, EventArgs e) { Remind(); } private void StartWatch_Click(object sender, EventArgs e) { BaseUtil.SetCacheData("ComPort", ComPort.Text); BaseUtil.SetCacheData("BaudRate", BaudRate.Text); BaseUtil.SetCacheData("Line", li_code.Text); if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'")) { OperateResult.AppendText("工单号不能为空\n"); return; } if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'")) { OperateResult.AppendText("线别不能为空\n"); return; } string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString(); if (ExitConfirm != "Yes") { return; } //设置按钮不可点击 StartWatch.Enabled = false; ma_code.Enabled = false; StopWatch.Enabled = true; try { serialPort1.PortName = ComPort.Text; serialPort1.BaudRate = int.Parse(BaudRate.Text); serialPort1.Open(); Timer.Start(); } catch (Exception mes) { if (BaudRate.Text == "" || BaudRate.Text == "") OperateResult.AppendText(">>请先维护波特率和串口\n"); else OperateResult.AppendText(">>" + mes.Message + "\n"); } OperateResult.AppendText("开始执行监控\n"); } private void StopWatch_Click(object sender, EventArgs e) { StartWatch.Enabled = true; ma_code.Enabled = true; StopWatch.Enabled = false; OperateResult.AppendText("停止执行监控\n"); } } }