c#多串口重量采集上位机程序

1.运行界面

2.代码

cs 复制代码
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.IO.Ports;
using System.Data.SqlClient;
using System.Collections;

namespace Weight
{
    public partial class Form1 : Form
    {
        public static string[] Modbus_Text=new string[10];//指令连接字符串(文本文档)    
        public static string[] str = new string[10];//
        public static string[] strID = new string[10];//地址
        public static string[] strAddr = new string[10];//读起始地址
        public static string[] strLength = new string[10];//读数据长度
        SerialPort sp1 = null;
        SerialPort sp2 = null;
        SerialPort sp3 = null;
        SerialPort sp4 = null;
        SerialPort sp5 = null;
        SerialPort sp6 = null;
        DataTable dt = new DataTable();

        private Form2 form2;  // 保存引用




        public static string strSqlcon;//数据库连接字符串(文本文档)      
        SqlConnection conn;
        public Form1()
        {
            InitializeComponent();
        }

        void txt1ps()
        {
            strSqlcon = System.IO.File.ReadAllText(@"sqlcon.txt");//讀取內容 数据库连接字符串
        }

        void txt2ps()
        {

            Modbus_Text[0] = System.IO.File.ReadAllText(@"Modbus1.txt");//讀取內容  mobus读取指令
        }
        void txt3ps()
        {

            Modbus_Text[1] = System.IO.File.ReadAllText(@"Modbus2.txt");//讀取內容  mobus读取指令
        }
        void txt4ps()
        {

            Modbus_Text[2] = System.IO.File.ReadAllText(@"Modbus3.txt");//讀取內容  mobus读取指令
        }
        void txt5ps()
        {

            Modbus_Text[3] = System.IO.File.ReadAllText(@"Modbus4.txt");//讀取內容  mobus读取指令
        }
        void txt6ps()
        {

            Modbus_Text[4] = System.IO.File.ReadAllText(@"Modbus5.txt");//讀取內容  mobus读取指令
        }
        void txt7ps()
        {

            Modbus_Text[5] = System.IO.File.ReadAllText(@"Modbus6.txt");//讀取內容  mobus读取指令
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txt1ps();//读取txt到全局变量            
            string str = (strSqlcon);//定义数据库连接字符串
            conn = new SqlConnection(str);//连接数据库
            txt2ps();//读取txt到全局变量
            txt3ps();//读取txt到全局变量
            txt4ps();//读取txt到全局变量
            txt5ps();//读取txt到全局变量
            txt6ps();//读取txt到全局变量
            txt7ps();//读取txt到全局变量

            timer1.Interval = 1000;//设定程序扫描时间1S
            timer1.Enabled = true;//定时器启动    

           
            接收1.Interval = 1000;
            接收2.Interval = 1000;
            接收3.Interval = 1000;
            接收4.Interval = 1000;
            接收5.Interval = 1000;
            接收6.Interval = 1000;
            发送1.Interval = 1000;
            发送2.Interval = 1000;
            发送3.Interval = 1000;
            发送4.Interval = 1000;
            发送5.Interval = 1000;
            发送6.Interval = 1000;

            接收1.Enabled = true;//启动的太早了,sp1还是空的
            接收2.Enabled = true;//启动的太早了,sp1还是空的
            接收3.Enabled = true;//启动的太早了,sp1还是空的
            接收4.Enabled = true;//启动的太早了,sp1还是空的
            接收5.Enabled = true;//启动的太早了,sp1还是空的
            接收6.Enabled = true;//启动的太早了,sp1还是空的
            发送1.Enabled = true;//启动的太早了,sp1还是空的
            发送2.Enabled = true;//启动的太早了,sp1还是空的
            发送3.Enabled = true;//启动的太早了,sp1还是空的
            发送4.Enabled = true;//启动的太早了,sp1还是空的
            发送5.Enabled = true;//启动的太早了,sp1还是空的
            发送6.Enabled = true;//启动的太早了,sp1还是空的

            try
            {
                //串口参数
                sp1 = new SerialPort();
                sp1.PortName = "COM1";//串口编号10
                sp1.BaudRate = 115200;// 9600;//波特率
                sp1.Parity = Parity.None;//无校验位
                sp1.DataBits = 8;
                sp1.StopBits = StopBits.One;
                sp1.Open();//打开串口
            }
            catch
            { }
            try
            {
                //串口参数
                sp2 = new SerialPort();
                sp2.PortName = "COM11";//串口编号11
                sp2.BaudRate = 9600;//波特率
                sp2.Parity = Parity.None;//无校验位
                sp2.DataBits = 8;
                sp2.StopBits = StopBits.One;
                sp2.Open();//打开串口
            }
            catch
            { }
            try
            {
                //串口参数
                sp3 = new SerialPort();
                sp3.PortName = "COM9";//串口编号9
                sp3.BaudRate = 9600;//波特率
                sp3.Parity = Parity.None;//无校验位
                sp3.DataBits = 8;
                sp3.StopBits = StopBits.One;
                sp3.Open();//打开串口
            }
            catch
            { }
            try
            {
                //串口参数
                sp4 = new SerialPort();
                sp4.PortName = "COM12";//串口编号12
                sp4.BaudRate = 9600;//波特率
                sp4.Parity = Parity.None;//无校验位
                sp4.DataBits = 8;
                sp4.StopBits = StopBits.One;
                sp4.Open();//打开串口
            }
            catch
            { }
            try
            {
                //串口参数
                sp5 = new SerialPort();
                sp5.PortName = "COM13";//串口编号13
                sp5.BaudRate = 9600;//波特率
                sp5.Parity = Parity.None;//无校验位
                sp5.DataBits = 8;
                sp5.StopBits = StopBits.One;
                sp5.Open();//打开串口
            }
            catch
            { }
            try
            {
                //串口参数
                sp6 = new SerialPort();
                sp6.PortName = "COM14";//串口编号14
                sp6.BaudRate = 9600;//波特率
                sp6.Parity = Parity.None;//无校验位
                sp6.DataBits = 8;
                sp6.StopBits = StopBits.One;
                sp6.Open();//打开串口
            }
            catch
            { }

            try
            {
                //开机读取原累积量
                dt.Clear();//临时数据表清空
                conn.Open();
                //填充参数列表			
                SqlCommand sqlcmdCount = new SqlCommand(@"SELECT    weight1, weight2, weight3, weight4, weight5, weight6, weight7, weight8, weight9, weight10, weight11, 
                weight12, weight13, weight14, weight15, weight16, weight17, weight18, weight19, weight20, weight21, weight22
                FROM      Weight_Data_LJ", conn);
                sqlcmdCount.CommandTimeout = 600;
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = sqlcmdCount;
                sda.Fill(dt);//填充数据到dt2			           
                textBox15.Text = string.Format("{0:F2}", (dt.Rows[0][0]));
                textBox18.Text = string.Format("{0:F2}", (dt.Rows[0][1]));
                textBox21.Text = string.Format("{0:F2}", (dt.Rows[0][2]));
                textBox24.Text = string.Format("{0:F2}", (dt.Rows[0][3]));
                textBox27.Text = string.Format("{0:F2}", (dt.Rows[0][4]));
                textBox30.Text = string.Format("{0:F2}", (dt.Rows[0][5]));
                textBox33.Text = string.Format("{0:F2}", (dt.Rows[0][6]));
                textBox36.Text = string.Format("{0:F2}", (dt.Rows[0][7]));
                textBox39.Text = string.Format("{0:F2}", (dt.Rows[0][8]));
                textBox42.Text = string.Format("{0:F2}", (dt.Rows[0][9]));
                textBox45.Text = string.Format("{0:F2}", (dt.Rows[0][10]));
                textBox48.Text = string.Format("{0:F2}", (dt.Rows[0][11]));
                textBox51.Text = string.Format("{0:F2}", (dt.Rows[0][12]));
                textBox54.Text = string.Format("{0:F2}", (dt.Rows[0][13]));
                textBox57.Text = string.Format("{0:F2}", (dt.Rows[0][14]));
                textBox60.Text = string.Format("{0:F2}", (dt.Rows[0][15]));
                textBox63.Text = string.Format("{0:F2}", (dt.Rows[0][16]));
                textBox66.Text = string.Format("{0:F2}", (dt.Rows[0][17]));
                textBox69.Text = string.Format("{0:F2}", (dt.Rows[0][18]));
                textBox72.Text = string.Format("{0:F2}", (dt.Rows[0][19]));
                textBox75.Text = string.Format("{0:F2}", (dt.Rows[0][20]));
                textBox78.Text = string.Format("{0:F2}", (dt.Rows[0][21]));

            }
            catch
            { }
        }
        
            
    


        //crc16校验码计算方法
        public static byte[] crc16(byte[] data)
        {
            if (data.Length == 0)
                throw new Exception("调用CRC16校验算法,(低字节在前,高字节在后)时发生异常,异常信息:被校验的数组长度为0。");
            byte[] temdata = new byte[data.Length + 2];
            int xda, xdapoly;
            byte i, j, xdabit;
            xda = 0xFFFF;
            xdapoly = 0xA001;
            for (i = 0; i < data.Length; i++)
            {
                xda ^= data[i];
                for (j = 0; j < 8; j++)
                {
                    xdabit = (byte)(xda & 0x01);
                    xda >>= 1;
                    if (xdabit == 1)
                        xda ^= xdapoly;
                }
            }
            temdata = new byte[2] { (byte)(xda & 0xFF), (byte)(xda >> 8) };
            return temdata;
        }
        //浮点数计算方法

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {         
            this.Dispose();
            conn.Close();
        }

       
        

        private void timer1_Tick(object sender, EventArgs e)
        {         
            //数据库连接状态
            if (conn.State == ConnectionState.Open)
            {
                ovalShape1.FillColor = Color.Lime;
            }
            else
            {
                ovalShape1.FillColor = Color.Red;
            }
            try
            {
                //1号串口连接状态
                if (sp1.IsOpen == true)
                {
                    ovalShape2.FillColor = Color.Lime;
                }
                else
                {
                    ovalShape2.FillColor = Color.Red;
                }
                //2号串口连接状态
                if (sp2.IsOpen == true)
                {
                    ovalShape3.FillColor = Color.Lime;
                }
                else
                {
                    ovalShape3.FillColor = Color.Red;
                }
                //3号串口连接状态
                if (sp3.IsOpen == true)
                {
                    ovalShape4.FillColor = Color.Lime;
                }
                else
                {
                    ovalShape4.FillColor = Color.Red;
                }
                //4号串口连接状态
                if (sp4.IsOpen == true)
                {
                    ovalShape5.FillColor = Color.Lime;
                }
                else
                {
                    ovalShape5.FillColor = Color.Red;
                }
                //5号串口连接状态
                if (sp5.IsOpen == true)
                {
                    ovalShape6.FillColor = Color.Lime;
                }
                else
                {
                    ovalShape6.FillColor = Color.Red;
                }
                //6号串口连接状态
                if (sp6.IsOpen == true)
                {
                    ovalShape7.FillColor = Color.Lime;
                }
                else
                {
                    ovalShape7.FillColor = Color.Red;
                }
            }
            catch
            { }


            
           




           

        }





        int temp1 = 2;
        int temp2 = 2;
        int temp3 = 2;
        int temp4 = 2;
        int temp5 = 2;
        int temp6 = 2;
        private void 发送1_Tick(object sender, EventArgs e)
        {
            str[0] = (Modbus_Text[0]);//赋值字符串
            str[0] = str[0].Replace(" ", "");//取消所有空格  
            str[0] = str[0].Replace("\r", "");//取消\r
            str[0] = str[0].Replace("\n", "");//取消\n
            string[] sArray = str[0].Split(';', '=', '-');
            int ins_num = sArray.Length;


           
            if (temp1 >= ins_num)
            {
                temp1 = 2;
            }


            strID[0] = sArray[temp1 - 1];
            strAddr[0] = sArray[temp1];
            strLength[0] = sArray[temp1 + 3];
            temp1 = temp1 + 6;


            //清除上一次内容
            textBox1.Clear();
            //获取文本框的数值
            string ID;//仪表地址
            string Fun = "03";//功能码

            string Read_addr;//读起始地址
            string Read_addr_H;//读起始地址高位
            string Read_addr_L;//读起始地址低位

            string Read_lengh;//读数据长度
            string Read_lengh_H;//读数据长度
            string Read_lengh_L;//读数据长度

            //转成数字
            //转16进制                                              变大写         一共两位不够的补0在左边
            ID = Convert.ToString(Convert.ToInt16(strID[0]), 16).ToUpper().PadLeft(2, '0');
            //读起始地址
            Read_addr = Convert.ToString(((Convert.ToInt16(strAddr[0])) - 1), 16).ToUpper().PadLeft(4, '0');
            //起始地址高位
            Read_addr_H = Read_addr.Substring(0, 2);
            //起始地址低位
            Read_addr_L = Read_addr.Substring(2, 2);
            //读数据长度
            Read_lengh = Convert.ToString(Convert.ToInt16(strLength[0]), 16).ToUpper().PadLeft(4, '0');
            //读数据长度高位
            Read_lengh_H = Read_lengh.Substring(0, 2);
            //读数据长度低位
            Read_lengh_L = Read_lengh.Substring(2, 2);

            //算crc16校验码
            byte[] buf = new byte[6];
            string crc_h;
            string crc_l;

            buf[0] = Convert.ToByte(ID, 16);
            buf[1] = 03;
            Fun = "03";

            buf[2] = Convert.ToByte(Read_addr_H, 16);
            buf[3] = Convert.ToByte(Read_addr_L, 16);
            buf[4] = Convert.ToByte(Read_lengh_H, 16);
            buf[5] = Convert.ToByte(Read_lengh_L, 16);
            //调用算法;
            byte[] res = crc16(buf);
            //执行输出;
            crc_h = Convert.ToString(res[0], 16).ToUpper().PadLeft(2, '0');
            crc_l = Convert.ToString(res[1], 16).ToUpper().PadLeft(2, '0');

            //指令发送
            textBox1.Text = ID + " " + Fun + " " + Read_addr_H + " " + Read_addr_L + " " + Read_lengh_H + " " + Read_lengh_L + " " + crc_h + " " + crc_l;


            //赋字符串
            string str1 = textBox1.Text;
            //删除所有空格
            string str2 = str1.Replace(" ", "");
            //获取字符串长度
            int lengh = str2.Length / 2;

            //填充数据
            int i;
            int j = 0;
            byte[] b = new byte[lengh];
            for (i = 0; i < lengh; i++)
            {
                //截取字符串
                b[i] = Convert.ToByte(str2.Substring(j, 2), 16);
                j = j + 2;
            }
            try
            {
                sp1.Write(b, 0, lengh);
            }
            catch
            { }


        }   
        private void 发送2_Tick(object sender, EventArgs e)
        {
            str[1] = (Modbus_Text[1]);//赋值字符串
            str[1] = str[1].Replace(" ", "");//取消所有空格  
            str[1] = str[1].Replace("\r", "");//取消\r
            str[1] = str[1].Replace("\n", "");//取消\n
            string[] sArray = str[1].Split(';', '=', '-');
            int ins_num = sArray.Length;

            if (temp2 >= ins_num)
            {
                temp2 = 2;
            }


            strID[1] = sArray[temp2 - 1];
            strAddr[1] = sArray[temp2];
            strLength[1] = sArray[temp2 + 3];
            temp2 = temp2 + 6;


            //清除上一次内容
            textBox3.Clear();
            //获取文本框的数值
            string ID;//仪表地址
            string Fun = "03";//功能码

            string Read_addr;//读起始地址
            string Read_addr_H;//读起始地址高位
            string Read_addr_L;//读起始地址低位

            string Read_lengh;//读数据长度
            string Read_lengh_H;//读数据长度
            string Read_lengh_L;//读数据长度

            //转成数字
            //转16进制                                              变大写         一共两位不够的补0在左边
            ID = Convert.ToString(Convert.ToInt16(strID[1]), 16).ToUpper().PadLeft(2, '0');
            //读起始地址
            Read_addr = Convert.ToString(((Convert.ToInt16(strAddr[1])) - 1), 16).ToUpper().PadLeft(4, '0');
            //起始地址高位
            Read_addr_H = Read_addr.Substring(0, 2);
            //起始地址低位
            Read_addr_L = Read_addr.Substring(2, 2);
            //读数据长度
            Read_lengh = Convert.ToString(Convert.ToInt16(strLength[1]), 16).ToUpper().PadLeft(4, '0');
            //读数据长度高位
            Read_lengh_H = Read_lengh.Substring(0, 2);
            //读数据长度低位
            Read_lengh_L = Read_lengh.Substring(2, 2);

            //算crc16校验码
            byte[] buf = new byte[6];
            string crc_h;
            string crc_l;

            buf[0] = Convert.ToByte(ID, 16);
            buf[1] = 03;
            Fun = "03";

            buf[2] = Convert.ToByte(Read_addr_H, 16);
            buf[3] = Convert.ToByte(Read_addr_L, 16);
            buf[4] = Convert.ToByte(Read_lengh_H, 16);
            buf[5] = Convert.ToByte(Read_lengh_L, 16);
            //调用算法;
            byte[] res = crc16(buf);
            //执行输出;
            crc_h = Convert.ToString(res[0], 16).ToUpper().PadLeft(2, '0');
            crc_l = Convert.ToString(res[1], 16).ToUpper().PadLeft(2, '0');

            //指令发送
            textBox3.Text = ID + " " + Fun + " " + Read_addr_H + " " + Read_addr_L + " " + Read_lengh_H + " " + Read_lengh_L + " " + crc_h + " " + crc_l;


            //赋字符串
            string str1 = textBox3.Text;
            //删除所有空格
            string str2 = str1.Replace(" ", "");
            //获取字符串长度
            int lengh = str2.Length / 2;

            //填充数据
            int i;
            int j = 0;
            byte[] b = new byte[lengh];
            for (i = 0; i < lengh; i++)
            {
                //截取字符串
                b[i] = Convert.ToByte(str2.Substring(j, 2), 16);
                j = j + 2;
            }
            try
            {
                sp2.Write(b, 0, lengh);
            }
            catch
            { }
        }       
        private void 发送3_Tick(object sender, EventArgs e)
        {
            str[2] = (Modbus_Text[2]);//赋值字符串
            str[2] = str[2].Replace(" ", "");//取消所有空格  
            str[2] = str[2].Replace("\r", "");//取消\r
            str[2] = str[2].Replace("\n", "");//取消\n
            string[] sArray = str[2].Split(';', '=', '-');
            int ins_num = sArray.Length;

            if (temp3 >= ins_num)
            {
                temp3 = 2;
            }


            strID[2] = sArray[temp3 - 1];
            strAddr[2] = sArray[temp3];
            strLength[2] = sArray[temp3 + 3];
            temp3 = temp3 + 6;


            //清除上一次内容
            textBox5.Clear();
            //获取文本框的数值
            string ID;//仪表地址
            string Fun = "03";//功能码

            string Read_addr;//读起始地址
            string Read_addr_H;//读起始地址高位
            string Read_addr_L;//读起始地址低位

            string Read_lengh;//读数据长度
            string Read_lengh_H;//读数据长度
            string Read_lengh_L;//读数据长度

            //转成数字
            //转16进制                                              变大写         一共两位不够的补0在左边
            ID = Convert.ToString(Convert.ToInt16(strID[2]), 16).ToUpper().PadLeft(2, '0');
            //读起始地址
            Read_addr = Convert.ToString(((Convert.ToInt16(strAddr[2])) - 1), 16).ToUpper().PadLeft(4, '0');
            //起始地址高位
            Read_addr_H = Read_addr.Substring(0, 2);
            //起始地址低位
            Read_addr_L = Read_addr.Substring(2, 2);
            //读数据长度
            Read_lengh = Convert.ToString(Convert.ToInt16(strLength[2]), 16).ToUpper().PadLeft(4, '0');
            //读数据长度高位
            Read_lengh_H = Read_lengh.Substring(0, 2);
            //读数据长度低位
            Read_lengh_L = Read_lengh.Substring(2, 2);

            //算crc16校验码
            byte[] buf = new byte[6];
            string crc_h;
            string crc_l;

            buf[0] = Convert.ToByte(ID, 16);
            buf[1] = 03;
            Fun = "03";

            buf[2] = Convert.ToByte(Read_addr_H, 16);
            buf[3] = Convert.ToByte(Read_addr_L, 16);
            buf[4] = Convert.ToByte(Read_lengh_H, 16);
            buf[5] = Convert.ToByte(Read_lengh_L, 16);
            //调用算法;
            byte[] res = crc16(buf);
            //执行输出;
            crc_h = Convert.ToString(res[0], 16).ToUpper().PadLeft(2, '0');
            crc_l = Convert.ToString(res[1], 16).ToUpper().PadLeft(2, '0');

            //指令发送
            textBox5.Text = ID + " " + Fun + " " + Read_addr_H + " " + Read_addr_L + " " + Read_lengh_H + " " + Read_lengh_L + " " + crc_h + " " + crc_l;


            //赋字符串
            string str1 = textBox5.Text;
            //删除所有空格
            string str2 = str1.Replace(" ", "");
            //获取字符串长度
            int lengh = str2.Length / 2;

            //填充数据
            int i;
            int j = 0;
            byte[] b = new byte[lengh];
            for (i = 0; i < lengh; i++)
            {
                //截取字符串
                b[i] = Convert.ToByte(str2.Substring(j, 2), 16);
                j = j + 2;
            }
            try
            {
                sp3.Write(b, 0, lengh);
            }
            catch
            { }
        }       
        private void 发送4_Tick(object sender, EventArgs e)
        {
            str[3] = (Modbus_Text[3]);//赋值字符串
            str[3] = str[3].Replace(" ", "");//取消所有空格  
            str[3] = str[3].Replace("\r", "");//取消\r
            str[3] = str[3].Replace("\n", "");//取消\n
            string[] sArray = str[3].Split(';', '=', '-');
            int ins_num = sArray.Length;

            if (temp4 >= ins_num)
            {
                temp4 = 2;
            }


            strID[3] = sArray[temp4 - 1];
            strAddr[3] = sArray[temp4];
            strLength[3] = sArray[temp4 + 3];
            temp4 = temp4 + 6;


            //清除上一次内容
            textBox7.Clear();
            //获取文本框的数值
            string ID;//仪表地址
            string Fun = "03";//功能码

            string Read_addr;//读起始地址
            string Read_addr_H;//读起始地址高位
            string Read_addr_L;//读起始地址低位

            string Read_lengh;//读数据长度
            string Read_lengh_H;//读数据长度
            string Read_lengh_L;//读数据长度

            //转成数字
            //转16进制                                              变大写         一共两位不够的补0在左边
            ID = Convert.ToString(Convert.ToInt16(strID[3]), 16).ToUpper().PadLeft(2, '0');
            //读起始地址
            Read_addr = Convert.ToString(((Convert.ToInt16(strAddr[3])) - 1), 16).ToUpper().PadLeft(4, '0');
            //起始地址高位
            Read_addr_H = Read_addr.Substring(0, 2);
            //起始地址低位
            Read_addr_L = Read_addr.Substring(2, 2);
            //读数据长度
            Read_lengh = Convert.ToString(Convert.ToInt16(strLength[3]), 16).ToUpper().PadLeft(4, '0');
            //读数据长度高位
            Read_lengh_H = Read_lengh.Substring(0, 2);
            //读数据长度低位
            Read_lengh_L = Read_lengh.Substring(2, 2);

            //算crc16校验码
            byte[] buf = new byte[6];
            string crc_h;
            string crc_l;

            buf[0] = Convert.ToByte(ID, 16);
            buf[1] = 03;
            Fun = "03";

            buf[2] = Convert.ToByte(Read_addr_H, 16);
            buf[3] = Convert.ToByte(Read_addr_L, 16);
            buf[4] = Convert.ToByte(Read_lengh_H, 16);
            buf[5] = Convert.ToByte(Read_lengh_L, 16);
            //调用算法;
            byte[] res = crc16(buf);
            //执行输出;
            crc_h = Convert.ToString(res[0], 16).ToUpper().PadLeft(2, '0');
            crc_l = Convert.ToString(res[1], 16).ToUpper().PadLeft(2, '0');

            //指令发送
            textBox7.Text = ID + " " + Fun + " " + Read_addr_H + " " + Read_addr_L + " " + Read_lengh_H + " " + Read_lengh_L + " " + crc_h + " " + crc_l;


            //赋字符串
            string str1 = textBox7.Text;
            //删除所有空格
            string str2 = str1.Replace(" ", "");
            //获取字符串长度
            int lengh = str2.Length / 2;

            //填充数据
            int i;
            int j = 0;
            byte[] b = new byte[lengh];
            for (i = 0; i < lengh; i++)
            {
                //截取字符串
                b[i] = Convert.ToByte(str2.Substring(j, 2), 16);
                j = j + 2;
            }
            try
            {
                sp4.Write(b, 0, lengh);
            }
            catch
            { }
        }      
        private void 发送5_Tick(object sender, EventArgs e)
        {
            str[4] = (Modbus_Text[4]);//赋值字符串
            str[4] = str[4].Replace(" ", "");//取消所有空格  
            str[4] = str[4].Replace("\r", "");//取消\r
            str[4] = str[4].Replace("\n", "");//取消\n
            string[] sArray = str[4].Split(';', '=', '-');
            int ins_num = sArray.Length;

            if (temp5 >= ins_num)
            {
                temp5 = 2;
            }


            strID[4] = sArray[temp5 - 1];
            strAddr[4] = sArray[temp5];
            strLength[4] = sArray[temp5 + 3];
            temp5 = temp5 + 6;


            //清除上一次内容
            textBox9.Clear();
            //获取文本框的数值
            string ID;//仪表地址
            string Fun = "03";//功能码

            string Read_addr;//读起始地址
            string Read_addr_H;//读起始地址高位
            string Read_addr_L;//读起始地址低位

            string Read_lengh;//读数据长度
            string Read_lengh_H;//读数据长度
            string Read_lengh_L;//读数据长度

            //转成数字
            //转16进制                                              变大写         一共两位不够的补0在左边
            ID = Convert.ToString(Convert.ToInt16(strID[4]), 16).ToUpper().PadLeft(2, '0');
            //读起始地址
            Read_addr = Convert.ToString(((Convert.ToInt16(strAddr[4])) - 1), 16).ToUpper().PadLeft(4, '0');
            //起始地址高位
            Read_addr_H = Read_addr.Substring(0, 2);
            //起始地址低位
            Read_addr_L = Read_addr.Substring(2, 2);
            //读数据长度
            Read_lengh = Convert.ToString(Convert.ToInt16(strLength[4]), 16).ToUpper().PadLeft(4, '0');
            //读数据长度高位
            Read_lengh_H = Read_lengh.Substring(0, 2);
            //读数据长度低位
            Read_lengh_L = Read_lengh.Substring(2, 2);

            //算crc16校验码
            byte[] buf = new byte[6];
            string crc_h;
            string crc_l;

            buf[0] = Convert.ToByte(ID, 16);
            buf[1] = 03;
            Fun = "03";

            buf[2] = Convert.ToByte(Read_addr_H, 16);
            buf[3] = Convert.ToByte(Read_addr_L, 16);
            buf[4] = Convert.ToByte(Read_lengh_H, 16);
            buf[5] = Convert.ToByte(Read_lengh_L, 16);
            //调用算法;
            byte[] res = crc16(buf);
            //执行输出;
            crc_h = Convert.ToString(res[0], 16).ToUpper().PadLeft(2, '0');
            crc_l = Convert.ToString(res[1], 16).ToUpper().PadLeft(2, '0');

            //指令发送
            textBox9.Text = ID + " " + Fun + " " + Read_addr_H + " " + Read_addr_L + " " + Read_lengh_H + " " + Read_lengh_L + " " + crc_h + " " + crc_l;


            //赋字符串
            string str1 = textBox9.Text;
            //删除所有空格
            string str2 = str1.Replace(" ", "");
            //获取字符串长度
            int lengh = str2.Length / 2;

            //填充数据
            int i;
            int j = 0;
            byte[] b = new byte[lengh];
            for (i = 0; i < lengh; i++)
            {
                //截取字符串
                b[i] = Convert.ToByte(str2.Substring(j, 2), 16);
                j = j + 2;
            }
            try
            {
                sp5.Write(b, 0, lengh);
            }
            catch
            { }
        }
        
        private void 发送6_Tick(object sender, EventArgs e)
        {
            str[5] = (Modbus_Text[5]);//赋值字符串
            str[5] = str[5].Replace(" ", "");//取消所有空格  
            str[5] = str[5].Replace("\r", "");//取消\r
            str[5] = str[5].Replace("\n", "");//取消\n
            string[] sArray = str[5].Split(';', '=', '-');
            int ins_num = sArray.Length;

            if (temp6 >= ins_num)
            {
                temp6 = 2;
            }


            strID[5] = sArray[temp6 - 1];
            strAddr[5] = sArray[temp6];
            strLength[5] = sArray[temp6 + 3];
            temp6 = temp6 + 6;


            //清除上一次内容
            textBox11.Clear();
            //获取文本框的数值
            string ID;//仪表地址
            string Fun = "03";//功能码

            string Read_addr;//读起始地址
            string Read_addr_H;//读起始地址高位
            string Read_addr_L;//读起始地址低位

            string Read_lengh;//读数据长度
            string Read_lengh_H;//读数据长度
            string Read_lengh_L;//读数据长度

            //转成数字
            //转16进制                                              变大写         一共两位不够的补0在左边
            ID = Convert.ToString(Convert.ToInt16(strID[5]), 16).ToUpper().PadLeft(2, '0');
            //读起始地址
            Read_addr = Convert.ToString(((Convert.ToInt16(strAddr[5])) - 1), 16).ToUpper().PadLeft(4, '0');
            //起始地址高位
            Read_addr_H = Read_addr.Substring(0, 2);
            //起始地址低位
            Read_addr_L = Read_addr.Substring(2, 2);
            //读数据长度
            Read_lengh = Convert.ToString(Convert.ToInt16(strLength[5]), 16).ToUpper().PadLeft(4, '0');
            //读数据长度高位
            Read_lengh_H = Read_lengh.Substring(0, 2);
            //读数据长度低位
            Read_lengh_L = Read_lengh.Substring(2, 2);

            //算crc16校验码
            byte[] buf = new byte[6];
            string crc_h;
            string crc_l;

            buf[0] = Convert.ToByte(ID, 16);
            buf[1] = 03;
            Fun = "03";

            buf[2] = Convert.ToByte(Read_addr_H, 16);
            buf[3] = Convert.ToByte(Read_addr_L, 16);
            buf[4] = Convert.ToByte(Read_lengh_H, 16);
            buf[5] = Convert.ToByte(Read_lengh_L, 16);
            //调用算法;
            byte[] res = crc16(buf);
            //执行输出;
            crc_h = Convert.ToString(res[0], 16).ToUpper().PadLeft(2, '0');
            crc_l = Convert.ToString(res[1], 16).ToUpper().PadLeft(2, '0');

            //指令发送
            textBox11.Text = ID + " " + Fun + " " + Read_addr_H + " " + Read_addr_L + " " + Read_lengh_H + " " + Read_lengh_L + " " + crc_h + " " + crc_l;


            //赋字符串
            string str1 = textBox11.Text;
            //删除所有空格
            string str2 = str1.Replace(" ", "");
            //获取字符串长度
            int lengh = str2.Length / 2;

            //填充数据
            int i;
            int j = 0;
            byte[] b = new byte[lengh];
            for (i = 0; i < lengh; i++)
            {
                //截取字符串
                b[i] = Convert.ToByte(str2.Substring(j, 2), 16);
                j = j + 2;
            }
            try
            {
                sp6.Write(b, 0, lengh);
            }
            catch
            {
                return;
            }
        }

        //4-20ma数据处理
        public void AI (float i,TextBox AI_OUT, TextBox Rang)
        {
            if (i >= 4000)
            {
                AI_OUT.Text = (((i - 4000) / 16000) * Convert.ToSingle(Rang.Text)).ToString("F2");
            }
            else
            { AI_OUT.Text = "0"; }
        }

        //private List<byte> buffer = new List<byte>();
        private void 接收1_Tick(object sender, EventArgs e)
        {

            //接收数据
            textBox2.Clear();
            int i;
            int j;
            int Data_Length = 0;

            string str = (Modbus_Text[0]);//赋值字符串
            str = str.Replace(" ", "");//取消所有空格  
            str = str.Replace("\r", "");//取消\r
            str = str.Replace("\n", "");//取消\n
            string[] sArray = str.Split(';', '=', '-');
            /////////////////////////////////////////////////////////////////
            if (form2 != null && !form2.IsDisposed)
            {
                //form2.textBox1.Text += "hello /r/n";
                form2.textBox1.AppendText("hello\r\n");
                // form2.SetTextBoxText("hello");
            }
            else
            {
              //  MessageBox.Show("请先打开窗体2");
            }
            ////////////////////////////////////////////////////////////////////////////////

            try
            {
                if (sp1.BytesToRead != 0)
                {
                    int n = sp1.BytesToRead;//SP = null



                    //try
                    //{

                    byte[] buf = new byte[n];
                    byte[] buf_crc = new byte[n - 2];
                    sp1.Read(buf, 0, n);



                    string[] b = new string[n];
                    for (i = 0; i < n; i++)
                    {
                        //截取字符串
                        b[i] = Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0'); ;

                        textBox2.Text = textBox2.Text + b[i] + " ";
                        //h获取数据长度
                        Data_Length = Convert.ToInt32(b[2], 16);

                        //确保全部字节接收完成

                    }

                    Array.Copy(buf, 0, buf_crc, 0, n - 2);

                    //接收数据校验;
                    byte[] res = crc16(buf_crc);
                    if ((res[0] == buf[n - 2]) && (res[1] == buf[n - 1]))
                    {



                        //数据处理
                        byte[] bytes = new byte[n - 5];
                        //前3后2 清楚
                        for (j = 0; j < n - 5; j++)
                        {
                            //去掉包头前3个字节
                            //bytes[j] = (Convert.ToByte(b[j + 3], 16));
                            bytes[j] = buf[j + 3];

                        }
                        bytes = bytes.Reverse().ToArray();
                        //bytes[0] = 0x00;
                        //bytes[1] = 0x00;
                        //bytes[2] = 0x44;
                        //bytes[3] = 0x44;
                        //bytes[4] = 0x42;
                        //bytes[5] = 0x42;
                        //接收数据数组

                        int p = (n - 5) / 2;
                        int q;
                        //int r;
                        float[] data_rcv = new float[p];

                        for (q = 0; q < p; q++)
                        {
                            data_rcv[q] = BitConverter.ToUInt16(bytes, q * 2);
                        }
                        data_rcv = data_rcv.Reverse().ToArray();

                        //float lll = BitConverter.ToSingle(bytes, 0);
                        //textBox14.Text = data_rcv[0].ToString("F2");

                        //if (data_rcv[0] >= 4000)
                        //{
                        //    textBox14.Text = (((data_rcv[0] - 4000) / 16000) * Convert.ToSingle(textBox13.Text)).ToString("F2");
                        //}
                        //else
                        //{ textBox14.Text = "0"; }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[1])
                        {
                            AI(data_rcv[0], textBox13, textBox14);
                        }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[7])
                        {
                            AI(data_rcv[0], textBox16, textBox17);
                        }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[13])
                        {
                            AI(data_rcv[0], textBox19, textBox20);
                        }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[19])
                        {
                            AI(data_rcv[0], textBox22, textBox23);
                        }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[25])
                        {
                            AI(data_rcv[0], textBox25, textBox26);
                        }
                       
                        
                        
                        
                        
                    }
                    else
                    {
                        return;
                    }
                }
                sp1.DiscardInBuffer();//丢弃来自串行驱动程序的接收缓冲区的数据
            }
            catch { }
         
           
        }  
        private void 接收2_Tick(object sender, EventArgs e)
        {
            //接收数据
            textBox4.Clear();
            int i;
            int j;
            int Data_Length = 0;

            string str = (Modbus_Text[1]);//赋值字符串
            str = str.Replace(" ", "");//取消所有空格  
            str = str.Replace("\r", "");//取消\r
            str = str.Replace("\n", "");//取消\n
            string[] sArray = str.Split(';', '=', '-');
            try
            {
                if (sp2.BytesToRead != 0)
                {
                    int n = sp2.BytesToRead;//SP = null
                    byte[] buf = new byte[n];
                    byte[] buf_crc = new byte[n - 2];
                    sp2.Read(buf, 0, n);



                    string[] b = new string[n];
                    for (i = 0; i < n; i++)
                    {
                        //截取字符串
                        b[i] = Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0'); ;

                        textBox4.Text = textBox4.Text + b[i] + " ";
                        //h获取数据长度
                        Data_Length = Convert.ToInt32(b[2], 16);

                        //确保全部字节接收完成

                    }

                    Array.Copy(buf, 0, buf_crc, 0, n - 2);

                    //接收数据校验;
                    byte[] res = crc16(buf_crc);
                    if ((res[0] == buf[n - 2]) && (res[1] == buf[n - 1]))
                    {



                        //数据处理
                        byte[] bytes = new byte[n - 5];
                        //前3后2 清楚
                        for (j = 0; j < n - 5; j++)
                        {
                            //去掉包头前3个字节
                            //bytes[j] = (Convert.ToByte(b[j + 3], 16));
                            bytes[j] = buf[j + 3];

                        }
                        bytes = bytes.Reverse().ToArray();
                        //bytes[0] = 0x00;
                        //bytes[1] = 0x00;
                        //bytes[2] = 0x44;
                        //bytes[3] = 0x44;
                        //bytes[4] = 0x42;
                        //bytes[5] = 0x42;
                        //接收数据数组

                        int p = (n - 5) / 2;
                        int q;
                        //int r;
                        float[] data_rcv = new float[p];

                        for (q = 0; q < p; q++)
                        {
                            data_rcv[q] = BitConverter.ToUInt16(bytes, q * 2);
                        }
                        data_rcv = data_rcv.Reverse().ToArray();

                        //float lll = BitConverter.ToSingle(bytes, 0);
                        //label14.Text = data_rcv[0].ToString("F2");
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[1])
                        { AI(data_rcv[0], textBox28, textBox29); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[7])
                        { AI(data_rcv[0], textBox31, textBox32); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[13])
                        { AI(data_rcv[0], textBox34, textBox35); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[19])
                        { AI(data_rcv[0], textBox37, textBox38); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[25])
                        { AI(data_rcv[0], textBox40, textBox41); }
                        
                        
                        
                        
                        
                       


                    }
                    else
                    {
                        return;
                    }
                }
                sp2.DiscardInBuffer();//丢弃来自串行驱动程序的接收缓冲区的数据
            }
            catch
            { }

            
        }
        private void 接收3_Tick(object sender, EventArgs e)
        {
            //接收数据
            textBox6.Clear();
            int i;
            int j;
            int Data_Length = 0;

            string str = (Modbus_Text[2]);//赋值字符串
            str = str.Replace(" ", "");//取消所有空格  
            str = str.Replace("\r", "");//取消\r
            str = str.Replace("\n", "");//取消\n
            string[] sArray = str.Split(';', '=', '-');
            try
            {
                if (sp3.BytesToRead != 0)
                {
                    int n = sp3.BytesToRead;//SP = null
                    byte[] buf = new byte[n];
                    byte[] buf_crc = new byte[n - 2];
                    sp3.Read(buf, 0, n);



                    string[] b = new string[n];
                    for (i = 0; i < n; i++)
                    {
                        //截取字符串
                        b[i] = Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0'); ;

                        textBox6.Text = textBox6.Text + b[i] + " ";
                        //h获取数据长度
                        Data_Length = Convert.ToInt32(b[2], 16);

                        //确保全部字节接收完成

                    }

                    Array.Copy(buf, 0, buf_crc, 0, n - 2);

                    //接收数据校验;
                    byte[] res = crc16(buf_crc);
                    if ((res[0] == buf[n - 2]) && (res[1] == buf[n - 1]))
                    {



                        //数据处理
                        byte[] bytes = new byte[n - 5];
                        //前3后2 清楚
                        for (j = 0; j < n - 5; j++)
                        {
                            //去掉包头前3个字节
                            //bytes[j] = (Convert.ToByte(b[j + 3], 16));
                            bytes[j] = buf[j + 3];

                        }
                        bytes = bytes.Reverse().ToArray();
                        //bytes[0] = 0x00;
                        //bytes[1] = 0x00;
                        //bytes[2] = 0x44;
                        //bytes[3] = 0x44;
                        //bytes[4] = 0x42;
                        //bytes[5] = 0x42;
                        //接收数据数组

                        int p = (n - 5) / 2;
                        int q;
                        //int r;
                        float[] data_rcv = new float[p];

                        for (q = 0; q < p; q++)
                        {
                            data_rcv[q] = BitConverter.ToUInt16(bytes, q * 2);
                        }
                        data_rcv = data_rcv.Reverse().ToArray();

                        //float lll = BitConverter.ToSingle(bytes, 0);
                        //label14.Text = data_rcv[0].ToString("F2");

                        
                        
                        
                        

                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[1])
                        { AI(data_rcv[0], textBox43, textBox44); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[7])
                        { AI(data_rcv[0], textBox46, textBox47); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[13])
                        { AI(data_rcv[0], textBox49, textBox50); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[19])
                        { AI(data_rcv[0], textBox52, textBox53); }
                    }
                    else
                    {
                        return;
                    }
                }
                sp3.DiscardInBuffer();//丢弃来自串行驱动程序的接收缓冲区的数据
            }
            catch
            { }
        }
        private void 接收4_Tick(object sender, EventArgs e)
        {
            //接收数据
            textBox8.Clear();
            int i;
            int j;
            int Data_Length = 0;

            string str = (Modbus_Text[3]);//赋值字符串
            str = str.Replace(" ", "");//取消所有空格  
            str = str.Replace("\r", "");//取消\r
            str = str.Replace("\n", "");//取消\n
            string[] sArray = str.Split(';', '=', '-');
            try
            {
                if (sp4.BytesToRead != 0)
                {
                    int n = sp4.BytesToRead;//SP = null
                    byte[] buf = new byte[n];
                    byte[] buf_crc = new byte[n - 2];
                    sp4.Read(buf, 0, n);



                    string[] b = new string[n];
                    for (i = 0; i < n; i++)
                    {
                        //截取字符串
                        b[i] = Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0'); ;

                        textBox8.Text = textBox8.Text + b[i] + " ";
                        //h获取数据长度
                        Data_Length = Convert.ToInt32(b[2], 16);

                        //确保全部字节接收完成

                    }

                    Array.Copy(buf, 0, buf_crc, 0, n - 2);

                    //接收数据校验;
                    byte[] res = crc16(buf_crc);
                    if ((res[0] == buf[n - 2]) && (res[1] == buf[n - 1]))
                    {



                        //数据处理
                        byte[] bytes = new byte[n - 5];
                        //前3后2 清楚
                        for (j = 0; j < n - 5; j++)
                        {
                            //去掉包头前3个字节
                            //bytes[j] = (Convert.ToByte(b[j + 3], 16));
                            bytes[j] = buf[j + 3];

                        }
                        bytes = bytes.Reverse().ToArray();
                        //bytes[0] = 0x00;
                        //bytes[1] = 0x00;
                        //bytes[2] = 0x44;
                        //bytes[3] = 0x44;
                        //bytes[4] = 0x42;
                        //bytes[5] = 0x42;
                        //接收数据数组

                        int p = (n - 5) / 2;
                        int q;
                        //int r;
                        float[] data_rcv = new float[p];

                        for (q = 0; q < p; q++)
                        {
                            data_rcv[q] = BitConverter.ToUInt16(bytes, q * 2);
                        }
                        data_rcv = data_rcv.Reverse().ToArray();

                        //float lll = BitConverter.ToSingle(bytes, 0);
                        //label14.Text = data_rcv[0].ToString("F2");

                        
                        
                        
                        
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[1])
                        { AI(data_rcv[0], textBox55, textBox56); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[7])
                        { AI(data_rcv[0], textBox58, textBox59); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[13])
                        { AI(data_rcv[0], textBox61, textBox62); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[19])
                        { AI(data_rcv[0], textBox64, textBox65); }
                    }
                    else
                    {
                        return;
                    }
                }
                sp4.DiscardInBuffer();//丢弃来自串行驱动程序的接收缓冲区的数据
            }
            catch
            { }
        }
        private void 接收5_Tick(object sender, EventArgs e)
        {
            //接收数据
            textBox10.Clear();
            int i;
            int j;
            int Data_Length = 0;

            string str = (Modbus_Text[4]);//赋值字符串
            str = str.Replace(" ", "");//取消所有空格  
            str = str.Replace("\r", "");//取消\r
            str = str.Replace("\n", "");//取消\n
            string[] sArray = str.Split(';', '=', '-');
            try
            {
                if (sp5.BytesToRead != 0)
                {
                    int n = sp5.BytesToRead;//SP = null
                    byte[] buf = new byte[n];
                    byte[] buf_crc = new byte[n - 2];
                    sp5.Read(buf, 0, n);



                    string[] b = new string[n];
                    for (i = 0; i < n; i++)
                    {
                        //截取字符串
                        b[i] = Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0'); ;

                        textBox10.Text = textBox10.Text + b[i] + " ";
                        //h获取数据长度
                        Data_Length = Convert.ToInt32(b[2], 16);

                        //确保全部字节接收完成

                    }

                    Array.Copy(buf, 0, buf_crc, 0, n - 2);

                    //接收数据校验;
                    byte[] res = crc16(buf_crc);
                    if ((res[0] == buf[n - 2]) && (res[1] == buf[n - 1]))
                    {



                        //数据处理
                        byte[] bytes = new byte[n - 5];
                        //前3后2 清楚
                        for (j = 0; j < n - 5; j++)
                        {
                            //去掉包头前3个字节
                            //bytes[j] = (Convert.ToByte(b[j + 3], 16));
                            bytes[j] = buf[j + 3];

                        }
                        bytes = bytes.Reverse().ToArray();
                        //bytes[0] = 0x00;
                        //bytes[1] = 0x00;
                        //bytes[2] = 0x44;
                        //bytes[3] = 0x44;
                        //bytes[4] = 0x42;
                        //bytes[5] = 0x42;
                        //接收数据数组

                        int p = (n - 5) / 2;
                        int q;
                        //int r;
                        float[] data_rcv = new float[p];

                        for (q = 0; q < p; q++)
                        {
                            data_rcv[q] = BitConverter.ToUInt16(bytes, q * 2);
                        }
                        data_rcv = data_rcv.Reverse().ToArray();

                        //float lll = BitConverter.ToSingle(bytes, 0);
                        //label14.Text = data_rcv[0].ToString("F2");
                        
                        
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[1])
                        { AI(data_rcv[0], textBox67, textBox68); }
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[7])
                        { AI(data_rcv[0], textBox70, textBox71); }

                    }
                    else
                    {
                        return;
                    }
                }
                sp5.DiscardInBuffer();//丢弃来自串行驱动程序的接收缓冲区的数据
            }
            catch
            { }
        }
        private void 接收6_Tick(object sender, EventArgs e)
        {
            //接收数据
            textBox12.Clear();
            int i;
            int j;
            int Data_Length = 0;

            string str = (Modbus_Text[5]);//赋值字符串
            str = str.Replace(" ", "");//取消所有空格  
            str = str.Replace("\r", "");//取消\r
            str = str.Replace("\n", "");//取消\n
            string[] sArray = str.Split(';', '=', '-');
            try
            {
                if (sp6.BytesToRead != 0)
                {
                    int n = sp6.BytesToRead;//SP = null
                    byte[] buf = new byte[n];
                    byte[] buf_crc = new byte[n - 2];
                    sp6.Read(buf, 0, n);



                    string[] b = new string[n];
                    for (i = 0; i < n; i++)
                    {
                        //截取字符串
                        b[i] = Convert.ToString(buf[i], 16).ToUpper().PadLeft(2, '0'); ;

                        textBox12.Text = textBox12.Text + b[i] + " ";
                        //h获取数据长度
                        Data_Length = Convert.ToInt32(b[2], 16);

                        //确保全部字节接收完成

                    }

                    Array.Copy(buf, 0, buf_crc, 0, n - 2);

                    //接收数据校验;
                    byte[] res = crc16(buf_crc);
                    if ((res[0] == buf[n - 2]) && (res[1] == buf[n - 1]))
                    {



                        //数据处理
                        byte[] bytes = new byte[n - 5];
                        //前3后2 清楚
                        for (j = 0; j < n - 5; j++)
                        {
                            //去掉包头前3个字节
                            //bytes[j] = (Convert.ToByte(b[j + 3], 16));
                            bytes[j] = buf[j + 3];

                        }
                        bytes = bytes.Reverse().ToArray();
                        //bytes[0] = 0x00;
                        //bytes[1] = 0x00;
                        //bytes[2] = 0x44;
                        //bytes[3] = 0x44;
                        //bytes[4] = 0x42;
                        //bytes[5] = 0x42;
                        //接收数据数组

                        int p = (n - 5) / 2;
                        int q;
                        //int r;
                        float[] data_rcv = new float[p];

                        for (q = 0; q < p; q++)
                        {
                            data_rcv[q] = BitConverter.ToUInt16(bytes, q * 2);
                        }
                        data_rcv = data_rcv.Reverse().ToArray();

                        //float lll = BitConverter.ToSingle(bytes, 0);
                        //label14.Text = data_rcv[0].ToString("F2");
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[1])
                        {
                            AI(data_rcv[0], textBox73, textBox74);
                        }
                        string a = Convert.ToInt16(b[0], 16).ToString();
                        if (Convert.ToInt16(b[0], 16).ToString() == sArray[7])
                        {
                            AI(data_rcv[0], textBox76, textBox77);
                        }

                    }
                    else
                    {
                        return;
                    }
                }
                sp6.DiscardInBuffer();//丢弃来自串行驱动程序的接收缓冲区的数据
            }
            catch
            { }
        }

        private void label58_Click(object sender, EventArgs e)
        {

        }



        //ArrayList weight1 = new ArrayList();
        //ArrayList weight2 = new ArrayList();
        //ArrayList weight3 = new ArrayList();
        //ArrayList weight4 = new ArrayList();
        //ArrayList weight5 = new ArrayList();
        //ArrayList weight6 = new ArrayList();
        //ArrayList weight7 = new ArrayList();
        //ArrayList weight8 = new ArrayList();
        //ArrayList weight9 = new ArrayList();
        //ArrayList weight10 = new ArrayList();
        //ArrayList weight11 = new ArrayList();
        //ArrayList weight12 = new ArrayList();
        //ArrayList weight13 = new ArrayList();
        //ArrayList weight14 = new ArrayList();
        //ArrayList weight15 = new ArrayList();
        //ArrayList weight16 = new ArrayList();
        //ArrayList weight17 = new ArrayList();
        //ArrayList weight18 = new ArrayList();
        //ArrayList weight19 = new ArrayList();
        //ArrayList weight20 = new ArrayList();
        //ArrayList weight21 = new ArrayList();
        //ArrayList weight22 = new ArrayList();
        float[] value = new float[50];
        //称重数据累计
        public void Weight_LJ(TextBox value, TextBox max, string str, float[] tempArray, int index)
        {
            if (Convert.ToSingle(value.Text) > 1)
            {
                //weight.Add(Convert.ToSingle(value.Text));               
                //weight.Sort();
                if (Convert.ToSingle(value.Text) >= tempArray[index])
                {
                    tempArray[index] = Convert.ToSingle(value.Text);
                }




            }

            if (Convert.ToSingle(value.Text) <= 1)
            {
                try
                {
                    //value1 = Convert.ToSingle(weight[weight.Count - 1]);
                    max.Text = (tempArray[index] + Convert.ToSingle(max.Text)).ToString();

                    //写入数据库  
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    string SQLCOMMANDCount = @"UPDATE  Weight_Data_LJ SET  " + str + " = '" + max.Text + "'";
                    SqlCommand cmd = new SqlCommand(SQLCOMMANDCount, conn);
                    cmd.ExecuteNonQuery();
                    //weight.Clear();

                }
                catch
                {
                    //conn.Dispose();
                    //conn.Close();
                }
            }
        }
        private void textBox13_TextChanged(object sender, EventArgs e)
        {
                     
            Weight_LJ(textBox13, textBox15, "Weight1", value, 0);

        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox16, textBox18, "Weight2", value, 1);

        }

        private void textBox19_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox19, textBox21, "Weight3", value, 2);

        }

        private void textBox22_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox22, textBox24, "Weight4", value, 3);

        }

        private void textBox25_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox25, textBox27, "Weight5", value, 4);

        }

        private void textBox28_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox28, textBox30, "Weight6", value, 5);

        }

        private void textBox31_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox31, textBox33, "Weight7", value, 6);

        }

        private void textBox34_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox34, textBox36, "Weight8", value, 7);

        }

        private void textBox37_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox37, textBox39, "Weight9", value, 8);

        }

        private void textBox40_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox40, textBox42, "Weight10", value, 9);

        }

        private void textBox43_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox43, textBox45, "Weight11", value, 10);

        }

        private void textBox46_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox46, textBox48, "Weight12", value, 11);

        }

        private void textBox49_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox49, textBox51, "Weight13", value, 12);

        }

        private void textBox52_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox52, textBox54, "Weight14", value, 13);

        }

        private void textBox55_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox55, textBox57, "Weight15", value, 14);

        }

        private void textBox58_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox58, textBox60, "Weight16", value, 15);

        }

        private void textBox61_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox61, textBox63, "Weight17", value, 16);

        }

        private void textBox64_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ( textBox64, textBox66, "Weight18", value, 17);

        }

        private void textBox67_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox67, textBox69, "Weight19", value, 18);

        }

        private void textBox70_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox70, textBox72, "Weight20", value, 19);

        }

        private void textBox73_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ(textBox73, textBox75, "Weight21", value, 20);

        }

        private void textBox76_TextChanged(object sender, EventArgs e)
        {
            Weight_LJ( textBox76, textBox78, "Weight22", value, 21);

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Form2 form2 = new Form2();
            //form2.Show();
            //form2.SetTextBoxText("hello");
            if (form2 == null || form2.IsDisposed)
            {
                form2 = new Form2();
                form2.Show();
            }
            else
            {
                form2.Focus();
            }
        }
    }
}
相关推荐
QK_002 小时前
volatile 关键字核心作用
开发语言
Dxy12393102162 小时前
Python Tensor 向量入门:从零理解深度学习的“数据语言“
开发语言·python·深度学习
林森lsjs2 小时前
【日耕一题】3. 通过键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
java·开发语言
雪豹阿伟2 小时前
14.C# —— 虚方法,new/override,密封类,索引器,接口
c#·上位机
yzy852 小时前
数据同步工具 -- syncthing
开发语言
catchadmin2 小时前
PHP 应用 security.txt 漏洞披露实践
开发语言·php
糖果店的幽灵2 小时前
LangChain 1.3 完全教程:从入门到精通-Part 11: Tools(工具系统)
开发语言·langchain·c#
夜勤月2 小时前
AQS 与 ThreadPoolExecutor 深度拆解:JDK 高并发底层设计精髓
android·java·开发语言
luj_17682 小时前
R语言生态优势与学习曲线分析
c语言·开发语言·网络·经验分享·算法