using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Runtime.InteropServices; using Microsoft.Win32; using System.Threading; namespace MTP_DLL_DEMO { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } [DllImport("DLL_C302V.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DLL_LoadModel4Download")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DLL_LoadModel4Download(int PGSelect, string FullName); // public static extern bool DLL_LoadModel4Download(int PGSelect, [InAttribute()] [MarshalAs(UnmanagedType.LPTStr)] string FullName); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern byte DLL_GetPtnCount(int PGSelect, int GIndex); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DLL_ConnectClientPG(int PGSelect, string IPAddress, int nPort); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern void DLL_DisconnectClientPG(int PGSelect); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern byte DLL_TurnOff(int PGSelect); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern byte DLL_DownloadInfo(int PGSelect, int ModelZone, int TargetZone); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern byte DLL_DownloadPattern(int PGSelect, int ModelZone, int TargetZone, int PtnIndex); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern byte DLL_DownloadOST(int PGSelect); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.Cdecl)] public static extern byte DLL_GetStatus(int PGSelect); [DllImport("DLL_C302V.DLL", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern byte DLL_GetPtnInfo(int PGSelect, int GIndex, int PtnIndex, StringBuilder pPatternName, ref byte pPtnKind, ref uint pPatternTime, ref uint pCMax, ref uint pCMin); public string ErrorMessage(byte ErrorCode) { string text; switch (ErrorCode) { case 0: text = null; break; case 1: text = "Communication TimeOut"; break; case 2: case 3: case 4: text = "Wrong Date"; break; case 0x15: text = "NAK"; break; case 0xa1: text = "Port Not Connected"; break; case 0xa2: text = "Packet Length"; break; case 0xa3: text = "Compressing"; break; case 0xa4: text = "Decompressing"; break; default: text = "Unknown"; break; } return text; } private void Loadrecipe_button_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFile1 = new OpenFileDialog() { Title = "Recipe Load:", InitialDirectory = @"C:\ELP\C302V\Models", Filter = "302V文件|*.302V|所有文件|*.*" }; if (openFile1.ShowDialog()==true) { this.loadrecipe_text.Text = openFile1.SafeFileName; if (!DLL_LoadModel4Download(0, openFile1.FileName)) { MessageBox.Show("PG Recipe load error", "Error:"); } } } private void getpattern_button_Click(object sender, RoutedEventArgs e) { this.patterncoun_text.Text = DLL_GetPtnCount(0, 0).ToString(); } private void patterninfo_button_Click(object sender, RoutedEventArgs e) { StringBuilder PatternName = new StringBuilder(); byte PtnKind = 0; uint PtnTime = 0; uint PtnCMAX = 0; uint PtnCMIN = 0; byte x; try { x = DLL_GetPtnInfo(0, 0, 1, PatternName, ref PtnKind, ref PtnTime, ref PtnCMAX, ref PtnCMIN); } catch (Exception) { throw; } this.patterninfolist_text.Text = PtnCMAX.ToString(); this.patterninfolist_text.AppendText(" " + PtnCMIN.ToString()); this.patterninfolist_text.AppendText(" " + PtnTime.ToString()); this.patterninfolist_text.AppendText(" " + PtnKind.ToString()); this.patterninfolist_text.AppendText(" " + PatternName.ToString()); //StringBuilder X = null; //Byte Y=0; //uint I=0; //uint J=0; //uint K=0; //DLL_GetPtnInfo(0, 0, 0, X,ref Y, ref I, ref J,ref K); } private void Download_button_Click(object sender, RoutedEventArgs e) { DLL_ConnectClientPG(0, "192.168.0.101", 1000); byte ptnCount = DLL_GetPtnCount(0, 0); byte ret; ret = DLL_DownloadOST(0); if (ret != 0) { DLL_DisconnectClientPG(0); MessageBox.Show("Error In Downloading OST Data." + ErrorMessage(ret), "Download Error"); return; } Thread.Sleep(500); ret = DLL_DownloadInfo(0, 0, 0); if (ret != 0) { DLL_DisconnectClientPG(0); MessageBox.Show("Error In Downloading Information." + ErrorMessage(ret), "Download Error"); return; } Thread.Sleep(500); for (int i = 0; i < ptnCount; i++) { ret = DLL_DownloadPattern(0, 0, 0, i); if (ret != 0) { DLL_DisconnectClientPG(0); MessageBox.Show("Error In Downloading Pattern." + ErrorMessage(ret), "Download Error"); return; } } DLL_DisconnectClientPG(0); return; } } }