| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using System.Xml;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Text.RegularExpressions;
- using System.IO.Ports;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- using System.Windows.Media.Media3D;
- using System.Runtime.InteropServices;
- namespace FileWatcher
- {
- public partial class AutoMakeQTY : Form
- {
- DataHelper dh;
- DataTable dt;
- DataTable DB;
- /// <summary>
- /// 用户编号
- /// </summary>
- string iusercode;
- /// <summary>
- /// 岗位资源
- /// </summary>
- string isource;
- Thread InitDB;
- /// 当前工序
- /// </summary>
- string istepcode;
- StringBuilder sql = new StringBuilder();
- /// <summary>
- /// 缓存的文件
- /// </summary>
- public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
- /// <summary>
- /// 缓存的文件夹
- /// </summary>
- public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
- ftpOperater ftp = new ftpOperater();
- public AutoMakeQTY()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- string IPAddress = "";
- private void Form1_Load(object sender, EventArgs e)
- {
- CheckForIllegalCrossThreadCalls = false;
- FormBorderStyle = FormBorderStyle.FixedSingle;
- serialPort1.DataReceived += SerialPort1_DataReceived;
- InitDB = new Thread(ConnectDB);
- //添加监控事件
- XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
- //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
- SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
- stw.StartPosition = FormStartPosition.CenterScreen;
- stw.ShowDialog();
- List<string> CacheInf = new List<string>();
- 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();
- }
- //获取缓存信息
- try
- {
- Type.Text = BaseUtil.GetCacheData("Type").ToString();
- ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
- BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
- }
- catch (Exception ex) { MessageBox.Show(ex.Message); }
- }
- 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("-", "");
- double Num1 = double.Parse(data.Substring(8, 2)) * 10000;
- double Num2 = double.Parse(data.Substring(10, 2)) * 1000;
- double Num3 = double.Parse(data.Substring(12, 2)) * 100;
- double Num4 = double.Parse(data.Substring(14, 2)) * 10;
- double Num5 = double.Parse(data.Substring(16, 2));
- double qty = Num1 + Num2 + Num3 + Num4 + Num5;
- NowQTY.Text = qty.ToString();
- }
- private void ConnectDB()
- {
- dh = new DataHelper();
- }
- SerialPort serialPort1 = new SerialPort();
- private void StartWatch_Click(object sender, EventArgs e)
- {
- BaseUtil.SetCacheData("Type", Type.Text);
- BaseUtil.SetCacheData("ComPort", ComPort.Text);
- BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
- //设置按钮不可点击
- StartWatch.Enabled = false;
- ma_code.Enabled = false;
- StopWatch.Enabled = true;
- try
- {
- serialPort1.PortName = ComPort.Text;
- serialPort1.BaudRate = int.Parse(BaudRate.Text);
- serialPort1.Open();
- }
- catch (Exception mes)
- {
- if (BaudRate.Text == "" || BaudRate.Text == "")
- OperateResult.AppendText(">>请先维护波特率和串口\n");
- else
- OperateResult.AppendText(">>" + mes.Message + "\n");
- }
- OperateResult.AppendText("开始执行监控\n");
- Timer.Start();
- }
- private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
- {
- OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
- TxtHandleProcess(e.FullPath);
- }
- string nextLine;
- private void TxtHandleProcess(string FileName)
- {
- List<string> badcode = new List<string>();
- List<string> badlocation = new List<string>();
- List<string> badprod = new List<string>();
- }
- private void StopWatch_Click(object sender, EventArgs e)
- {
- XmlWatcher.EnableRaisingEvents = false;
- StartWatch.Enabled = true;
- ma_code.Enabled = true;
- StopWatch.Enabled = false;
- OperateResult.AppendText("停止执行监控\n");
- }
- 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 Timer_Tick(object sender, EventArgs e)
- {
- byte[] data = HexStringToBytes("01 03 00 01 00 06 94 08");
- serialPort1.Write(data, 0, data.Length);
- }
- private void AutoStart_CheckedChanged(object sender, EventArgs e)
- {
- SetAutoRun();
- }
- 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 byte[] HexStringToBytes(string hs)//十六进制字符串转byte
- {
- string a = hs.Replace(" ", "");
- int bytelength = 0;
- if (a.Length % 2 == 0)
- {
- bytelength = a.Length / 2;
- }
- else
- {
- bytelength = a.Length / 2 + 1;
- }
- byte[] b = new byte[bytelength];
- //逐个字符变为16进制字节数据
- for (int i = 0; i < bytelength; i++)
- {
- if (i == bytelength - 1)
- {
- b[i] = Convert.ToByte(a.Substring(i * 2), 16);
- }
- else
- {
- b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
- }
- }
- //按照指定编码将字节数组变为字符串
- return b;
- }
- }
- }
|