公司采購了一個夾具,項目負責人想要試探這個夾具的性能,于是想要我這邊寫一個烤機的程序,小編結合官網資料
https://wiki.amsamotion.com/?title=196&doc=222
查看其pdf說明文檔和調試工具并按照其工具寫一個烤機上位機
根據項目負責人的要求開發了一個小程序
using JY_MODBUS_IO8R_IS_Soft.BLL;
using JY_MODBUS_IO8R_IS_Soft.Business;
using JY_MODBUS_IO8R_IS_Soft.JY_MODBUS_IO8R_Controller;
using JY_MODBUS_IO8R_IS_Soft.Model;
using JY_MODBUS_IO8R_IS_Soft.userControl;
using LogManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace JY_MODBUS_IO8R_IS_Soft
{public partial class frmMain : Form{ModbusController controller = new ModbusController();static readonly Modbus_helper Modbus_helper = new Modbus_helper(DeviceBll.Single.SerialPort);public frmMain(){InitializeComponent();//獲取電腦上可用的串口號string[] ports = System.IO.Ports.SerialPort.GetPortNames();if (ports.Length != 0){cbb_Com.Items.AddRange(ports);}Modbus_helper.DelResult = DelWriteLog;Modbus_helper.DelTime = DelWriteLog;}private void btnConnect_Click(object sender, EventArgs e){try{LogNetManagment.LogNet.WriteDebug("連接串口");string errMsg = string.Empty;if (!Modbus_helper.Connect(cbb_Com.Text.Trim(), Convert.ToInt32(cbb_ComRate.Text.Trim()), Convert.ToInt32(txtWriteReadTime.Text.Trim()), ref errMsg)){MessageBox.Show(errMsg);}else{UpdateConnectStatus(true);}}catch (Exception ex){LogNetManagment.LogNet.WriteException("連接通信", ex);MessageBox.Show(ex.ToString());}}/// <summary>/// 改變連接狀態/// </summary>/// <param name="status"></param>public void UpdateConnectStatus(bool status){if (status){btnConnect.Enabled = false;btnBreak.Enabled = true;}else{btnConnect.Enabled = true;btnBreak.Enabled = false;}}private void btnBreak_Click(object sender, EventArgs e){try{LogNetManagment.LogNet.WriteDebug("斷開串口");string errMsg = string.Empty;if (!Modbus_helper.Close(ref errMsg)){MessageBox.Show(errMsg);}else{UpdateConnectStatus(false);}}catch (Exception ex){LogNetManagment.LogNet.WriteException("斷開通訊", ex);MessageBox.Show(ex.ToString());}}private void btnTest_Click(object sender, EventArgs e){try{LogNetManagment.LogNet.WriteDebug("開始循環測試,循環次數為"+txtTestTime.ToString());this.btnTest.Enabled = false;int channel = cmbChannel.SelectedIndex;Task.Factory.StartNew(() =>{string errMsg = string.Empty;Modbus_helper.IsTestStatus = true;if (!Modbus_helper.ContinueTest(channel, Convert.ToInt32(txtTestTime.Text),ref errMsg)){this.Invoke(new Action(() =>{MessageBox.Show(errMsg);}));}}).ContinueWith((task) => {this.Invoke(new Action(() => {this.btnTest.Enabled = true;}));});}catch (Exception ex){LogNetManagment.LogNet.WriteException("開始循環測試", ex);MessageBox.Show(ex.ToString());}}private void DelWriteLog(int runtime){this.BeginInvoke(new Action(() =>{DateTime date = DateTime.Now;txtLog.AppendText($"{date.ToString("yyyy-MM-dd HH:mm:ss.fff")} 當前正在執行{runtime}次"+ Environment.NewLine);}));}private void DelWriteLog(ResultModel result){this.BeginInvoke(new Action(() =>{DateTime date = DateTime.Now;if (result.TestResult == (uint)Common.CommonEnum.TestResultType.Success){txtLog.AppendText(date.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + result.LabelTip + Environment.NewLine);}else if (result.TestResult == (uint)Common.CommonEnum.TestResultType.Fail){txtLog.AppendText(date.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + result.LabelTip + Environment.NewLine);}else{txtLog.AppendText(date.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + result.LabelTip + Environment.NewLine);}}));}private void switchButtons1_Btn_Click(object sender, EventArgs e){Button userCtl = sender as Button;string errMsg = string.Empty;if (switchButtons1.Controls.Find($"btn{userCtl.Tag}", true)[0].BackColor == SystemColors.Control){if (!Modbus_helper.TestByChannel(Convert.ToInt32(userCtl.Tag), Common.CommonEnum.OrderType.Control, ref errMsg)){switchButtons1.Controls.Find($"btn{userCtl.Tag}", true)[0].BackColor = SystemColors.Control;}else{switchButtons1.Controls.Find($"btn{userCtl.Tag}", true)[0].BackColor = Color.Red;}}else if (switchButtons1.Controls.Find($"btn{userCtl.Tag}", true)[0].BackColor == Color.Red){if (!Modbus_helper.TestByChannel(Convert.ToInt32(userCtl.Tag), Common.CommonEnum.OrderType.Clearance, ref errMsg)){switchButtons1.Controls.Find($"btn{userCtl.Tag}", true)[0].BackColor = Color.Red;}else{switchButtons1.Controls.Find($"btn{userCtl.Tag}", true)[0].BackColor = SystemColors.Control;}}}private void btnStop_Click(object sender, EventArgs e){LogNetManagment.LogNet.WriteDebug("點擊停止");Modbus_helper.IsTestStatus = false;DateTime date = DateTime.Now;txtLog.AppendText(date.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 點擊停止" + Environment.NewLine);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace JY_MODBUS_IO8R_IS_Soft.Common
{public class CommonEnum{/// <summary>/// 指令類型/// </summary>public enum OrderType : uint{/// <summary>/// 控制/// </summary>Control = 0,/// <summary>/// 解控/// </summary>Clearance = 1}/// <summary>/// 測試結束各種原因/// </summary>public enum TestResultType : uint{/// <summary>/// 正常成功結束/// </summary>Success = 0,/// <summary>/// 異常結束/// </summary>Fail = 1,/// <summary>/// 倒計時結束/// </summary>FixedSuccess = 2}}
}
using Common;
using JY_MODBUS_IO8R_IS_Soft.Config;
using LogManager;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace JY_MODBUS_IO8R_IS_Soft.Business
{public class DeviceBll{private static DeviceBll bDeviceSingle = null;public static DeviceBll Single{get{if (bDeviceSingle == null) bDeviceSingle = new DeviceBll();return bDeviceSingle;}}//不允許外部New操作private DeviceBll() { }/// <summary>/// 硬件配置文件路徑/// </summary>public string ChoosesetPath = AppDomain.CurrentDomain.BaseDirectory + "\\Chooseset.ini";/// <summary>/// 保存數據的路徑/// </summary>public string SaveDataPath = string.Empty;/// <summary>/// 是否保存數據/// </summary>public bool IsSaveData = true;/// <summary>/// 保存數據最大的行數 到達之后重新創建文件/// </summary>public int ExportDataLines = 0;/// <summary>/// 通道數/// </summary>public int ChannelNum = 12;/// <summary>/// 設備通訊連接狀態 true已連接 false未連接/// </summary>public bool IsLink = false;/// <summary>/// 鎖對象/// </summary>public readonly object LockFileObj = new object();/// <summary>/// 配置文件/// </summary>public mainSet MainConfigSet = new mainSet();///// <summary>///// 設備接口對象///// </summary>//public I_MUX _ifun;public delegate void DelUpdateConfig();/// <summary>/// 用戶切換響應事件/// </summary>public static event DelUpdateConfig EventUpdateConfig;/// <summary>/// 串口對象/// </summary>public SerialPort SerialPort;/// <summary>/// 用戶切換/// </summary>public static void UpdateConfig(){if (EventUpdateConfig != null){EventUpdateConfig();}}public delegate void DelUpdateBtnStartAll();public static event DelUpdateBtnStartAll EventUpdateBtnStartAll;public static void UpdateBtnStartAll(){if (EventUpdateBtnStartAll != null){EventUpdateBtnStartAll();}}/// <summary>///系統硬件初始化/// </summary>public void Init(){try{DeviceInit();int delayTime = Convert.ToInt32(IniFileHelper.IniReadValue(DeviceBll.Single.ChoosesetPath, "DelayTime", "DelayTime_ms", ""));ChannelNum = Convert.ToInt32(IniFileHelper.IniReadValue(DeviceBll.Single.ChoosesetPath, "Channel", "ChannelNum", ""));LogManager.LogNetManagment.LogNet.WriteInfo("獲取參數保護文件參數");}catch (Exception ex){LogNetManagment.LogNet.WriteException("Init", ex);}}/// <summary>/// 設備初始化加載/// </summary>public void DeviceInit(){SerialPort = new SerialPort();//串口對象}}
}
using JY_MODBUS_IO8R_IS_Soft.Model;
using LogManager;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace JY_MODBUS_IO8R_IS_Soft.BLL
{public class Modbus_helper{/// <summary>/// 串口對象/// </summary>public SerialPort SerialPort { get; set; }/// <summary>/// 發送結果數據/// </summary>public Action<ResultModel> DelResult { get; set; }/// <summary>/// 發送執行的次數/// </summary>public Action<int> DelTime { get; set; }/// <summary>/// 獲取結果類/// </summary>public ResultModel Result { get; set; }/// <summary>/// 測試狀態/// </summary>public bool IsTestStatus { get; set; }public int WriteReadTime { get; set; }/// <summary>/// 鎖對象/// </summary>private readonly object _lockWr = new object();public Modbus_helper(SerialPort serialPort){this.SerialPort = serialPort;this.Result = new ResultModel();IsTestStatus = false;}/// <summary>/// 連接串口/// </summary>/// <param name="comName">串口號</param>/// <param name="comRate">串口速率</param>/// <param name="errMsg">錯誤信息</param>/// <returns></returns>public bool Connect(string comName, int comRate, int time, ref string errMsg){try{SerialPort.BaudRate = comRate;SerialPort.StopBits = StopBits.One;SerialPort.PortName = comName;SerialPort.NewLine = "\r\n";SerialPort.ReadTimeout = 10000;SerialPort.WriteTimeout = 1000;SerialPort.Open();if (SerialPort.IsOpen.Equals(false)){//串口連接失敗!errMsg = "連接串口失敗";return false;}else{WriteReadTime = time;return true;}}catch (Exception ex){LogNetManagment.LogNet.WriteException(nameof(Modbus_helper) + "_" + nameof(Connect), ex);errMsg = ex.ToString();return false;}}public bool Close(ref string errMsg){try{if (SerialPort.IsOpen){SerialPort.Close();}if (!SerialPort.IsOpen){return true;}return true;}catch (Exception ex){LogNetManagment.LogNet.WriteException(nameof(Modbus_helper) + "_" + nameof(Close), ex);errMsg = ex.ToString();return false;}}public bool ContinueTest(int channel, int time, ref string errMsg){try{for (int i=0;i<time;i++){if (!IsTestStatus){return true;}int testtime = i + 1;DelTime(testtime);byte[] controlBytes = GetControlBytes(channel, Common.CommonEnum.OrderType.Control);//控制if (!WriteRead(controlBytes, ref errMsg)){Result.TestResult = 1;Result.LabelTip = "當前正在執行" + testtime + "次吸合失敗";DelResult(Result);return false;}byte[] clearanceBytes = GetControlBytes(channel, Common.CommonEnum.OrderType.Clearance);//解控if (!WriteRead(clearanceBytes, ref errMsg)){Result.TestResult = 1;Result.LabelTip = "當前正在執行" + testtime + "次斷開失敗";DelResult(Result);return false;}Result.TestResult = 0;Result.LabelTip = "執行成功";DelResult(Result);}Result.LabelTip = "執行完畢";Result.TestResult = 2;DelResult(Result);return true;}catch (Exception ex){LogNetManagment.LogNet.WriteException(nameof(Modbus_helper) + "_" + nameof(ContinueTest), ex);errMsg = ex.ToString();return false;}}public bool TestByChannel(int channel, Common.CommonEnum.OrderType orderType, ref string errMsg){try{if (orderType == Common.CommonEnum.OrderType.Control){byte[] controlBytes = GetControlBytes(channel, Common.CommonEnum.OrderType.Control);//控制if (!WriteRead(controlBytes, ref errMsg)){Result.TestResult = 1;Result.LabelTip = $"當前正在執行channel{channel}吸合失敗";DelResult(Result);return false;}else{Result.TestResult = 0;Result.LabelTip = $"當前正在執行channel{channel}吸合成功";DelResult(Result);return true;}}else if (orderType == Common.CommonEnum.OrderType.Clearance){byte[] clearanceBytes = GetControlBytes(channel, Common.CommonEnum.OrderType.Clearance);//解控if (!WriteRead(clearanceBytes, ref errMsg)){Result.TestResult = 1;Result.LabelTip = $"當前正在執行channel{channel}斷開失敗";DelResult(Result);return false;}else{Result.TestResult = 0;Result.LabelTip = $"當前正在執行channel{channel}斷開成功";DelResult(Result);return true;}}return true;}catch (Exception ex){LogNetManagment.LogNet.WriteException(nameof(Modbus_helper) + "_" + nameof(TestByChannel), ex);errMsg = ex.ToString();return false;}}public bool WriteRead(byte[] strWrite, ref string errMsg){try{SerialPort.DiscardInBuffer();//丟棄緩沖區數據SerialPort.DiscardOutBuffer();//丟棄緩沖區數據SerialPort.DiscardInBuffer();//丟棄緩沖區數據SerialPort.DiscardOutBuffer();//丟棄緩沖區數據SerialPort.Write(strWrite, 0, 8);Thread.Sleep(WriteReadTime);SerialPort.ReadExisting();return true;}catch (Exception ex){LogNetManagment.LogNet.WriteException(nameof(Modbus_helper) + "_" + nameof(WriteRead), ex);errMsg = ex.ToString();return false;}}public byte[] GetControlBytes(int channel, Common.CommonEnum.OrderType OrderType){byte[] bytes = new byte[8];//01 05 00 00 FF 00 8C 3A 控制if (OrderType == Common.CommonEnum.OrderType.Control){//bytes[0] = 01;//bytes[1] = 05;//bytes[2] = 00;//bytes[3] = 00;//bytes[4] = 0xFF;//bytes[5] = 00;//bytes[6] = 0x8C;//bytes[7] = 0x3A;//return bytes;byte address = Convert.ToByte(channel);byte[] cmd = GenerateCommand(0x01, address, true);return cmd;}else if (OrderType == Common.CommonEnum.OrderType.Clearance){//01 05 00 00 00 00 CD CA//bytes[0] = 01;//bytes[1] = 05;//bytes[2] = 00;//bytes[3] = 00;//bytes[4] = 00;//bytes[5] = 00;//bytes[6] = 0xCD;//bytes[7] = 0xCA;//return bytes;byte address = Convert.ToByte(channel);byte[] cmd = GenerateOffCommand(0x01, address);return cmd;}return bytes;}// CRC16校驗計算核心方法public static ushort CalculateCrc(byte[] data){ushort crc = 0xFFFF;foreach (byte b in data){crc ^= b;for (int i = 0; i < 8; i++){bool lsb = (crc & 1) == 1;crc >>= 1;if (lsb) crc ^= 0xA001;}}return crc;}// 生成完整Modbus指令(包含CRC)public static byte[] GenerateCommand(byte deviceId, ushort address, bool state){byte[] cmd = new byte[6];cmd[0] = deviceId;cmd[1] = 0x05; // 功能碼cmd[2] = (byte)(address >> 8); // 地址高字節cmd[3] = (byte)(address & 0xFF); // 地址低字節cmd[4] = state ? (byte)0xFF : (byte)0x00; // 狀態值cmd[5] = 0x00; // 固定填充ushort crc = CalculateCrc(cmd);byte[] fullCmd = new byte[8];Array.Copy(cmd, fullCmd, 6);fullCmd[6] = (byte)(crc & 0xFF); // CRC低字節在前fullCmd[7] = (byte)(crc >> 8);return fullCmd;}// 生成斷開繼電器指令public static byte[] GenerateOffCommand(byte deviceId, ushort address){byte[] cmd = new byte[6];cmd[0] = deviceId;cmd[1] = 0x05; // 功能碼cmd[2] = (byte)(address >> 8); // 地址高字節cmd[3] = (byte)(address & 0xFF); // 地址低字節cmd[4] = 0x00; // 斷開繼電器cmd[5] = 0x00; // 固定填充ushort crc = CalculateCrc(cmd);byte[] fullCmd = new byte[8];Array.Copy(cmd, fullCmd, 6);fullCmd[6] = (byte)(crc & 0xFF); // CRC 低字節在前fullCmd[7] = (byte)(crc >> 8);return fullCmd;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace JY_MODBUS_IO8R_IS_Soft.Config
{// 通過此類可以處理設置類的特定事件: // 在更改某個設置的值之前將引發 SettingChanging 事件。// 在更改某個設置的值之后將引發 PropertyChanged 事件。// 在加載設置值之后將引發 SettingsLoaded 事件。// 在保存設置值之前將引發 SettingsSaving 事件。public sealed partial class mainSet{public mainSet(){// // 若要為保存和更改設置添加事件處理程序,請取消注釋下列行: //// this.SettingChanging += this.SettingChangingEventHandler;//// this.SettingsSaving += this.SettingsSavingEventHandler;//}private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e){// 在此處添加用于處理 SettingChangingEvent 事件的代碼。}private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e){// 在此處添加用于處理 SettingsSaving 事件的代碼。}}
}
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成。
// 運行時版本:4.0.30319.42000
//
// 對此文件的更改可能會導致不正確的行為,并且如果
// 重新生成代碼,這些更改將會丟失。
// </auto-generated>
//------------------------------------------------------------------------------namespace JY_MODBUS_IO8R_IS_Soft.Config {[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()][global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]public sealed partial class mainSet : global::System.Configuration.ApplicationSettingsBase {private static mainSet defaultInstance = ((mainSet)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new mainSet())));public static mainSet Default {get {return defaultInstance;}}[global::System.Configuration.UserScopedSettingAttribute()][global::System.Diagnostics.DebuggerNonUserCodeAttribute()][global::System.Configuration.DefaultSettingValueAttribute("COM1")]public string ComName {get {return ((string)(this["ComName"]));}set {this["ComName"] = value;}}[global::System.Configuration.UserScopedSettingAttribute()][global::System.Diagnostics.DebuggerNonUserCodeAttribute()][global::System.Configuration.DefaultSettingValueAttribute("115200")]public string ComRate {get {return ((string)(this["ComRate"]));}set {this["ComRate"] = value;}}[global::System.Configuration.UserScopedSettingAttribute()][global::System.Diagnostics.DebuggerNonUserCodeAttribute()][global::System.Configuration.DefaultSettingValueAttribute("Q.0")]public string Channel {get {return ((string)(this["Channel"]));}set {this["Channel"] = value;}}[global::System.Configuration.UserScopedSettingAttribute()][global::System.Diagnostics.DebuggerNonUserCodeAttribute()][global::System.Configuration.DefaultSettingValueAttribute("10")]public int TestTime {get {return ((int)(this["TestTime"]));}set {this["TestTime"] = value;}}}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace JY_MODBUS_IO8R_IS_Soft.userControl
{public partial class SwitchButtons : UserControl{public SwitchButtons(){InitializeComponent();InitializeTableLayout();}private void InitializeTableLayout(){// 設置TableLayoutPanel的行和列tableLayoutPanel1.RowCount = 2; // 例如,3行tableLayoutPanel1.ColumnCount = 9; // 1列,可以根據需要調整tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F)); // 標題行占20%高度tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F)); // 按鈕行占60%高度}private void SwitchButtons_Load(object sender, EventArgs e){Label labelcol = new Label();labelcol.Text = $"Do0"; // 按鈕的文本從"按鈕2"開始計數labelcol.AutoSize = false;labelcol.TextAlign = ContentAlignment.MiddleCenter;labelcol.Dock = DockStyle.Fill; // 使按鈕填充整個單元格tableLayoutPanel1.Controls.Add(labelcol, 0, 1); // 添加到相應的行,第一列for (int i = 1; i < 9; i++) // 假設你想添加4個按鈕,從第二行開始(索引為1){this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 60F));Label label = new Label();label.Text = $"Q.{i-1}"; // 按鈕的文本從"按鈕2"開始計數label.AutoSize = false;label.TextAlign = ContentAlignment.MiddleCenter;label.Dock = DockStyle.Fill; // 使按鈕填充整個單元格Button button = new Button();button.Dock = DockStyle.Fill;button.Tag = (i - 1).ToString();button.Name = $"btn{(i-1)}";button.Click += new EventHandler(this.btn_click);tableLayoutPanel1.Controls.Add(label, i, 0); // 添加到相應的行,第一列tableLayoutPanel1.Controls.Add(button, i, 1); // 添加到相應的行,第一列}}//public void btn_click(Object sender, System.EventArgs e)//{// string id = ((Button)sender).Tag.ToString();// MessageBox.Show(id);//}/// <summary>/// 點擊事件/// </summary>[Browsable(true), Category("自定義事件"), Description("點擊事件")]public event EventHandler Btn_Click;private void btn_click(object sender, EventArgs e){if (Btn_Click != null){Btn_Click(sender, e);}}}
}
using JY_MODBUS_IO8R_IS_Soft.Business;
using JY_MODBUS_IO8R_IS_Soft.frms;
using LogManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;namespace JY_MODBUS_IO8R_IS_Soft
{static class Program{/// <summary>/// 應用程序的主入口點。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);#region 日志構建//初始化日志string logPath = Application.StartupPath + "\\Log"; //日志路徑LogManager.GenerateMode LogSaveMode = LogManager.GenerateMode.ByEveryDay; //日志存儲方式 按天LogNetManagment.LogNet = new LogManager.LogNetDateTime(logPath, LogSaveMode);LogNetManagment.LogNet.WriteDebug("啟動程序");#endregionDeviceBll.Single.Init();Application.Run(new frmMain());}}
}