C# 232串口转485网络读卡器源码

 本示例使用设备的淘宝链接:https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.38.26435b43iDUZjG&id=41895642706https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.38.26435b43iDUZjG&id=41895642706


 

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int SendCode;  //发送指令代码
        bool connwaitnow = false;
        string dispstr = "";
        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;
            }
        }



        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;
                    axMSComm1.RThreshold = 1;//按下位机返回数据字节长设置,设置接收字节长度
                    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 button2_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[5];            
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行读取在线设备的操作!","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = 0x00;
            SendBuf[3] = 0x00;
            SendBuf[4] = 0xA5;

            SendCode = 1;
            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 axMSComm1_OnComm(object sender, EventArgs e)
        {
            short i;
            uint cardhao;
            string cardstr;
            string keystr;
            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; }
                        if (ReceiveBuff[4] == (ReceiveBuff[2] ^ ReceiveBuff[3]))
                         {
                            textBox1 .Text =Convert.ToString (ReceiveBuff[3] * 256 + ReceiveBuff[2]) ;
                         }
                        break ;
                    case 2:
                        if (inbuffercountbak < 2) { return; }
                        if(ReceiveBuff[0] == 0x69 && ReceiveBuff[1] == 0xF0)
                        {
                            DialogResult result = MessageBox.Show("在线设备的站号已修改,是否要再次读出设备站号?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                            if (result ==DialogResult.OK )
                            {
                                button2_Click(button2, EventArgs.Empty);
                            }                            
                        }
                        break;
                    case 3:
                        if (inbuffercountbak < 2) { return; }
                        if (ReceiveBuff[0] == 0x69 && ReceiveBuff[1] == 0xF0)
                        {
                            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;
                    default :
                            break ;

                }
               
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            byte[] SendBuf = new byte[10];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行更改在线设备站号的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int oldjihao = Convert.ToInt32(textBox1.Text);
            int newjihao = (int)(newjihao1.Value) ;
            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(oldjihao % 256); 
            SendBuf[3] = (byte)(oldjihao / 256);
            SendBuf[4] = 0xF0;
            SendBuf[5] = (byte)(newjihao % 256);
            SendBuf[6] = (byte)(newjihao /256);
            SendBuf[7] = fangma(SendBuf[5]);
            SendBuf[8] = fangma(SendBuf[6]);
            SendBuf[9] = 0x00;

            SendCode = 2;
            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 9; i++)
            {
                SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
            }

            listBox1.Items.Add(SendStr);
            listBox1.SelectedIndex = listBox1.Items.Count - 1; 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[9];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行设定在线设备密码的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            string paswstr=textBox2 .Text ;
            paswstr = paswstr.PadLeft(6, '0');

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x0F;
            SendBuf[5] = (byte)Convert.ToInt16 (paswstr.Substring(0,2) , 16);
            SendBuf[6] = (byte)Convert.ToInt16(paswstr.Substring(2, 2), 16);
            SendBuf[7] = (byte)Convert.ToInt16(paswstr.Substring(4, 2), 16);
            SendBuf[8] = (byte)(SendBuf[5] ^ SendBuf[6] ^ SendBuf[7]);

            SendCode = 3;
            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 8; i++)
            {
                SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
            }

            listBox1.Items.Add(SendStr);
            listBox1.SelectedIndex = listBox1.Items.Count - 1; 
        }

        private void button5_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[8];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            int Deepdelay = (int)Deepdelay1.Value ;
            
            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0xC3;
            SendBuf[5] = (byte)(Deepdelay / 256);
            SendBuf[6] = (byte)(Deepdelay % 256);
            SendBuf[7] = (byte)(SendBuf[5] ^ SendBuf[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 button6_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear () ;
        }

        private void button7_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);
            int Deepdelay = (int)Deepdelay1.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0xB4;
            
            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 button8_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[8];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            int Deepdelay = (int)Deepdelay2.Value;
            int stopdelay = (int)stopdelay1.Value;
            int numno = (int)numb1.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x96;
            SendBuf[5] = (byte)(Deepdelay / 20);
            SendBuf[6] = (byte)(stopdelay /20);
            SendBuf[7] = (byte)numno;

            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(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);
            int Deepdelay = (int)Deepdelay1.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0xD2;

            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 button10_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);
            int Deepdelay = (int)Deepdelay1.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x87;

            SendCode = 5;
            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 button11_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);
            int Deepdelay = (int)Deepdelay1.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x69;

            SendCode = 6;
            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;
            timer1.Enabled = false;
            timer2.Enabled = true;  //

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 4; i++)
            {
                SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
            }

            if (listBox1.Items.Count > 10) { listBox1.Items.Clear(); }
            listBox1.Items.Add(SendStr);
            listBox1.SelectedIndex = listBox1.Items.Count - 1; 
        }

        private void button12_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[39];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);            
            int Deepdelay = (int)Deepdelay3.Value;
            int stopdelay = (int)stopdelay2.Value;
            int numno = (int)numb2.Value;

            dispstr = dispstr + textBox5.Text + "                                ";
            byte[] byteArray = System.Text.Encoding.GetEncoding(936).GetBytes(dispstr);

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x5A;

            for (int i = 0; i <= 29;i++ )
            {
                SendBuf[5 + i] = byteArray[i];
            }
            SendBuf[35] = (byte)(Deepdelay / 20);
            SendBuf[36] = (byte)(stopdelay / 20);
            SendBuf[37] = (byte)numno;

            SendBuf[38] = 0x00;
            for (int j=5;j<=37;j++)
            {
                SendBuf[38] =(byte)( SendBuf[38] ^ SendBuf[j]);
            }

            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 38; i++)
            {
                SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
            }

            listBox1.Items.Add(SendStr);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
           
        }

        private void button13_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[9];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            int Deepdelay = (int)Deepdelay4.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x1E;
            SendBuf[5] = 0xF1; 
            SendBuf[6] = (byte)(Deepdelay / 256);
            SendBuf[7] = (byte)(Deepdelay % 256);
            SendBuf[8] = (byte)(SendBuf[5] ^ SendBuf[6] ^ SendBuf[7]);

            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 8; i++)
            {
                SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
            }

            listBox1.Items.Add(SendStr);
            listBox1.SelectedIndex = listBox1.Items.Count - 1; 
        }

        private void button14_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[9];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            int Deepdelay = (int)Deepdelay4.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x1E;
            SendBuf[5] = 0xE1;
            SendBuf[6] = 0x00;
            SendBuf[7] = 0x00;
            SendBuf[8] = 0xE1;

            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 8; 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[36];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            int Deepdelay = (int)Deepdelay3.Value;
            int stopdelay = (int)stopdelay2.Value;
            int numno = (int)numb2.Value;

            string dispstr = textBox5.Text + "                                ";
            byte[] byteArray = System.Text.Encoding.GetEncoding(936).GetBytes(dispstr);

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x78;

            for (int i = 0; i <= 29; i++)
            {
                SendBuf[5 + i] = byteArray[i];
            }
            
            SendBuf[35] = 0x00;
            for (int j = 5; j <= 34; j++)
            {
                SendBuf[35] = (byte)(SendBuf[35] ^ SendBuf[j]);
            }

            axMSComm1.Output = SendBuf;          //向串口发送数据
            axMSComm1.RThreshold = 1;

            string SendStr = "串口发送数据:";   //将发送的数据显示在列表框
            for (int i = 0; i <= 35; i++)
            {
                SendStr = SendStr + SendBuf[i].ToString("X2") + " ";
            }

            listBox1.Items.Add(SendStr);
            listBox1.SelectedIndex = listBox1.Items.Count - 1; 
        }

        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 button16_Click(object sender, EventArgs e)
        {
            byte[] SendBuf = new byte[13];
            if (button1.Text == "打开串口")
            {
                MessageBox.Show("请先打开串口后再执行设定在线设备密码的操作!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int jihao = Convert.ToInt32(textBox1.Text);
            int wekk = Convert.ToInt32(DateTime.Now.DayOfWeek);

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x4B;
            SendBuf[5] = (byte)Convert.ToByte(Convert.ToString(System.DateTime.Now.Second,10),16);
            SendBuf[6] = (byte)Convert.ToByte(Convert.ToString(System.DateTime.Now.Minute , 10), 16);
            SendBuf[7] = (byte)Convert.ToByte(Convert.ToString(System.DateTime.Now.Hour , 10), 16);
            SendBuf[8] = (byte)Convert.ToByte(Convert.ToString(System.DateTime.Now.Day , 10), 16);
            SendBuf[9] = (byte)Convert.ToByte(Convert.ToString(System.DateTime.Now.Month , 10), 16);
            SendBuf[10] = (byte)Convert.ToByte(Convert.ToString(wekk, 10), 16);
            SendBuf[11] = (byte)Convert.ToByte(Convert.ToString(System.DateTime.Now.Year -2000, 10), 16);
            SendBuf[12] = (byte)(SendBuf[5] ^ SendBuf[6] ^ SendBuf[7] ^ SendBuf[8] ^ SendBuf[9] ^ SendBuf[10] ^ SendBuf[11]);

            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(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);
            int Deepdelay = (int)Deepdelay1.Value;

            SendBuf[0] = 0xAA;
            SendBuf[1] = 0xAA;
            SendBuf[2] = (byte)(jihao % 256);
            SendBuf[3] = (byte)(jihao / 256);
            SendBuf[4] = 0x2D;

            SendCode = 7;
            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 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);
            int Deepdelay = (int)Deepdelay1.Value;

            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; 
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

津津有味道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值