找传奇、传世资源到传世资源站!

运动控制IO显示(C#源码)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈


from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Drawing.Imaging;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Runtime.InteropServices;using csLTDMC;using System.IO;namespace IO监控{ public partial class Form1 : Form { private string IniFilePath; private bool datechange = false; private bool aaaa = false; [DllImport("kernel32.dll")] private static extern long WritePrivateProfileString(string section, string key, string value, string filepath); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder returnvalue, int buffersize, string filepath); private void GetValue(string section, string key, out string value) { StringBuilder stringBuilder = new StringBuilder(); GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath); value = stringBuilder.ToString(); } private ushort _CardID = 0; private ushort _CANStatus = 0; private ushort _CANNum = 0; public Form1() { InitializeComponent(); //DateInit(date_IOIN, 70); //DateInit(date_IOOUT, 70); aaaa = true; } /// <summary> /// 表格控件初始化后 /// </summary> /// <param name="date">表格控件的名称</param> /// <param name="num">表格控件初始化行数</param> public void DateInit(DataGridView date, int num) { int Num = num; date.RowTemplate.Height = 34; //设置行高 date.Rows.Clear(); //清除掉控件表格里面的数据和行数 for (int i = 0; i < Num; i ) { string number = Convert.ToString(i); date.Rows.Add(number); } date.AllowUserToAddRows = false;//最后一行隐藏 } private void button1_Click(object sender, EventArgs e) { DateIoSave("SectionIN", date_IOIN, 70); DateIoSave("SectionOUT", date_IOOUT, 70); } private void DateIoSave(string section, DataGridView date, int num) { IniFilePath = Application.StartupPath "\\DateIoSet.ini"; string Section = section; int Num = num; try { for (int i = 0; i < Num; i ) { //string numbera = "ROW[2]" i; //string numberb = "ROW[3]" i; string numbera = "DateIoSet" "2" i; string numberb = "DateIoSet" "3" i; WritePrivateProfileString(Section, numbera, Convert.ToString(date.Rows[i].Cells[2].Value), IniFilePath); WritePrivateProfileString(Section, numberb, Convert.ToString(date.Rows[i].Cells[3].Value), IniFilePath); } } catch (Exception ee) { MessageBox.Show(ee.Message); MessageBox.Show("1"); } } private void DateIOINShow(string section, DataGridView date) { IniFilePath = Application.StartupPath "\\DateIoSet.ini"; string Section = section; string outString; try { for (int i = 0; i < 70; i ) { string numbera = "ROW[2]" i; string numberb = "ROW[3]" i; GetValue(Section, numbera, out outString); date.Rows[i].Cells[2].Value = outString; GetValue(Section, numberb, out outString); date.Rows[i].Cells[3].Value = outString; } } catch (Exception ee) { MessageBox.Show(ee.Message); MessageBox.Show("2"); } } private void DateIOOUTShow(string section, DataGridView date) { IniFilePath = Application.StartupPath "\\DateIoSet.ini"; string Section = section; string outString; try { for (int i = 0; i < 70; i ) { string numbera = "ROW[3]" i; string numberb = "ROW[4]" i; GetValue(Section, numbera, out outString); date.Rows[i].Cells[3].Value = outString; GetValue(Section, numberb, out outString); date.Rows[i].Cells[4].Value = outString; } } catch (Exception ee) { MessageBox.Show(ee.Message); MessageBox.Show("3"); } } private void Form1_Load(object sender, EventArgs e) { DateIOINShow("SectionIN", date_IOIN); DateIOOUTShow("SectionOUT", date_IOOUT); date_IOIN.ReadOnly = true; date_IOOUT.ReadOnly = true; date_IOOUT.Columns["OutImage"].ReadOnly = true; btn_datechange.Text = "数据修改关闭"; short num = LTDMC.dmc_board_init();//获取卡数量 if (num <= 0 || num > 8) { MessageBox.Show("初始卡失败!", "出错"); } ushort _num = 0; ushort[] cardids = new ushort[8]; uint[] cardtypes = new uint[8]; short res = LTDMC.dmc_get_CardInfList(ref _num, cardtypes, cardids); LTDMC.dmc_read_inbit(0, 1); if (res != 0) { MessageBox.Show("获取卡信息失败!"); } _CardID = cardids[0]; DateIOState(date_IOIN); timer1.Enabled = true; } private void btn_datechange_Click(object sender, EventArgs e) { if (datechange == true) { date_IOIN.ReadOnly = false; date_IOOUT.ReadOnly = false; datechange = false; btn_datechange.Text = "数据修改打开"; } else { date_IOIN.ReadOnly = true; date_IOOUT.ReadOnly = true; datechange = true; btn_datechange.Text = "数据修改关闭"; } } private void DateIOState(DataGridView date) { //DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); //btn.Name = "btnModify"; //btn.HeaderText = "保存"; //btn.DefaultCellStyle.NullValue = "保存"; //date.Columns.Add(btn); } private void DateIIIIIII() { while (aaaa) if (radioButton1.Checked == true) { DateInit(date_IOIN, 16); DateInit(date_IOOUT, 16); aaaa = false; } else if (radioButton2.Checked == true) { DateInit(date_IOIN, 32); DateInit(date_IOOUT, 32); aaaa = false; } else if (radioButton3.Checked == true) { DateInit(date_IOIN, 20); DateInit(date_IOOUT, 20); aaaa = false; } else if (radioButton4.Checked == true) { DateInit(date_IOIN, 50); DateInit(date_IOOUT, 50); aaaa = false; } else if (radioButton5.Checked == true) { DateInit(date_IOIN, 10); DateInit(date_IOOUT, 10); aaaa = false; } else if (radioButton6.Checked == true) { DateInit(date_IOIN, 16); DateInit(date_IOOUT, 16); aaaa = false; } else { aaaa = false; } } private void date_IOIN_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (date_IOIN.Columns[e.ColumnIndex].Name == "btnModify") { MessageBox.Show("成功!"); } //date_IOIN.Rows[e.RowIndex].Cells[1].Value = 10; //date_IOIN.Rows[e.RowIndex].Cells[1].Style.BackColor = Color.Red; } private void IOshow() { if (radioButton1.Checked == true) { DateCardIN(0, 16, date_IOIN); } else if (radioButton2.Checked == true) { DateCardIN(1, 32, date_IOIN); } for (ushort i = 0; i < 10; i ) { if (LTDMC.dmc_read_outbit(0, i) == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date_IOOUT["OutImage", i].Value = new Bitmap(IniFilePath); date_IOOUT.Rows[i].Cells[1].Value = "关闭"; } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date_IOOUT["OutImage", i].Value = new Bitmap(IniFilePath); date_IOOUT.Rows[i].Cells[1].Value = "打开"; } } } private void timer1_Tick(object sender, EventArgs e) { DateIIIIIII(); IOshow(); } private bool b = false; private void date_IOOUT_CellContentClick(object sender, DataGridViewCellEventArgs e) { int Rindex = e.RowIndex; if (Rindex > -1) { if (b == false) { IOoutSET(Rindex, 0); date_IOOUT.Rows[e.RowIndex].Cells[1].Value = "关闭"; b = true; } else { IOoutSET(Rindex, 1); date_IOOUT.Rows[e.RowIndex].Cells[1].Value = "打开"; b = false; } } //int CIndex = e.ColumnIndex; //int Rindex = e.RowIndex; //switch(Rindex) //{ // case 0: // LTDMC.dmc_write_outbit(0, 0, 0); // break; // case 1: // LTDMC.dmc_write_outbit(0, 1, 0); // break; // case 2: // LTDMC.dmc_write_outbit(0, 2, 0); // break; //} } private void IOoutSET(int num, ushort state) { ushort mynum = Convert.ToUInt16(num); LTDMC.dmc_write_outbit(0, mynum, state); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { aaaa = true; } private void radioButton2_CheckedChanged(object sender, EventArgs e) { aaaa = true; } #region /// <summary> /// IO输入IN界面显示 /// </summary> /// <param name="cardNum">控制卡卡号</param> /// <param name="InNum">输入IO数量</param> /// <param name="date">显示数据控件名称</param> private void DateCardIN(ushort cardNum, ushort InNum, DataGridView date) { for (ushort i = 0; i < InNum; i ) { if (LTDMC.dmc_read_inbit(cardNum, i) == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } } } /// <summary> /// O输入OUT界面显示 /// </summary> /// <param name="cardNum">控制卡号</param> /// <param name="OutNum">输出的OUT数量</param> /// <param name="date">显示数据控件名称</param> private void DateCardOUT(ushort cardNum, ushort OutNum, DataGridView date) { for (ushort i = 0; i < OutNum; i ) { if (LTDMC.dmc_read_outbit(cardNum, i) == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } } } /// <summary> /// CANopen模块的IN /// </summary> /// <param name="cardNum">控制卡号</param> /// <param name="NodeID">模块的节点号</param> /// <param name="InNum">模块的输入IN数</param> /// <param name="date">显示的数据名称</param> private void DateCANIN(ushort cardNum, ushort NodeID, ushort InNum, DataGridView date) { for (ushort i = 0; i < InNum; i ) { ushort CANIN = 0; LTDMC.nmc_read_inbit(cardNum, NodeID, i, ref CANIN); if (CANIN == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } } } /// <summary> /// CANopen模块OUT的显示 /// </summary> /// <param name="cardNum">控制卡号</param> /// <param name="NodeID">模块的节点号</param> /// <param name="InNum">模块自带IO数</param> /// <param name="date">IO显示数据名称</param> private void DateCANOUT(ushort cardNum, ushort NodeID, ushort InNum, DataGridView date) { for (ushort i = 0; i < InNum; i ) { ushort CANIN = 0; LTDMC.nmc_read_outbit(cardNum, NodeID, i, ref CANIN); if (CANIN == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } } } /// <summary> /// 总线IO模块 /// </summary> /// <param name="cardNum">控制卡号</param> /// <param name="startNum">开始IO数</param> /// <param name="InNum">IO模块的IN数</param> /// <param name="date">IO显示数据名称</param> private void DateCATIN(ushort cardNum, ushort startNum, ushort InNum, DataGridView date) { for (ushort i = startNum; i < InNum startNum; i ) { if (LTDMC.dmc_read_inbit(cardNum, i) == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } } } /// <summary> /// 总线IO模块 /// </summary> /// <param name="cardNum">控制卡号</param> /// <param name="startNum">开始IO数</param> /// <param name="OUTNum">IO模块的out数</param> /// <param name="date">IO显示数据名称</param> private void DateCATOUT(ushort cardNum, ushort startNum, ushort OUTNum, DataGridView date) { for (ushort i = startNum; i < OUTNum startNum; i ) { if (LTDMC.dmc_read_outbit(cardNum, i) == 0) { string IniFilePath = Application.StartupPath "//LampON.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } else { string IniFilePath = Application.StartupPath "//LampOFF.ico"; date["Image", i].Value = new Bitmap(IniFilePath); } } } #endregion private void radioButton3_CheckedChanged(object sender, EventArgs e) { aaaa = true; } private void radioButton4_CheckedChanged(object sender, EventArgs e) { aaaa = true; } private void radioButton5_CheckedChanged(object sender, EventArgs e) { aaaa = true; } private void radioButton6_CheckedChanged(object sender, EventArgs e) { aaaa = true; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复