|
|
@@ -19,133 +19,153 @@ using System.IO;
|
|
|
using System.Reflection;
|
|
|
using System.Threading;
|
|
|
using UAS_MES.CustomControl;
|
|
|
+using System.IO.Ports;
|
|
|
|
|
|
namespace UAS_MES
|
|
|
{
|
|
|
public partial class Form1 : Form
|
|
|
{
|
|
|
- DataHelper dh;
|
|
|
- DataTable dt;
|
|
|
- LogStringBuilder sql = new LogStringBuilder();
|
|
|
- //经过的步骤
|
|
|
- string PastStep = "";
|
|
|
- //拆分后的经过的步骤
|
|
|
- Dictionary<int, string> Step;
|
|
|
- //屏幕高度
|
|
|
- int ScreenWidth;
|
|
|
- //屏幕宽度
|
|
|
- int ScreenHeight;
|
|
|
public Form1()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
}
|
|
|
|
|
|
- [DllImport("kernel32.dll")]
|
|
|
+ private void Form1_Load(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ Com.PortName = "COM4";
|
|
|
+ Com.ReadTimeout = 5000;
|
|
|
+ Com.WriteTimeout = 5000;
|
|
|
+ Com.BaudRate = 9600;
|
|
|
+ Com.StopBits = StopBits.One;
|
|
|
+ Com.Parity = Parity.None;
|
|
|
+ Com.Open();
|
|
|
+ }
|
|
|
|
|
|
- public static extern bool d(int freq, int duration);
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern bool InitializeWinIo();
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern void ShutdownWinIo();
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern bool SetPortVal(IntPtr wPortAddr, int dwPortVal, byte bSize);
|
|
|
+ //以下是与并口无关
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
|
|
|
+ [DllImport("WinIo64.dll")]
|
|
|
+ public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
|
|
|
+ [DllImport("user32.dll")]
|
|
|
+ public static extern int MapVirtualKey(uint Ucode, uint uMapType);
|
|
|
|
|
|
- private void button3_Click(object sender, EventArgs e)
|
|
|
+ public bool online = false; //WinIo打开标志
|
|
|
+ //数据端口共8位,控制端口共4位,可以组成1~12位的任意数字输出端口;
|
|
|
+ //状态端口共5位,控制端口共4位,可以组成1~9位的任意数字输入端口
|
|
|
+ private static IntPtr data_port = (IntPtr)0x378; //数据端口地址 D0-D7 8个端口
|
|
|
+ private static IntPtr state_port = (IntPtr)0x379; //状态端口地址 S3-S7 只能读取5位
|
|
|
+ private static IntPtr control_port = (IntPtr)0x37A; //控制端口地址 C0-C3 只能控制或输出4位
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 设定DataPort输出
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="WriteValue">设定值:范围为0~255</param>
|
|
|
+ private void setDateBuff(int WriteValue)
|
|
|
{
|
|
|
- d(800, 600);
|
|
|
+ IntPtr m_nport = data_port; //数据端口地址data_port
|
|
|
+
|
|
|
+ int m_nValue = WriteValue;
|
|
|
+
|
|
|
+ //调用WinIo库函数SetPortVal写端口值
|
|
|
+ SetPortVal(m_nport, m_nValue, 1); //write a BYTE value to an I/O port
|
|
|
}
|
|
|
|
|
|
- private void Form1_Load(object sender, EventArgs e)
|
|
|
+ /// <summary>
|
|
|
+ /// 设定ControlPort输出
|
|
|
+ /// 高4位默认设置,请不要修改,因此输出时保持高位值不变,将要输出的值从低4位输出
|
|
|
+ /// Control端口C2读写正常,C0、C1、C3写入0则输出高电平,写入1输出低电平;读取亦然
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="WriteValue">设定值</param>
|
|
|
+ private void setControlBuff(int WriteValue)
|
|
|
{
|
|
|
+ IntPtr m_nport = control_port; //获得控制端口地址
|
|
|
+ //获得控制端口的值,
|
|
|
+ int temp_dwPortVal = 0;
|
|
|
+ GetPortVal(m_nport, out temp_dwPortVal, 1); //reads a BYTE value from an I/O port
|
|
|
+
|
|
|
+ int temp_aa = temp_dwPortVal & 0xF0; //将低4位置为0;高4位不变
|
|
|
|
|
|
+ int WriValue = WriteValue & 0x0F; //取设定值低4位;高4位为0
|
|
|
+ temp_aa = temp_aa | WriteValue; //合并端口值高4位与设定值低4位
|
|
|
+ temp_aa = temp_aa ^ 0x0B; //将低4位中C0、C1、C3取反(0->1;1->0)(保证设定值与实际电平相吻合)
|
|
|
+ SetPortVal(m_nport, temp_aa, 1); //写出的值中,高4位保持端口原来的值不变,低4位实际输出
|
|
|
}
|
|
|
|
|
|
- private void button3_Click_1(object sender, EventArgs e)
|
|
|
+ /// <summary>
|
|
|
+ /// 读取ControlPort值
|
|
|
+ /// Control端口C2读取正常,C0、C1、C3高电平则读入0,低电平则读入1
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>返回C0-C3 四个端口的值:范围为0~15</returns>
|
|
|
+ private int getControlBuff()
|
|
|
{
|
|
|
- // 禁用按钮
|
|
|
- //this.button1.Enabled = false;
|
|
|
+ IntPtr m_nport = control_port; //控制端口地址data_port
|
|
|
+
|
|
|
+ int m_nValue = 0;
|
|
|
+ //调用WinIo库函数SetPortVal写端口值
|
|
|
+ GetPortVal(m_nport, out m_nValue, 1); //reads a BYTE value from an I/O port
|
|
|
|
|
|
- //// 实例化业务对象
|
|
|
- //PublicMethod.LongTimeWork workder = new PublicMethod.LongTimeWork();
|
|
|
+ int ValueGet = m_nValue ^ 0x0B; //将低4位中C0、C1、C3取反(0->1;1->0)
|
|
|
+ ValueGet = ValueGet & 0x0F; //去掉高4位值
|
|
|
+ return ValueGet;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 读取StatePort值
|
|
|
+ /// State端口S7高电平则读入0,低电平则读入1;其他正常
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>返回S3-S7 五个端口的值:范围0-31</returns>
|
|
|
+ private int getStateBuff()
|
|
|
+ {
|
|
|
+ IntPtr m_nport = state_port; //控制端口地址state_port
|
|
|
|
|
|
- //workder.ValueChanged += new PublicMethod.ValueChangedEventHandler(workder_ValueChanged);
|
|
|
+ int dwPortVal = 0;
|
|
|
+ GetPortVal(m_nport, out dwPortVal, 1); //reads a BYTE value from an I/O port
|
|
|
|
|
|
- //// 使用异步方式调用长时间的方法
|
|
|
- //Action handler = new Action(workder.LongTimeMethod);
|
|
|
- //handler.BeginInvoke(new AsyncCallback(this.AsyncCallback), handler);
|
|
|
- WebClient wc = new WebClient();
|
|
|
- wc.DownloadProgressChanged += Wc_DownloadProgressChanged;
|
|
|
- wc.DownloadFileAsync(new Uri("http://218.17.158.219:8888/UAS_WinForm.zip"), "UAS_WinForm.zip");
|
|
|
- //processBar1.Run();
|
|
|
+ int ValueGet = dwPortVal ^ 0x80; //将S7取反(保证读取与实际电平相吻合)
|
|
|
+ ValueGet = ValueGet & 0xF8; //去掉S0 ~S2位;
|
|
|
+ ValueGet = ValueGet >> 3; //右移3位,将S7~S3变为低5位
|
|
|
+ return ValueGet;
|
|
|
}
|
|
|
|
|
|
- // 结束异步操作
|
|
|
- private void AsyncCallback(IAsyncResult ar)
|
|
|
+ public bool OpenWinIo()
|
|
|
{
|
|
|
- // 标准的处理步骤
|
|
|
- Action handler = ar.AsyncState as Action;
|
|
|
- handler.EndInvoke(ar);
|
|
|
- MessageBox.Show("工作完成!");
|
|
|
- System.Windows.Forms.MethodInvoker invoker = () =>
|
|
|
+ if (InitializeWinIo())
|
|
|
{
|
|
|
- // 重新启用按钮
|
|
|
- this.button3.Enabled = true;
|
|
|
- };
|
|
|
- if (this.InvokeRequired)
|
|
|
- {
|
|
|
- this.Invoke(invoker);
|
|
|
+ this.online = true;
|
|
|
+ return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- invoker();
|
|
|
+ this.online = false;
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void Wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
|
|
|
+ public void CloseWinIo()
|
|
|
{
|
|
|
- Action act = () =>
|
|
|
- {
|
|
|
- this.progressBar1.Value = e.ProgressPercentage;
|
|
|
- //this.label1.Text = e.ProgressPercentage + "%";
|
|
|
- };
|
|
|
- if (e.ProgressPercentage == 100)
|
|
|
- {
|
|
|
- ZipHelper.UnZip(@"H:\UAS_WinForm\UAS-MES\bin\Debug\icsharpcode-SharpZipLib-4ad264b.zip", @"D:\");
|
|
|
- }
|
|
|
- this.Invoke(act);
|
|
|
+ ShutdownWinIo();
|
|
|
+ this.online = false;
|
|
|
}
|
|
|
|
|
|
- //public static void Unzip()
|
|
|
- //{
|
|
|
- // string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
|
|
|
- // string s7z = _appPath + @"\7-Zip\7z.exe";
|
|
|
- // System.Diagnostics.Process pNew = new System.Diagnostics.Process();
|
|
|
- // pNew.StartInfo.FileName = s7z;
|
|
|
- // pNew.StartInfo.Arguments = string.Format(" x \"{0}\\{1}\" -y -o\"{0}\"", _appPath, zipFileFullName);
|
|
|
- // //x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
|
|
|
- // pNew.Start();
|
|
|
- // //等待完成
|
|
|
- // pNew.WaitForExit();
|
|
|
- // //删除压缩包
|
|
|
- // File.Delete(_appPath + @"\" + zipFileFullName);
|
|
|
- //}
|
|
|
-
|
|
|
- private void button4_Click(object sender, EventArgs e)
|
|
|
+ SerialPort Com = new SerialPort();
|
|
|
+
|
|
|
+ private void button3_Click_1(object sender, EventArgs e)
|
|
|
{
|
|
|
- MessageBox.Show(textBoxWithPlaceHolder1.Text);
|
|
|
+ //byte[] byteArr = new byte[20];
|
|
|
+ //Com.Write(byteArr, 0, byteArr.Length);
|
|
|
+ //CloseWinIo();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- //public class ZipHelper
|
|
|
- //{
|
|
|
- // public static string zipFileFullName = "UAS_WinForm.zip";
|
|
|
- // public static void Unzip()
|
|
|
- // {
|
|
|
- // string _appPath = new DirectoryInfo(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).Parent.FullName;
|
|
|
- // string s7z = _appPath + @"\7z.exe";
|
|
|
- // System.Diagnostics.Process pNew = new System.Diagnostics.Process();
|
|
|
- // pNew.StartInfo.FileName = s7z;
|
|
|
- // pNew.StartInfo.Arguments = string.Format(" x \"{0}\\{1}\" -y -o\"{0}\"", _appPath, zipFileFullName);
|
|
|
- // //x "1" -y -o"2" 这段7z命令的意思: x是解压的意思 "{0}"的位置写要解压文件路径"{1}"这个1的位置写要解压的文件名 -y是覆盖的意思 -o是要解压的位置
|
|
|
- // pNew.Start();
|
|
|
- // //等待完成
|
|
|
- // pNew.WaitForExit();
|
|
|
- // //删除压缩包
|
|
|
- // //File.Delete(_appPath + @"\" + zipFileFullName);
|
|
|
- // }
|
|
|
- //}
|
|
|
}
|