using MvCamCtrl.NET; using Newtonsoft.Json; using ProductionControl.Device; using ProductionControl.UI; using ProductionControl.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace ProductionControl { public partial class Form2 : Form { enum Motor { X = 'X', Y = 'Y' }; [DllImport("PomeasDualDll-Z.dll", EntryPoint= "?open@CDComm@@QAEHH@Z")] private static extern bool open(int port); [DllImport("PomeasDualDll-Z.dll")] private static extern void close(); [DllImport("PomeasDualDll-Z.dll")] private static extern void MoveHome(Motor m); //运动到指定位置 * [DllImport("PomeasDualDll-Z.dll")] private static extern void MoveGoto(Motor m,long dest); //获取总长度 [DllImport("PomeasDualDll-Z.dll")] private static extern long MoveMaxLength(Motor m); //获取当前位置 [DllImport("PomeasDualDll-Z.dll")] private static extern long MovePos(Motor m); [DllImport("PMSOpticalDll.dll", CharSet = CharSet.Ansi, EntryPoint = "PMSOptical_OpenComm", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr PMSOptical_OpenComm(string comname, int nBaud); SSerialPort sSerialPort;// = new SSerialPort("COM4", 2400,1); HeightDev dev = new HeightDev(); public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { return; string Scanner_GENTL_CTI = "ScannerRuntime\\Win64_x64\\MvFGProducerCXP(MV-GX1004).cti"; MyCamera.MV_GENTL_IF_INFO_LIST m_stIFInfoList = new MyCamera.MV_GENTL_IF_INFO_LIST(); int nRet = MyCamera.MV_CC_EnumInterfacesByGenTL_NET(ref m_stIFInfoList, Scanner_GENTL_CTI); if (0 != nRet) { return; } //this.uiScannerDev1.init(Device.ScannerDev.ScannerType.GENTL); //uiScannerDev1.log += new Action((level, info) => //{ //}); //dev.start("192.168.3.35", 64000); //var cc = PMSOptical_OpenComm("COM2", 9600); return; //byte[] getMaxPos = new byte[] { 0x58, 0x4D, 0x0D }; //sSerialPort.send(getMaxPos, 0, getMaxPos.Length); //Thread.Sleep(20); //byte[] recv = sSerialPort.getMessage(1000);//9 byte //if (recv == null) recv = new byte[0]; //this.label1.Text = $"{recv.Length} | "; //if (recv.Length > 0) //{ // byte[] maxPos = subBuff(recv, 2, 5);//5 byte // for(int i = 0;i 0) // this.Text += recv[0].ToString("X"); } private void button4_Click(object sender, EventArgs e) { //byte[] goHome = new byte[] { 0x58, 0x48, 0x0D }; //sSerialPort.send(goHome, 0, goHome.Length); //var buff = sSerialPort.getMessage(-1); //if (buff == null) // return; //if (buff.Length % 11 != 0) // throw new Exception("数据错误!"); //if (buff.Length > 11) // buff = subBuff(buff, buff.Length - 11, 11); //if (buff[0] != 0x10) // throw new Exception("数据0错误!"); //int add = 0; //for (int i = 1; i < 10; i++) // add += (int)buff[i]; //if (add != buff[10]) // throw new Exception("数据ADD错误!"); //string unit = buff[3] == 0x10 ? "N/cm" : "Kgf/cm"; //string data = ""; //int zf = 1; //for (int i = 4; i <= 8; i++) //{ // if (buff[i] == 0x0a) continue; // if (buff[i] == 0x0b) zf = -1; // if (buff[i] <0x0a) // data += (int)buff[i]; //} //double lfNum = Convert.ToSingle(data) * zf; //if (buff[9] > 0) lfNum /= Math.Pow(10, buff[9]); //this.Text = lfNum + " " + unit; } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { dev.stop(); //sSerialPort.close(); } private void button5_Click(object sender, EventArgs e) { byte[] getPos = new byte[] { 0x58, 0x4E, 0x0D }; sSerialPort.send(getPos, 0, getPos.Length); Thread.Sleep(20); byte[] recv = sSerialPort.getMessage(1000);//9 byte if (recv == null) recv = new byte[0]; this.label2.Text = $"{recv.Length} | "; if (recv.Length > 0) { byte[] maxPos = subBuff(recv, 2, 5);//5 byte for (int i = 0; i < maxPos.Length; i++) this.label2.Text += maxPos[i].ToString("X") + " "; } } private byte[] subBuff(byte[] buff,int start,int length) { byte[] res=new byte[length]; for(int i=start;i