沈明明
级别: 正式会员
精华主题: 0
发帖数量: 9 个
工控威望: 90 点
下载积分: 475 分
在线时间: 5(小时)
注册时间: 2018-03-28
最后登录: 2024-08-20
查看沈明明的 主题 / 回贴
楼主  发表于: 2018-05-27 16:18
图片:
图片:
本人新手,SetDevice()函数不会用,图中字体变蓝说明赋值不成功
fengyyy
级别: 家园常客
精华主题: 0
发帖数量: 126 个
工控威望: 511 点
下载积分: 2680 分
在线时间: 275(小时)
注册时间: 2015-12-02
最后登录: 2024-09-17
查看fengyyy的 主题 / 回贴
1楼  发表于: 2018-05-28 21:57
自己做了个小程序玩的,代码写的不严谨,没改控件名。我没用这个控件,我是直接写协议的。FX3U+232BD+4AD。实时读取采集的温度,存入SQL数据库中。
  
            for (int i=0; i<99;i++ )
             {
              comboBox1.Items.Add("COM"+i.ToString ());
             }
           foreach (string com in comboBox1.Items  )
           {
               try
               {
                   serialPort1.PortName = com;
                   serialPort1.Open();
                   if(serialPort1 .IsOpen ==true)
                   {
                       break;
                   }
               }
               catch
               {
                   continue;
               }

           }
           timer1.Enabled = true;
           comboBox1.SelectedIndex = 0;










private void timer2_Tick(object sender, EventArgs e)//D0 读取
        {

            try
            {
                textBox1.Text = W_Reveive(serialPort1.ReadExisting());

                timer3.Enabled = true;
                timer2.Enabled = false;
            }
            catch(Exception ex)
            {
                timer1.Enabled = false;
                timer2.Enabled = false;
                timer3.Enabled = false;
                timer3.Enabled = false;
                timer4.Enabled = false;
                timer5.Enabled = false;
                timer6.Enabled = false;

                MessageBox.Show(ex.Message.ToString());
                this.Close();
            }



private string W_Reveive(string text)//字读取
        {

            try
            {
                if (text.Length == 12)
                {
                    string a = text.Substring(1, 4);
                    string b = text.Substring(5, 4);
                    if (a == "00FF" && b != "0000")
                    {
                        int c = Convert.ToInt32(b, 16);
                        return c.ToString();

                    }
                    else
                    {
                        return null;
                    }

                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return"";
            }
         }

private void timer9_Tick(object sender, EventArgs e)//读取表内容
        {
           string conn = "server=.;database=wenducaiji;integrated Security=True;";
            string Sql = "select  time as '时间',temperature as '温度' from temperature order by  time desc,temperature desc;";
            using(SqlConnection con =new SqlConnection(conn))
            {
                con.Open();
                DataSet dataset=new DataSet ();
                SqlDataAdapter dataadapter = new SqlDataAdapter(Sql,con);
                dataadapter.Fill(dataset);
                dataGridView1.DataSource = dataset.Tables[0];
                dataGridView1.Columns["时间"].DefaultCellStyle.Format = "yyyy/MM/dd  HH:mm:ss";
                dataadapter.Dispose();
            }
          
            button6.Visible= true;
        }