赞
踩
Modbus RTU 读写器介绍:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.41.55aa5b43wXJPTM&id=41938964666https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.41.55aa5b43wXJPTM&id=41938964666
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- using System.Runtime.InteropServices; //调用动态库一定要加入这个引用
-
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- int SendCode; //发送指令代码
- bool connwaitnow = false;
- string dispstr = "";
-
-
- [DllImport("crcbyhoho.dll", EntryPoint = "CRC_16", CallingConvention = CallingConvention.StdCall)]
- static extern long CRC_16(byte[] DataBuf, byte DataLen); MODBUS CRC计算
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private static byte fangma(byte indata) //求反码函数
- {
- string binstr=Convert.ToString(indata, 2).PadLeft(8,'0') ; //前面补0共8位
- byte retudata= 0x00;
- for (int i = 0; i<=7 ;i++ )
- {
- if (binstr.Substring(7 - i, 1) == Convert .ToString ('0'))
- {
- retudata =Convert .ToByte ( retudata + Math.Pow (2,i));
- }
-
- }
- return retudata;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- short i;
- if (axMSComm1.PortOpen)
- {
- axMSComm1.PortOpen = false;//关闭串口
- }
-
- //搜索一遍串口
- comboBox1.Items.Clear();
-
- for (i = 1; i <= 20; i++)
- {
- try
- {
- axMSComm1.CommPort = i;
- axMSComm1.PortOpen = true;
- if (axMSComm1.PortOpen)
- {
- axMSComm1.PortOpen = false;
- comboBox1.Items.Add("COM" + Convert.ToString(i));
- }
- }
- catch (Exception ex)
- {
-
- }
- }
-
- if (comboBox1.Items.Count > 0)
- {//有有效端口
- comboBox1.SelectedIndex = comboBox1.Items.Count-1;
- comboBox2.SelectedIndex = 2;
- comboBox3.SelectedIndex = 0;
- }
- comboBox4.SelectedIndex = 0;
- comboBox5.SelectedIndex = 2;
- comboBox6.SelectedIndex = 0;
- comboBox7.SelectedIndex = 0;
- comboBox8.SelectedIndex = 0;
- comboBox9.SelectedIndex = 0;
- comboBox10.SelectedIndex = 0;
- comboBox11.SelectedIndex = 2;
- comboBox12.SelectedIndex = 0;
- comboBox13.SelectedIndex = 1;
- comboBox14.SelectedIndex = 0;
- comboBox15.SelectedIndex = 7;
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- string comstr;
- if (button1.Text == "打开串口")
- {
- if (comboBox1.Items.Count < 1)
- {//无有效端口
- MessageBox.Show("没用任何串口可供使用!","提示");
- return;
- }
-
-
- if (axMSComm1.PortOpen == false)
- {
- comstr = "";
- switch (comboBox2.SelectedIndex)
- {
- case 0:
- comstr = "4800,";
- break;
- case 1:
- comstr = "9600,";
- break;
- case 2:
- comstr = "19200,";
- break;
- default :
- comstr = "38400,";
- break;
- }
- switch (comboBox3.SelectedIndex)
- {
- case 0:
- comstr = comstr+ "N,8,1";
- break;
- case 1:
- comstr = comstr + "O,8,1";
- break;
- case 2:
- comstr = comstr + "E,8,1";
- break;
- case 3:
- comstr = comstr + "M,8,1";
- break;
- default:
- comstr = comstr + "S,8,1";
- break;
- }
-
- axMSComm1.InBufferSize = 1024;
- axMSComm1.OutBufferSize = 1024;
- axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;//两进制模式
- axMSComm1.InputLen = 0;
- axMSComm1.InBufferCount = 0;//清除接收缓冲
- axMSComm1.OutBufferCount = 0;//清除发送缓冲
- axMSComm1.SThreshold = 1; // '0:一旦发送数据就触发OnComm()事件来检测是否返回数据
- axMSComm1.RThreshold = 1; //接收到1个字节数据就立即触发OnComm()事件
- axMSComm1.Settings = comstr;
- axMSComm1.CommPort = Convert.ToInt16(comboBox1.Text.Substring(3, comboBox1.Text.Length - 3));//设置串口号,要根据自己的电脑设为相应的
-
- try
- {
- timer1.Enabled = false;
- //checkBox2 .Checked =false ;
- axMSComm1.PortOpen = true;//打开串口
- button1.Text = "关闭串口";
- comboBox1.Enabled = false;
- }
- catch (Exception ex)
- {
- //异常处理:打开端口失败
- MessageBox.Show("打开串口失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- else
- {
- if (connwaitnow == true)
- {
- MessageBox.Show("正在轮询站号,请先停止轮询再关闭串口!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- axMSComm1.PortOpen = false;//关闭串口
- button1.Text = "打开串口";
- comboBox1.Enabled = true;
- }
- }
-
- private void axMSComm1_OnComm(object sender, EventArgs e)
- {
- short i;
- uint cardhao;
- string cardstr;
- string keystr;
- string BufStr;
- byte[] SendBuf=new byte[8];
- byte[] ReceiveBuff = new byte[1024]; //串口接收数据缓冲
- short inbuffercountbak; //接收数据长度
- long timerbak; //用于计算数据包是否已经接收完毕
- long timerbak1;
-
- if (axMSComm1.CommEvent == 2)
- {//收到数据 //先不再产生 OnComm 事件
- //axMSComm1.RThreshold = 0; //设置/返回要接收的字符数
- timerbak = DateTime.Now.Ticks;//单位为100纳秒,除10000后得出毫秒
- timerbak1 = timerbak;
- inbuffercountbak = 0;
- while ((timerbak1 - timerbak) < (delaytime1.Value * 10000))//等待通讯超时长后再无数据表示这是一个数据包
- {
- if (inbuffercountbak != axMSComm1.InBufferCount)
- {
- inbuffercountbak = axMSComm1.InBufferCount;
- timerbak = DateTime.Now.Ticks; //重新等待
- }
- timerbak1 = DateTime.Now.Ticks;
- }
- inbuffercountbak = axMSComm1.InBufferCount;
- ReceiveBuff = (byte[])(axMSComm1.Input);//接收到的内容放在接收缓冲
- axMSComm1.InBufferCount = 0;
-
-
- string ReceiveStr = "串口接收数据:"; //将接收到的数据显示在列表框
- for (i = 0; i <= inbuffercountbak-1; i++)
- {
- ReceiveStr = ReceiveStr + ReceiveBuff[i].ToString("X2")+" ";
- }
- listBox1.Items.Add(ReceiveStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
-
- switch (SendCode)
- {
- case 1:
- if (inbuffercountbak < 4) { return; }
- else {textBox1 .Text =Convert.ToString (ReceiveBuff[4]); }
- break ;
- case 2:
- if (inbuffercountbak > 4)
- {
- DialogResult result = MessageBox.Show("在线设备的站号已修改,是否要再次读出设备站号?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (result ==DialogResult.OK )
- {
- button2_Click_1(button2, EventArgs.Empty);
- }
- }
- break;
- case 9:
- if (inbuffercountbak > 4)
- {
- DialogResult result = MessageBox.Show("读卡器已执行读卡指令,是否要读出保存在寄存器内的读卡数据?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (result == DialogResult.OK)
- {
- button11_Click(button11, EventArgs.Empty);
- }
- }
- break;
- case 10:
- if (inbuffercountbak > 4)
- {
- BufStr = "";
- for (i = 3; i < ReceiveBuff[2]+3;i++ )
- {
- BufStr = BufStr + ReceiveBuff[i].ToString("X2") + " ";
- }
- WinfBox.Text = BufStr;
-
- if (BEGEE.Value==7 && EENum.Value >2)
- {
- if (ReceiveBuff[4] > 0)
- {
- cardhao = (uint)(ReceiveBuff[5] * 256 * 256 * 256 + ReceiveBuff[6] * 256 * 256 + ReceiveBuff[7] * 256 + ReceiveBuff[8]);
- BufStr = "状态位:" + ReceiveBuff[4].ToString("X2") + " 物理卡号:" + ReceiveBuff[5].ToString("X2") + ReceiveBuff[6].ToString("X2") + ReceiveBuff[7].ToString("X2") + ReceiveBuff[8].ToString("X2");
- BufStr = BufStr + " 换成十位卡号:" + Convert.ToString(cardhao).PadLeft(10, '0') + " 扇区内数据:";
- string aaa = Convert.ToString(ReceiveBuff[4], 2).PadLeft(8,'0');
- if (aaa.Substring(aaa.Length-2,1 ) == "1")
- {
- for(i=9;i<=inbuffercountbak-3;i++)
- {
- BufStr = BufStr + ReceiveBuff[i].ToString("X2");
- }
- textBox3.Text = BufStr;
- }
- }
- }
- }
- break;
- case 12:
- if (inbuffercountbak > 4)
- {
- SendBuf[0] = Convert.ToByte(textBox1.Text); //站点号
- SendBuf[1] = 0x06; //功能码
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x07;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x14;
- long CRC16 = CRC_16(SendBuf, 6); //CRC
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 13;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for ( i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
- break;
- case 13:
- if (inbuffercountbak > 4)
- {
- SendBuf[0] = Convert.ToByte(textBox1.Text); //站点号
- SendBuf[1] = 0x03;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x07;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x01;
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 14;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
- break;
- case 14:
- if (inbuffercountbak > 4)
- {
- string aaa = Convert.ToString(ReceiveBuff[4], 2);
- if (aaa.Substring (aaa.Length -1)=="1")
- {
- button5_Click_1(button5, EventArgs.Empty);
- MessageBox.Show("写卡操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
- }
- }
- break;
- case 4:
- if (inbuffercountbak < 8) { return; }
- if (ReceiveBuff[0] == 0x69 && (ReceiveBuff[1] == 0xD2 || ReceiveBuff[1] == 0x3C) && ((byte)(ReceiveBuff[2] ^ ReceiveBuff[3] ^ ReceiveBuff[4] ^ ReceiveBuff[5] ^ ReceiveBuff[6]) == ReceiveBuff[7]))
- {
- cardhao = (uint)(ReceiveBuff[3] * 256 * 256 * 256 + ReceiveBuff[4] * 256 * 256 + ReceiveBuff[5] * 256 + ReceiveBuff[6]);
- cardstr=ReceiveBuff[3].ToString("X2")+"-"+ReceiveBuff[4].ToString("X2")+"-"+ReceiveBuff[5].ToString("X2")+"-"+ReceiveBuff[6].ToString("X2");
- textBox3.Text = "物理卡号:" + cardstr + " 换算成十位卡号:" + Convert.ToString(cardhao).PadLeft(10,'0');
- }
- break;
- case 5:
- if (inbuffercountbak < 2) { return; }
- if (ReceiveBuff[0] == 0x69 && inbuffercountbak > 2)
- {
- keystr = "";
- for(i=1;i<inbuffercountbak-1;i++)
- {
- keystr = keystr + (char)(ReceiveBuff[i]);
- }
- textBox3.Text = "设备按键:" + keystr;
- }
- //else if (ReceiveBuff[0] == 0x69 && ReceiveBuff[2] == 0x0D)
- //{
- // textBox3.Text = "设备单键:" +(char)(ReceiveBuff[1]);
- //}
- break;
- case 6:
- if (inbuffercountbak < 8)
- {
- if (connwaitnow == true) { timer1.Enabled = true; }
- return;
- }
- if( ReceiveBuff[0] == 0x69 && ReceiveBuff[1] == 0x69)
- {
- if (ReceiveBuff[2] == 0x01 && ((byte)(ReceiveBuff[3] ^ ReceiveBuff[4] ^ ReceiveBuff[5] ^ ReceiveBuff[6] ^ ReceiveBuff[7]) == ReceiveBuff[8])) //'只有卡号
- {
- cardhao = (uint)(ReceiveBuff[4] * 256 * 256 * 256 + ReceiveBuff[5] * 256 * 256 + ReceiveBuff[6] * 256 + ReceiveBuff[7]);
- cardstr = ReceiveBuff[4].ToString("X2") + "-" + ReceiveBuff[5].ToString("X2") + "-" + ReceiveBuff[6].ToString("X2") + "-" + ReceiveBuff[7].ToString("X2");
- // textBox4.Text = "物理卡号:" + cardstr + " 换算成十位卡号:" + Convert.ToString(cardhao).PadLeft(10, '0');
- dispstr = "卡号:" + Convert.ToString(cardhao).PadLeft(10, '0');
- }
- else if (ReceiveBuff[2] == 0x02 && inbuffercountbak > 8) //有*+数字按键
- {
- keystr = "";
- for (i = 3; i < inbuffercountbak - 3; i++)
- {
- keystr = keystr + (char)(ReceiveBuff[i]);
- }
- //textBox4.Text = "设备按键:" + keystr;
- dispstr = "按键:" + Convert.ToString(keystr).PadRight (10, ' ');
- }
- else if (ReceiveBuff[2] == 0x03 && ((byte)(ReceiveBuff[3] ^ ReceiveBuff[4] ^ ReceiveBuff[5] ^ ReceiveBuff[6] ^ ReceiveBuff[7]) == ReceiveBuff[8])) //卡号和*+数字按键
- {
- cardhao = (uint)(ReceiveBuff[4] * 256 * 256 * 256 + ReceiveBuff[5] * 256 * 256 + ReceiveBuff[6] * 256 + ReceiveBuff[7]);
- cardstr = ReceiveBuff[4].ToString("X2") + "-" + ReceiveBuff[5].ToString("X2") + "-" + ReceiveBuff[6].ToString("X2") + "-" + ReceiveBuff[7].ToString("X2");
-
- keystr = "";
- for (i = 9; i < inbuffercountbak - 3; i++)
- {
- keystr = keystr + (char)(ReceiveBuff[i]);
- }
-
- //textBox4.Text = "物理卡号:" + cardstr + " 换算成十位卡号:" + Convert.ToString(cardhao).PadLeft(10, '0') + " 设备按键:" + keystr;
- dispstr = "卡号:" + Convert.ToString(cardhao).PadLeft(10, '0') + "按键:" + Convert.ToString(keystr).PadRight(10, ' ');
- }
- else if((byte)(ReceiveBuff[5] ^ ReceiveBuff[6]) == ReceiveBuff[7])
- {
- keystr = ""+(char)(ReceiveBuff[3]);
- // textBox4.Text = "设备单键:" + keystr;
- dispstr = "单键:" + Convert.ToString(keystr).PadRight (10, ' ');
- }
- if (checkBox1.Checked == true)
- {
- //button12_Click(button12, EventArgs.Empty);
- }
- }
- if (connwaitnow == true) { timer1.Enabled = true; }
- break;
- case 21:
- {
- if (inbuffercountbak < 4) { return; }
- else { textBox2.Text = Convert.ToString(ReceiveBuff[4]); }
- }
- break;
- case 22:
- if (inbuffercountbak > 4)
- {
- DialogResult result = MessageBox.Show("在线设备的站号已修改,是否要再次读出设备站号?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (result == DialogResult.OK)
- {
- button4_Click(button4, EventArgs.Empty);
- }
- }
- break;
- case 29:
- if (inbuffercountbak > 4)
- {
- string parastr = ReceiveBuff[4].ToString("X2");
- comboBox15.SelectedIndex = Convert.ToInt16(parastr.Substring(0,1));
- int paraH = Convert.ToInt16(parastr.Substring(parastr.Length -1));
- comboBox13.SelectedIndex = paraH % 2;
- comboBox14.SelectedIndex = paraH / 2;
- }
- break;
- case 210:
- if (inbuffercountbak > 6)
- {
- if (ReceiveBuff[1] == 3 && (ReceiveBuff[3] + ReceiveBuff[4] + ReceiveBuff[5] + ReceiveBuff[6]>0))
- {
- cardhao = (uint)(ReceiveBuff[3] * 256 * 256 * 256 + ReceiveBuff[4] * 256 * 256 + ReceiveBuff[5] * 256 + ReceiveBuff[6]);
- cardstr = ReceiveBuff[3].ToString("X2") + "-" + ReceiveBuff[4].ToString("X2") + "-" + ReceiveBuff[5].ToString("X2") + "-" + ReceiveBuff[6].ToString("X2");
- textBox4.Text = "物理卡号:" + cardstr + " 换算成十位卡号:" + Convert.ToString(cardhao).PadLeft(10, '0');
-
- if (checkBox2.Checked )
- {
- button25_Click(button25, EventArgs.Empty);
- }
- }
- else { textBox4.Text = ""; }
-
- }
- break;
- default :
- break ;
-
- }
-
- }
- }
-
- private void button6_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear () ;
- }
-
-
-
- private void checkBox2_MouseDown(object sender, MouseEventArgs e)
- {
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (textBox1 .Text=="0")
- {
- MessageBox.Show("请先选择要轮询的站号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (connwaitnow == false)
- {
- connwaitnow = true;
- timer1.Interval = 50;
- timer1.Enabled = true;
- }
- else
- {
- connwaitnow = false;
- timer1.Enabled =false ;
- }
-
- }
-
- private void timer1_Tick(object sender, EventArgs e)
- {
- //button11_Click(button11, EventArgs.Empty);
- }
-
- private void checkBox2_CheckedChanged(object sender, EventArgs e)
- {
-
- }
-
- private void delaytime1_ValueChanged(object sender, EventArgs e)
- {
- timer2.Interval = (int)delaytime1.Value+10;
- }
-
- private void timer2_Tick(object sender, EventArgs e)
- {
- timer2.Enabled = false;
- if (connwaitnow == true) { timer1.Enabled = true; }
- }
-
- private void timer3_Tick(object sender, EventArgs e)
- {
- label16.Text = DateTime.Now.ToString();
- }
-
-
- private void button18_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[5];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- int jihao = Convert.ToInt32(textBox1.Text);
-
- SendBuf[0] = 0xAA;
- SendBuf[1] = 0xAA;
- SendBuf[2] = (byte)(jihao % 256);
- SendBuf[3] = (byte)(jihao / 256);
- SendBuf[4] = 0x3C;
-
- textBox3.Text = "";
- SendCode = 4;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 4; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button2_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行读取在线设备的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- SendBuf[0] = 0x00;
- SendBuf[1] = 0x03;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x00;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x01;
- SendBuf[6] = 0x85;
- SendBuf[7] = 0xDB;
-
- SendCode = 1;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button5_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x02;
- SendBuf[4] = 0x00;
- SendBuf[5] = (byte)(comboBox4.SelectedIndex + 1);
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 4;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button3_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行更改在线设备站号的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte oldjihao = Convert.ToByte(textBox1.Text);
- byte newjihao = Convert.ToByte(newjihao1.Value);
- SendBuf[0] = oldjihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x00;
- SendBuf[4] = 0x00;
- SendBuf[5] = newjihao;
-
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
-
- SendCode = 2;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button8_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[13];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x10;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x3E;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x02;
- SendBuf[6] = 0x04;
- SendBuf[7] = 0x00;
- SendBuf[8] = (byte)(comboBox6.SelectedIndex);
- SendBuf[9] = 0x55;
- SendBuf[10] = 0xAA;
-
- long CRC16 = CRC_16(SendBuf, 11);
-
- SendBuf[11] = (byte)(CRC16 % 256);
- SendBuf[12] = (byte)((CRC16 / 256) % 256);
-
-
- SendCode = 3;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 12; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button17_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[13];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
- long Btl = Convert.ToInt16(comboBox5.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x10;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x3C;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x02;
- SendBuf[6] = 0x04;
- SendBuf[7] = (byte)((Btl / 256) % 256);
- SendBuf[8] = (byte)(Btl % 256);
- SendBuf[9] = 0x55;
- SendBuf[10] = 0xAA;
-
- long CRC16 = CRC_16(SendBuf, 11);
-
- SendBuf[11] = (byte)(CRC16 % 256);
- SendBuf[12] = (byte)((CRC16 / 256) % 256);
-
-
- SendCode = 5;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 12; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button13_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
- long Deepdelay = (int)Deepdelay4.Value;
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x46;
- SendBuf[4] = (byte)((Deepdelay / 256) % 256);
- SendBuf[5] = (byte)(Deepdelay % 256);
-
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode =7;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button14_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x46;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x00;
-
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 8;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button10_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x01;
- //if (checkBox1.Checked)
- //{
- SendBuf[4] = 0x80;
- SendBuf[5] = (byte)(comboBox7.SelectedIndex + 2 * comboBox8.SelectedIndex);
- //}
- //else
- //{
- // SendBuf[4] = 0x00;
- // SendBuf[5] = 0x00;
- //}
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 6;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button9_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x07;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x08;
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 9;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button11_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if(BEGEE.Value!=7)
- {
- DialogResult result = MessageBox.Show("从第7寄存器开始读才能返回读写状态,是否要从第7寄存器开始读?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (result == DialogResult.OK)
- {
- BEGEE.Value = 7;
- }
- }
-
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x03;
- SendBuf[2] = 0x00;
- SendBuf[3] = (byte)(BEGEE.Value) ;
- SendBuf[4] = 0x00;
- SendBuf[5] = (byte)(EENum.Value);
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 10;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button12_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[100];
- int i;
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (BEGEE.Value != 10)
- {
- DialogResult result = MessageBox.Show("写卡数据是存在第10寄存器开始的连续寄存器内,是否要将数据写入10以后的寄存器内?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
- if (result == DialogResult.OK)
- {
- BEGEE.Value = 10;
- }
- }
-
- byte jihao = Convert.ToByte(textBox1.Text);
- int Wlen = Convert.ToInt16(EENum.Value)*2;
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x10;
- SendBuf[2] = 0x00;
- SendBuf[3] = (byte)(BEGEE.Value);
- SendBuf[4] = 0x00;
- SendBuf[5] = (byte)(EENum.Value);
- SendBuf[6] = (byte)Wlen;
-
- string wstr = WinfBox.Text.Trim() ; //确保写缓冲数据
- for ( i=1;i<=Wlen;i++)
- {
- wstr = wstr + " 00";
- }
- wstr = wstr.Trim();
-
- for ( i = 0; i < Wlen; i++)
- {
- SendBuf[7 + i] = Convert.ToByte(Convert.ToInt32(wstr.Substring(i * 3, 2), 16));
- }
-
-
- long CRC16 = CRC_16(SendBuf, (byte)(Wlen+7));
- SendBuf[Wlen+7] = (byte)(CRC16 % 256);
- SendBuf[Wlen+8] = (byte)((CRC16 / 256) % 256);
-
- byte[] senddata = new byte[Wlen+9];
- for (i=0;i<=Wlen+8;i++)
- {
- senddata[i] = SendBuf[i];
- }
- SendCode = 11;
- axMSComm1.Output = senddata; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for ( i = 0; i <= Wlen+8; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button18_Click_1(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox1.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x07;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x04;
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 12;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void axIcCardReader1_Enter(object sender, EventArgs e)
- {
-
- }
-
- private void button4_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行读取在线设备的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- SendBuf[0] = 0x00;
- SendBuf[1] = 0x03;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x02;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x01;
- SendBuf[6] = 0x24;
- SendBuf[7] = 0x1B;
-
- SendCode = 21;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button7_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行更改在线设备站号的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte oldjihao = Convert.ToByte(textBox2.Text);
- byte newjihao = Convert.ToByte(newjihao2.Value);
- SendBuf[0] = oldjihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x02;
- SendBuf[4] = 0x00;
- SendBuf[5] = newjihao;
-
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
-
- SendCode = 22;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button15_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x04;
- SendBuf[4] = 0x00;
- SendBuf[5] = (byte)(comboBox9.SelectedIndex + 1);
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 24;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button16_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
- long Opensj = (int)opendelay.Value; //延时
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = (byte)(0x46 + comboBox10.SelectedIndex);
- SendBuf[4] = (byte)((Opensj / 256) % 256);
- SendBuf[5] = (byte)(Opensj % 256);
-
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 26;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button19_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x46;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x00;
-
- long CRC16 = CRC_16(SendBuf, 6);
-
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 27;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button20_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[13];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
- long Btl = Convert.ToInt16(comboBox11.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x10;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x3C;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x02;
- SendBuf[6] = 0x04;
- SendBuf[7] = (byte)((Btl / 256) % 256);
- SendBuf[8] = (byte)(Btl % 256);
- SendBuf[9] = 0x55;
- SendBuf[10] = 0xAA;
-
- long CRC16 = CRC_16(SendBuf, 11);
-
- SendBuf[11] = (byte)(CRC16 % 256);
- SendBuf[12] = (byte)((CRC16 / 256) % 256);
-
-
- SendCode = 23;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 12; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button21_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[13];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x10;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x3E;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x02;
- SendBuf[6] = 0x04;
- SendBuf[7] = 0x00;
- SendBuf[8] = (byte)(comboBox12.SelectedIndex);
- SendBuf[9] = 0x55;
- SendBuf[10] = 0xAA;
-
- long CRC16 = CRC_16(SendBuf, 11);
-
- SendBuf[11] = (byte)(CRC16 % 256);
- SendBuf[12] = (byte)((CRC16 / 256) % 256);
-
-
- SendCode = 25;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 12; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
-
- }
-
- private void button22_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x06;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x03;
- SendBuf[4] = 0x80;
-
- int para = comboBox13.SelectedIndex + 2 * comboBox14.SelectedIndex;
- string parahex = para.ToString("X");
- parahex = comboBox15.SelectedIndex.ToString("D") + parahex;
- SendBuf[5] = Convert.ToByte(Convert.ToInt32(parahex.Substring(0, 2), 16));
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 28;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button23_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x03;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x03;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x01;
-
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 29;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button24_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[8];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x03;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x00;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x02;
-
-
- long CRC16 = CRC_16(SendBuf, 6);
- SendBuf[6] = (byte)(CRC16 % 256);
- SendBuf[7] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 210;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 7; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void button25_Click(object sender, EventArgs e)
- {
- byte[] SendBuf = new byte[13];
-
- if (button1.Text == "打开串口")
- {
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- byte jihao = Convert.ToByte(textBox2.Text);
-
- SendBuf[0] = jihao;
- SendBuf[1] = 0x10;
- SendBuf[2] = 0x00;
- SendBuf[3] = 0x00;
- SendBuf[4] = 0x00;
- SendBuf[5] = 0x02;
- SendBuf[6] = 0x04;
- SendBuf[7] = 0x00;
- SendBuf[8] = 0x00;
- SendBuf[9] = 0x00;
- SendBuf[10] = 0x00;
-
- long CRC16 = CRC_16(SendBuf, 11);
- SendBuf[11] = (byte)(CRC16 % 256);
- SendBuf[12] = (byte)((CRC16 / 256) % 256);
-
- SendCode = 211;
- axMSComm1.Output = SendBuf; //向串口发送数据
- axMSComm1.RThreshold = 1;
-
- string SendStr = "串口发送数据:"; //将发送的数据显示在列表框
- for (int i = 0; i <= 12; i++)
- {
- SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
- }
-
- listBox1.Items.Add(SendStr);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- }
-
- private void checkBox3_CheckedChanged(object sender, EventArgs e)
- {
-
- }
-
- private void checkBox3_MouseUp(object sender, MouseEventArgs e)
- {
- if (checkBox3.Checked)
- {
- if (button1.Text == "打开串口")
- {
- connwaitnow = false;
- timer4.Enabled = false;
- checkBox3.Checked = false;
- MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (Convert.ToByte(textBox2.Text) == 0)
- {
- connwaitnow = false;
- timer4.Enabled = false;
- checkBox3.Checked = false;
- MessageBox.Show("请输入轮询的站点号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- connwaitnow = true;
- timer4.Enabled = true;
- }
- else
- {
- connwaitnow = false;
- timer4.Enabled = false;
- }
- }
-
- private void timer4_Tick(object sender, EventArgs e)
- {
- button24_Click(button24, EventArgs.Empty);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。