stm32上位機壓力計上位機源程序
0.png (4.38 KB, 下載次數: 31)
下載附件
2019-6-11 03:24 上傳
C#源程序:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- using Microsoft.Win32;
- using System.Text.RegularExpressions;
- using System.Data.SqlClient;
- using System.Threading;
- using System.Data.OleDb;
- using System.Windows.Forms.DataVisualization.Charting;
- using System.IO.Ports;//
- namespace coursedesign
- {
- public partial class Form1 : Form
- {
- const float high =1700;
- const float low =1400;
- double[] data = new double[50];
- Series ss = new Series("壓力");
- public SerialPort port1 = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- chart1.Series.Clear();
- ss.ChartType = SeriesChartType.Spline; //設置Y軸為折線
- chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (textBox2.Text == "")
- {
- MessageBox.Show("請輸入壓力值");
- return;
- }
- try
- {
- float num1 = float.Parse(textBox2.Text);
- update(num1);
- }
- catch (Exception ee)
- {
- MessageBox.Show("上傳失敗\n" + ee.ToString());
- Debug.Write(ee.ToString());
- }
- }
- //打來畫圖定時器
- private void button4_Click(object sender, EventArgs e)
- {
- timer1.Start();
- }
- //畫圖
- private void timer1_Tick(object sender, EventArgs e)
- {
- //從數據庫讀取數據
- GetRecord();
- //更新曲線
- chart1.Series.Clear();
- ss.Points.DataBindY(data);
- chart1.Series.Add(ss);
- }
- //打開串口
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- port1.Open();
- }
- catch (Exception ee)
- {
- MessageBox.Show("串口打開失敗\n" + ee.ToString());
- }
- Thread.Sleep(500);
- timer2.Start();
- }
- //定時器定時讀取串口緩沖區數據
- private void timer2_Tick(object sender, EventArgs e)
- {
- if (port1.IsOpen)
- {
- byte[] data = new byte[0];
- int iByteLen = port1.BytesToRead;
- Debug.WriteLine(iByteLen.ToString());
- try
- {
- if (iByteLen > 0)
- {
- data = new byte[iByteLen];
- port1.Read(data, 0, iByteLen);
- }
- String received = Encoding.ASCII.GetString(data);
- Debug.WriteLine(received);
- textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + Encoding.ASCII.GetString(data) + "\n");
- byte[] command = { 0x02, 0x33, 0x3c, 0x0d, 0x0a };
- string[] sa = received.Split(':');
- string[] svalue = sa[1].Split(' ');
- float value = float.Parse(svalue[0]);
- //上傳到數據庫
- update(value);
- Image l1 = Image.FromFile("level1.jpg");
- Image l2 = Image.FromFile("level2.jpg");
- Image l3 = Image.FromFile("level3.jpg");
- if (value > high)
- {
- pictureBox2.Image = l1;
- command[0] = 0x01;
- }
- else if (value > low)
- {
- pictureBox2.Image = l2;
- }
- else
- {
- pictureBox2.Image = l3;
- command[0] = 0x01;
- }
- //向下位機發送控制命令
- port1.Write(command, 0, command.Length);
- }
- catch (Exception ee)
- {
- Debug.WriteLine(ee.ToString());
- }
- }
- }
- //上傳數據庫
- private void update(double newv)
- {
- try
- {
- string reportPath = "temp.accdb";
- string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=" + reportPath;
- OleDbConnection con = new OleDbConnection(ConStr); //創建OleDbConnection對象,連接
- con.Open();
- string strSql = "Insert Into temp(yali) Values('" + newv.ToString() + "')";
- OleDbCommand oleDbCommand = new OleDbCommand(strSql, con);
- oleDbCommand.ExecuteNonQuery();
- con.Close();//關閉連接
- }
- catch (Exception ee)
- {
- Debug.WriteLine(ee.ToString());
- }
- }
- //從數據庫獲取最新的數據
- public void GetRecord()
- {
- try
- {
- string reportPath = "temp.accdb";
- string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=" + reportPath;
- OleDbConnection con = new OleDbConnection(ConStr); //創建OleDbConnection對象,連接
- con.Open();
- string sql = "select top 50 * from temp order by ID desc ";
- OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, con); //創建適配對象
- DataTable dt = new DataTable();
- dbDataAdapter.Fill(dt);
- int i = 49;
- foreach (DataRow item in dt.Rows)
- {
- data[i] = double.Parse(item[1].ToString());
- i--;
- }
- }
- catch (Exception ee)
- {
- Debug.WriteLine(ee.ToString());
- }
- }
- }
- }
復制代碼
單片機源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "timer.h"
- #include "HX711.h"
- /********電路連接*****/
- //PC1 <----> Buzzer
- //PC4 <----> SCK
- //PC5 <----> DOUT
- int main(void)
- {
- u8 dis[30];
- u8 len;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
-
- delay_init(); //延時函數初始化
- uart_init(9600); //串口初始化為9600
- LED_Init(); //初始化與LED連接的硬件接口
- LCD_Init();
- POINT_COLOR=RED;
- LCD_Clear(WHITE);
- BUZZER_ON();
- delay_ms(2000);
- BUZZER_OFF();
- printf("SUCCESS\n");
-
- Init_HX711pin();
- Get_Maopi(); //稱毛皮重量
- delay_ms(1000);
- delay_ms(1000);
- Get_Maopi(); //重新獲取毛皮重量
-
- POINT_COLOR=RED;
- LCD_ShowString(30,40,200,24,24,"C# & STM32");
- LCD_ShowString(30,70,200,24,24,"Pressure Gauge");
- POINT_COLOR=BLACK;
- LCD_ShowString(30,110,200,16,16,"Weight(g):"); //
-
- while(1)
- {
-
- Get_Weight();
-
- LCD_ShowxNum(90,140,Weight_Shiwu,4, 16, 0);
- printf("Weight:%d g\r\n",Weight_Shiwu); //打印
-
- delay_ms(700);
-
- //接收指令處理
- if(USART_RX_STA&0x8000)
- {
- len=USART_RX_STA&0x3fff;//得到此次接收到的數據長度
- if(USART_RX_BUF[0] == 0x01)
- {
- BUZZER_ON();
- LED_ON();
- }
- else
- {
- BUZZER_OFF();
- LED_OFF();
- }
-
- USART_RX_STA=0;
- }
- }
- }
復制代碼
所有資料51hei提供下載:
下位機-壓力計.7z
(226.99 KB, 下載次數: 26)
2019-6-11 03:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
coursedesign_1012.zip
(206.44 KB, 下載次數: 31)
2019-6-10 18:58 上傳
點擊文件名下載附件
上位機代碼 下載積分: 黑幣 -5
|