CopterSoftware.rar
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using DevExpress.Skins;using DevExpress.LookAndFeel;using DevExpress.UserSkins;using DevExpress.XtraEditors;using DevExpress.XtraBars.Helpers;namespace WindowsApplication1{ public partial class MainForm : XtraForm { #region 软件初始化 public MainForm() { InitializeComponent(); InitSkinGallery(); InitForm(); } void InitSkinGallery() { SkinHelper.InitSkinGallery(rgbiSkins, true); } #endregion #region 全局变量 const int DIO_VAL = 0x0000; //测量数据 const int PWM_BASE = 0x0014; //PWM基础值 const int PWM_RANGE = 0x0018; //PWM可控制范围 const int PWM_OFFSET = 0x001A; //PWM补偿值 const int PID_VAL = 0x001E; //PID参数 const int FOCTORY = 0x0100; //工厂模式 int FacilityNode = 100; //产品节点 int timeCountMs5 = 0; int timeCountMs50 = 0; #endregion #region 属性 string portName { get { return editName.EditValue.ToString(); } } int portBaud { get { return Convert.ToInt32(editBaud.EditValue); } } #endregion #region Form函数 private void InitForm() { MeasuringUpdataDisplay(); //数据刷新 ConfigUpdataDisplay(); Modbus.MBConfig(serialPortX, 100, 38400); Modbus.MBAddRepeatCmd(Modbus.D_DIO, Modbus.MB_READ_INPUT_REG); } /// <summary> /// 成功接收到 写寄存器的指令后的操作 /// </summary> private void receiveWriterValueSuccess() { } /// <summary> /// 成功接收到 读寄存器的指令后操作 /// </summary> private void receiveReadValueSuccess() { ConfigUpdataDisplay(); } int connectIndex = 1; /// <summary> /// 成功接收到只读寄存器指令后操作 /// </summary> private void receiveReadDioValueSuccess() { MeasuringUpdataDisplay(); connectIndex ; if (connectIndex > 3) connectIndex = 1; stateIndicatorComponent1.StateIndex = connectIndex; } /// <summary> /// 打开串口 /// </summary> private void SerialOpen() { if (!serialPortX.IsOpen) { serialPortX.PortName = portName; serialPortX.BaudRate = portBaud; serialPortX.Open(); } } /// <summary> /// 关闭串口 /// </summary> private void SerialClose() { if (serialPortX.IsOpen) { serialPortX.Close(); } } /// <summary> /// 始能传感器连接 /// </summary> private void EnableConnect() { try { ButtonConnect.Caption = "打开连接"; ButtonConnect.LargeImageIndex = 10; SerialOpen(); kernelTimer.Enabled = true; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 断开传感器连接 /// </summary> private void DisableConnect() { try { ButtonConnect.Caption = "断开连接"; ButtonConnect.LargeImageIndex = 9; SerialClose(); kernelTimer.Enabled = false; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// 读回的参数更新到显示 /// </summary> private void ConfigUpdataDisplay() { textBase1.EditValue = Modbus.gBaseF1; textBase2.EditValue = Modbus.gBaseF2; textBase3.EditValue = Modbus.gBaseF3; textBase4.EditValue = Modbus.gBaseF4; textMax.EditValue = Modbus.gRangeMax; textMin.EditValue = Modbus.gRangeMin; textOffset1.EditValue = Modbus.gPwmF1; textOffset2.EditValue = Modbus.gPwmF2; textOffset3.EditValue = Modbus.gPwmF3; textOffset4.EditValue = Modbus.gPwmF4; textP.EditValue = Modbus.gP; textI.EditValue = Modbus.gI; textD.EditValue = Modbus.gD; } /// <summary> /// 显示的参数 保存到寄存器中 /// </summary> private void DisplayUpdataConfig() { Modbus.gBaseF1 = Convert.ToInt16(textBase1.EditValue); Modbus.gBaseF2 =Convert.ToInt16(textBase2.EditValue); Modbus.gBaseF3 =Convert.ToInt16(textBase3.EditValue); Modbus.gBaseF4 =Convert.ToInt16(textBase4.EditValue); Modbus.gRangeMax = Convert.ToInt16(textMax.Text); Modbus.gRangeMin = Convert.ToInt16(textMin.Text); Modbus.gPwmF1 = Convert.ToInt16(textOffset1.Text); Modbus.gPwmF2 = Convert.ToInt16(textOffset2.Text); Modbus.gPwmF3 = Convert.ToInt16(textOffset3.Text); Modbus.gPwmF4 = Convert.ToInt16(textOffset4.Text); Modbus.gP = Convert.ToSingle(textP.Text); Modbus.gI = Convert.ToSingle(textI.Text); Modbus.gD = Convert.ToSingle(textD.Text); } /// <summary> /// 测量数据更新到显示 /// </summary> private void MeasuringUpdataDisplay() { barX.EditValue = Modbus.gDioX; barY.EditValue = Modbus.gDioY; barZ.EditValue = Modbus.gDioZ; barD.EditValue = Modbus.gDioD; barX45.EditValue = Modbus.gDioXx; barY45.EditValue = Modbus.gDioXy; barScale.EditValue = Modbus.gBaseF1; } /// <summary> /// 刷新窗口显示的数据 /// </summary> private void RefurbishDisplay() { MeasuringUpdataDisplay(); } #endregion #region 串口部分 private void serialPortX_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { Modbus.MBDataReceive(); } #endregion #region 系统时间片 private void kernelTimer_Tick(object sender, EventArgs e) { timeCountMs5 ; if (timeCountMs5 >= 10) { timeCountMs5 = 0; timeCountMs50 ; //Modbus.MBSchedRefresh(); } switch (Modbus.MBRefresh()) //读回保持寄存器 或离散线圈 进行参数刷新 { case Modbus.MB_READ_DISCRETE: case Modbus.MB_READ_HOLD_REG: ConfigUpdataDisplay(); break; case Modbus.MB_READ_COILS: case Modbus.MB_READ_INPUT_REG: receiveReadDioValueSuccess(); break; case Modbus.MB_WRITE_SINGLE_COIL: case Modbus.MB_WRITE_SINGLE_REG: case Modbus.MB_WRITE_MULTIPLE_COILS: case Modbus.MB_WRITE_MULTIPLE_REGS:break; default: break; } if (timeCountMs50 >= 4) //200ms { timeCountMs50 = 0; RefurbishDisplay(); } } #endregion #region 主体按键操作 private void ButtonConnect_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { if (kernelTimer.Enabled == false) { EnableConnect(); } else { DisableConnect(); } } catch (Exception ex) { throw new Exception(ex.Message); } } private void buttonDownLoad_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { DisplayUpdataConfig(); Modbus.SciSchedulingLock(); Modbus.MBClearCmd(); Modbus.MBAddCmd(Modbus.D_BASE, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.MBAddCmd(Modbus.D_PWM, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.MBAddCmd(Modbus.D_RANGE, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.MBAddCmd(Modbus.D_PID, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.SciSchedulingUnlock(); } private void buttonUpload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { Modbus.SciSchedulingLock(); Modbus.MBClearCmd(); Modbus.MBAddCmd(Modbus.D_BASE, Modbus.MB_READ_HOLD_REG); Modbus.MBAddCmd(Modbus.D_PWM, Modbus.MB_READ_HOLD_REG); Modbus.MBAddCmd(Modbus.D_RANGE, Modbus.MB_READ_HOLD_REG); Modbus.MBAddCmd(Modbus.D_PID, Modbus.MB_READ_HOLD_REG); Modbus.SciSchedulingUnlock(); } private void buttonReset_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { } private void buttonUp_Click(object sender, EventArgs e) { if (Modbus.gBaseF1 < 1000) Modbus.gBaseF1 = 10; else Modbus.gBaseF1 = 1000; Modbus.gBaseF4 = Modbus.gBaseF3 = Modbus.gBaseF2 = Modbus.gBaseF1; Modbus.SciSchedulingLock(); Modbus.MBClearCmd(); Modbus.MBAddCmd(Modbus.D_BASE, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.SciSchedulingUnlock(); buttonUp.Focus(); } private void buttonDown_Click(object sender, EventArgs e) { if (Modbus.gBaseF1 > 10) Modbus.gBaseF1 -= 10; else Modbus.gBaseF1 = 0; Modbus.gBaseF4 = Modbus.gBaseF3 = Modbus.gBaseF2 = Modbus.gBaseF1; Modbus.SciSchedulingLock(); Modbus.MBClearCmd(); Modbus.MBAddCmd(Modbus.D_BASE, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.SciSchedulingUnlock(); buttonDown.Focus(); } private void buttonStop_Click(object sender, EventArgs e) { Modbus.gBaseF4 = Modbus.gBaseF3 = Modbus.gBaseF2 = Modbus.gBaseF1 = 0; Modbus.SciSchedulingLock(); Modbus.MBClearCmd(); Modbus.MBAddCmd(Modbus.D_BASE, Modbus.MB_WRITE_MULTIPLE_REGS); Modbus.SciSchedulingUnlock(); buttonStop.Focus(); } private void MainForm_KeyDown(object sender, KeyEventArgs e) { EventArgs ev = new EventArgs(); switch (e.KeyCode) { case Keys.A: buttonUp_Click(sender, ev); break; case Keys.S: buttonStop_Click(sender, ev); break; case Keys.D: buttonDown_Click(sender, ev); break; default: break; } } private void MainForm_MouseClick(object sender, MouseEventArgs e) { gaugeControl1.Focus(); } #endregion }}
评论