通过RS_232串口向某检测台握手,发送和接受数据源代码

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.Windows;
using System.Data.SqlClient;
namespace 通讯源码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 
        private void UpdateTextBox(string text)
        {
            this.textBox1.Text += text;
        }

        byte[] d = new byte[1];

        byte[] b = new byte[1];

        byte[] r = new byte[1];

        byte[] data = new byte[100];

        string[] recive_data = new string[20];  
       
        bool IsCom = false;

        private void button1_Click(object sender, EventArgs e)
        {
            int k =1;

          while (k == 1)
           {
              
                   IsCom = this.comm();
              

                if (IsCom)
                {
                    try
                    {
                        this.Receive();
                    }
                    catch
                    {
                        MessageBox.Show("接受数据失败");
                   
                    }
                  
                  this.textBox1.Text =recive_data[0];

                  this.textBox2.Text = recive_data[2];

                  this.textBox3.Text =recive_data[6];

                  this.textBox4.Text =recive_data[10];

                  this.textBox5.Text =recive_data[12];

                  this.textBox6.Text =recive_data[16];

                    if ((recive_data[0] == "0") && (recive_data[3] == "0"))
                    {
                        MessageBox.Show("信息不全");
                    }
 
                    this.InsertDataBase(recive_data);

                    DialogResult result = MessageBox.Show("继续", "hello", MessageBoxButtons.OKCancel);

                    if (result == DialogResult.OK)
                    {                       

                        for (int flu = 0; flu < 20; flu++)
                        {
                            recive_data[flu] = null;
                        }

                        textBox1.Clear();

                        textBox2.Clear();

                        textBox3.Clear();

                        textBox4.Clear();

                        textBox5.Clear();

                        textBox6.Clear();

                    }
                    else
                    {
                        Application.Exit();

                        k = 0;
                    }
                }
            }


        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            for (int flu = 0; flu < 20; flu++)
            {
                recive_data[flu] = null;
            }     

            this.serialPort1.DataBits = 8;

            this.serialPort1.Open();

        }
//握手
        public bool comm()
        {
            int m = 1;

            while (m == 1)
            {

                int i = 1;

                while (i <= 4)
                {

                    b[0] = 173;

                    this.serialPort1.Write(b, 0, b.Length);

                    int delay = 0;

                    while (delay <= 12000000) { delay++; }

                    i++;
            
                }
                int delay1 = 0;

                while (delay1 <= 24000000) { delay1++; }


                //发送1
                b[0] = 1;

                r[0] = 0;

                this.serialPort1.DiscardInBuffer();

                this.serialPort1.DiscardOutBuffer();

                this.serialPort1.Write(b, 0, b.Length);

                int time = 0;

                while ((b[0] != r[0]) && (time <= 10))
                {

                    try
                    {
                        this.serialPort1.Read(r, 0, r.Length);
                    }
                    catch { }

                    time++;              

                }
                if (b[0] == r[0])
                {
//发送4
                    b[0] = 4;

                    r[0] = 0;

                    this.serialPort1.DiscardInBuffer();

                    this.serialPort1.DiscardOutBuffer();

                    this.serialPort1.Write(b, 0, b.Length);

                    int time1 = 0;

                    while ((b[0] != r[0]) && (time1 <= 10))
                    {

                        try
                        {
                            this.serialPort1.Read(r, 0, r.Length);
                        }
                        catch { }

                        time1++;
                        if (time1 == 11)
                        {
                            MessageBox.Show("读4失败");

                            m = 0;
                       
                        }
                    }

                    if (b[0] != r[0])
                    {

                        //                      MessageBox.Show("失败");

                        //                      Application.Exit();
                    }
                    else
                    {
                        this.serialPort1.Write(b, 0, b.Length);


                        //                      MessageBox.Show("接受数据");

                    }
       
                    m = 0;

                    IsCom = true;                 
//发送4结束                 
                  
                }

                delay1 = 0;

                while (delay1 <= 96000000) { delay1++; }

            }

            return IsCom;
        }
//接受数据
        public void Receive()
        {

            //接受数据

          

            int p = 1;

            while (p <= 74)
            {
                r[0] = 0;

                this.serialPort1.DiscardInBuffer();

                this.serialPort1.DiscardOutBuffer();

                int flag = 0;

                int time2 = 0;

                while ((time2 <= 10) && (flag == 0))
                {
                    try
                    {
                        flag = this.serialPort1.Read(r, 0, r.Length);
                    }
                    catch { }

                    time2++;

                }
                /*                              if (flag == 0)
                                              {
                                                  MessageBox.Show("通讯失败");

                                                  Application.Exit();
                                              }
                 */

                data[p] = r[0];

                this.serialPort1.Write(r, 0, r.Length);

                p++;
            }

            //提取表号                                    

            for (int i = 1; i <= 6; i++)
            {
                recive_data[0] += Convert.ToString(data[i], 16);

            }

            //测试点忽略,数据流量,仪表,表准读数,实测误差,压力补偿,基本误差,仪表系数 指标1-7
            //3个字节一个数据
            int j = 1;

            for (int i = 8; i <= 28; i = i + 3)
            {
                recive_data[j] = this.c_f(data[i], data[i + 1], data[i + 2]).ToString();

                j++;
            }
            // 工号 指标8
            for (int i = 29; i <= 30; i++)
            {
                recive_data[8] = Convert.ToString(data[i], 16);
            }

            //字节31启动方式,32停止方式,33字节Qmax                     
            recive_data[9] = Convert.ToString(data[33], 16);

/*            this.textBox2.Text = recive_data[0];

            this.textBox3.Text = recive_data[1];

            this.textBox4.Text = recive_data[2];

            this.textBox5.Text = recive_data[3];

            this.textBox6.Text = recive_data[4];

            this.textBox7.Text = recive_data[5];

            this.textBox8.Text = recive_data[6];

            this.textBox9.Text = recive_data[7];

            this.textBox10.Text = recive_data[8];

            this.textBox11.Text = recive_data[9];
 */

            //接受第二只表数据(34字节1,35字节8)

            //接受表号(36-41字节)          
            for (int i = 36; i <= 41; i++)
            {
                recive_data[10] += Convert.ToString(data[i], 16);

            }
            //测试点(第42个字节)忽略,数据流量,仪表,表准读数,实测误差,压力补偿,基本误差,仪表系数 (43-63字节)
            //3个字节一个数据
            j = 11;

            for (int i = 43; i <= 63; i = i + 3)
            {
                recive_data[j] = this.c_f(data[i], data[i + 1], data[i + 2]).ToString();

                j++;
            }
            //工号 64-65字节
            for (int i = 64; i <= 65; i++)
            {
                recive_data[18] = Convert.ToString(data[i], 16);
            }
            字节66启动方式,67停止方式,68字节Qmax                     
            recive_data[19] = Convert.ToString(data[68], 16);
         
        }

    
        public double c_f(byte cbyte1, byte cbyte2, byte cbyte3)
        {
            double x;

            int byte1 = (char)cbyte1;

            int byte2 = (char)cbyte2;

            int byte3 = (char)cbyte3;

 

            if (byte1 >= 128)
            {
                byte1 = byte1 - 128;

                if (byte1 >= 64)
                {
                    byte1 = 128 - byte1;

                    x = -(byte2 / 256.0 + byte3 / 65536.0) * Math.Exp(-byte1 * Math.Log(2.0) / Math.Log(2.718281828));
                }
                else
                {
                    x = -(byte2 / 256.0 + byte3 / 65536.0) * Math.Exp(+byte1 * Math.Log(2.0) / Math.Log(2.718281828));

                }
            }
            else if (byte1 >= 64)
            {
                byte1 = 128 - byte1;

                x = +(byte2 / 256.0 + byte3 / 65536.0) * Math.Exp(+byte1 * Math.Log(2.0) / Math.Log(2.718281828));
            }
            else
            {
                x = +(byte2 / 256.0 + byte3 / 65536.0) * Math.Exp(+byte1 * Math.Log(2.0) / Math.Log(2.718281828));
            }

            return x;
        }

        private void InsertDataBase(string[] InsertData)
        {
            string meterNo = InsertData[0];

            string liuLiang = InsertData[1];

            string meterDushu = InsertData[2];

            string biaoZhunDuShu = InsertData[3];

            string shicewucha = InsertData[4];

            string yaliBuchang = InsertData[5];

            string jiBenWucha = InsertData[6];

            string yibiaoxishu = InsertData[7];

            string gonghao = InsertData[8];

            string Qmax = InsertData[9];

            string sqlconn = " Data Source=localhost;Initial Catalog=TonBenERP;Integrated Security=True";

            SqlConnection conn = new SqlConnection(sqlconn);

            conn.Open();
                             
// 第二表  
            string meterNo1 = InsertData[10];

            string liuLiang1 = InsertData[11];

            string meterDushu1 = InsertData[12];

            string biaoZhunDuShu1 = InsertData[13];

            string shicewucha1 = InsertData[14];

            string yaliBuchang1 = InsertData[15];

            string jiBenWucha1 = InsertData[16];

            string yibiaoxishu1 = InsertData[17];

            string gonghao1 = InsertData[18];

            string Qmax1 = InsertData[19];

            string sqlstr = "insert into GongKuangCeShi(meterno,liuliang,meterDushu,biaozhundushu,shicewucha,yalibuchang,jibenbuchang,yibiaoxishu,gonghao,qmax) values (" + meterNo + "," + liuLiang + "," + meterDushu + "," + biaoZhunDuShu + "," + shicewucha + "," + yaliBuchang + "," + jiBenWucha + "," + yibiaoxishu + "," + gonghao + "," + Qmax + ")";

            string sqlstr1 = "insert into GongKuangCeShi(meterno,liuliang,meterDushu,biaozhundushu,shicewucha,yalibuchang,jibenbuchang,yibiaoxishu,gonghao,qmax) values (" + meterNo1 + "," + liuLiang1 + "," + meterDushu1 + "," + biaoZhunDuShu1 + "," + shicewucha + "," + yaliBuchang + "," + jiBenWucha1 + "," + yibiaoxishu1 + "," + gonghao1 + "," + Qmax1 + ")";

            SqlCommand cmd = new SqlCommand(sqlstr,conn);

            SqlCommand cmd1 = new SqlCommand(sqlstr1,conn);

            cmd.ExecuteNonQuery();

            cmd1.ExecuteNonQuery();

            conn.Close();


        }

      
      

    
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值