找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2254|回复: 2
打印 上一主题 下一主题
收起左侧

STM32压力计程序与C#上位机代码

[复制链接]
跳转到指定楼层
楼主
stm32上位机压力计上位机源程序


C#源程序:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using Microsoft.Win32;
  11. using System.Text.RegularExpressions;
  12. using System.Data.SqlClient;
  13. using System.Threading;
  14. using System.Data.OleDb;
  15. using System.Windows.Forms.DataVisualization.Charting;
  16. using System.IO.Ports;//

  17. namespace coursedesign
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         const float high =1700;
  22.         const float low =1400;

  23.         double[] data = new double[50];
  24.         Series ss = new Series("压力");

  25.         public SerialPort port1 = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);

  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.         }

  30.         
  31.         private void Form1_Load(object sender, EventArgs e)
  32.         {
  33.             chart1.Series.Clear();
  34.             ss.ChartType = SeriesChartType.Spline;   //设置Y轴为折线
  35.             chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
  36.         }


  37.         private void button3_Click(object sender, EventArgs e)
  38.         {
  39.             if (textBox2.Text == "")
  40.             {
  41.                 MessageBox.Show("请输入压力值");
  42.                 return;
  43.             }

  44.             try
  45.             {
  46.                 float num1 = float.Parse(textBox2.Text);
  47.                 update(num1);
  48.             }
  49.             catch (Exception ee)
  50.             {
  51.                 MessageBox.Show("上传失败\n" + ee.ToString());
  52.                 Debug.Write(ee.ToString());
  53.             }            
  54.         }

  55.         //打来画图定时器
  56.         private void button4_Click(object sender, EventArgs e)
  57.         {
  58.             timer1.Start();
  59.         }


  60.         //画图
  61.         private void timer1_Tick(object sender, EventArgs e)
  62.         {
  63.             //从数据库读取数据
  64.             GetRecord();

  65.             //更新曲线
  66.             chart1.Series.Clear();
  67.             ss.Points.DataBindY(data);
  68.             chart1.Series.Add(ss);
  69.         }

  70.         //打开串口
  71.         private void button1_Click(object sender, EventArgs e)
  72.         {
  73.             try
  74.             {
  75.                 port1.Open();
  76.             }
  77.             catch (Exception ee)
  78.             {
  79.                 MessageBox.Show("串口打开失败\n" + ee.ToString());
  80.             }

  81.             Thread.Sleep(500);
  82.             timer2.Start();
  83.         }

  84.         //定时器定时读取串口缓冲区数据
  85.         private void timer2_Tick(object sender, EventArgs e)
  86.         {
  87.             if (port1.IsOpen)
  88.             {
  89.                 byte[] data = new byte[0];

  90.                 int iByteLen = port1.BytesToRead;
  91.                 Debug.WriteLine(iByteLen.ToString());

  92.                 try
  93.                 {
  94.                     if (iByteLen > 0)
  95.                     {
  96.                         data = new byte[iByteLen];
  97.                         port1.Read(data, 0, iByteLen);
  98.                     }

  99.                     String received = Encoding.ASCII.GetString(data);
  100.                     Debug.WriteLine(received);
  101.                     textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  " + Encoding.ASCII.GetString(data) + "\n");

  102.                     byte[] command = { 0x02, 0x33, 0x3c, 0x0d, 0x0a };
  103.                     string[] sa = received.Split(':');
  104.                     string[] svalue = sa[1].Split(' ');
  105.                     float value = float.Parse(svalue[0]);

  106.                     //上传到数据库
  107.                     update(value);

  108.                     Image l1 = Image.FromFile("level1.jpg");
  109.                     Image l2 = Image.FromFile("level2.jpg");
  110.                     Image l3 = Image.FromFile("level3.jpg");

  111.                     if (value > high)
  112.                     {
  113.                         pictureBox2.Image = l1;
  114.                         command[0] = 0x01;

  115.                     }
  116.                     else if (value > low)
  117.                     {
  118.                         pictureBox2.Image = l2;
  119.                     }
  120.                     else
  121.                     {
  122.                         pictureBox2.Image = l3;
  123.                         command[0] = 0x01;
  124.                     }

  125.                     //向下位机发送控制命令
  126.                     port1.Write(command, 0, command.Length);

  127.                 }
  128.                 catch (Exception ee)
  129.                 {
  130.                     Debug.WriteLine(ee.ToString());
  131.                 }
  132.             }
  133.         }

  134.         //上传数据库
  135.         private void update(double newv)
  136.         {
  137.             try
  138.             {
  139.                 string reportPath = "temp.accdb";
  140.                 string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=" + reportPath;
  141.                 OleDbConnection con = new OleDbConnection(ConStr); //创建OleDbConnection对象,连接
  142.                 con.Open();

  143.                 string strSql = "Insert Into temp(yali) Values('" + newv.ToString() + "')";
  144.                 OleDbCommand oleDbCommand = new OleDbCommand(strSql, con);
  145.                 oleDbCommand.ExecuteNonQuery();

  146.                 con.Close();//关闭连接
  147.             }
  148.             catch (Exception ee)
  149.             {
  150.                 Debug.WriteLine(ee.ToString());
  151.             }
  152.         }

  153.         //从数据库获取最新的数据
  154.         public void GetRecord()
  155.         {
  156.             try
  157.             {
  158.                 string reportPath = "temp.accdb";
  159.                 string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=" + reportPath;
  160.                 OleDbConnection con = new OleDbConnection(ConStr); //创建OleDbConnection对象,连接
  161.                 con.Open();

  162.                 string sql = "select top 50 * from temp order by ID desc ";

  163.                 OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, con); //创建适配对象
  164.                 DataTable dt = new DataTable();
  165.                 dbDataAdapter.Fill(dt);

  166.                 int i = 49;
  167.                 foreach (DataRow item in dt.Rows)
  168.                 {
  169.                     data[i] = double.Parse(item[1].ToString());
  170.                     i--;
  171.                 }

  172.             }
  173.             catch (Exception ee)
  174.             {
  175.                 Debug.WriteLine(ee.ToString());
  176.             }

  177.         }

  178.     }
  179. }
复制代码



单片机源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "sys.h"
  4. #include "usart.h"
  5. #include "lcd.h"
  6. #include "timer.h"
  7. #include "HX711.h"


  8. /********电路连接*****/

  9. //PC1   <---->   Buzzer
  10. //PC4   <---->   SCK
  11. //PC5   <---->   DOUT

  12. int main(void)
  13. {
  14.         u8 dis[30];         
  15.   u8 len;
  16.        
  17.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);         //设置NVIC中断分组2:2位抢占优先级,2位响应优先级

  18.         delay_init();                     //延时函数初始化          
  19.         uart_init(9600);                 //串口初始化为9600
  20.         LED_Init();                                  //初始化与LED连接的硬件接口
  21.         LCD_Init();
  22.         POINT_COLOR=RED;
  23.   LCD_Clear(WHITE);         
  24.         BUZZER_ON();
  25.         delay_ms(2000);
  26.          BUZZER_OFF();
  27.   printf("SUCCESS\n");
  28.        
  29.         Init_HX711pin();
  30.         Get_Maopi();                                //称毛皮重量
  31.         delay_ms(1000);
  32.         delay_ms(1000);
  33.         Get_Maopi();                                //重新获取毛皮重量
  34.        
  35.         POINT_COLOR=RED;          
  36.         LCD_ShowString(30,40,200,24,24,"C# & STM32");
  37.         LCD_ShowString(30,70,200,24,24,"Pressure Gauge");  
  38.         POINT_COLOR=BLACK;
  39.         LCD_ShowString(30,110,200,16,16,"Weight(g):");                //
  40.        
  41.         while(1)
  42.         {                                
  43.                
  44.                 Get_Weight();
  45.                
  46.                 LCD_ShowxNum(90,140,Weight_Shiwu,4, 16, 0);
  47.                 printf("Weight:%d g\r\n",Weight_Shiwu); //打印

  48.                
  49.                 delay_ms(700);       
  50.                
  51.                 //接收指令处理
  52.                 if(USART_RX_STA&0x8000)
  53.                 {                                          
  54.                         len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度

  55.                         if(USART_RX_BUF[0] == 0x01)
  56.                         {
  57.                                 BUZZER_ON();
  58.                                 LED_ON();
  59.                         }
  60.                         else
  61.                         {
  62.                                 BUZZER_OFF();
  63.                                 LED_OFF();
  64.                         }
  65.                        
  66.                         USART_RX_STA=0;
  67.                 }

  68.         }
  69. }
复制代码

所有资料51hei提供下载:
下位机-压力计.7z (226.99 KB, 下载次数: 26)
coursedesign_1012.zip (206.44 KB, 下载次数: 31)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:561245 发表于 2019-6-24 19:45 | 只看该作者
学习学习~~~~~~~~~~~~~~~~~~~~
回复

使用道具 举报

板凳
ID:558459 发表于 2019-7-13 13:31 | 只看该作者
学习~~~~~~~~~~~~~~~~~~~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表