標(biāo)題:
STM32壓力計(jì)程序與C#上位機(jī)代碼
[打印本頁(yè)]
作者:
HX1342005186
時(shí)間:
2019-6-10 18:59
標(biāo)題:
STM32壓力計(jì)程序與C#上位機(jī)代碼
stm32上位機(jī)壓力計(jì)上位機(jī)源程序
0.png
(4.38 KB, 下載次數(shù): 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; //設(shè)置Y軸為折線(xiàn)
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
MessageBox.Show("請(qǐng)輸入壓力值");
return;
}
try
{
float num1 = float.Parse(textBox2.Text);
update(num1);
}
catch (Exception ee)
{
MessageBox.Show("上傳失敗\n" + ee.ToString());
Debug.Write(ee.ToString());
}
}
//打來(lái)畫(huà)圖定時(shí)器
private void button4_Click(object sender, EventArgs e)
{
timer1.Start();
}
//畫(huà)圖
private void timer1_Tick(object sender, EventArgs e)
{
//從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)
GetRecord();
//更新曲線(xiàn)
chart1.Series.Clear();
ss.Points.DataBindY(data);
chart1.Series.Add(ss);
}
//打開(kāi)串口
private void button1_Click(object sender, EventArgs e)
{
try
{
port1.Open();
}
catch (Exception ee)
{
MessageBox.Show("串口打開(kāi)失敗\n" + ee.ToString());
}
Thread.Sleep(500);
timer2.Start();
}
//定時(shí)器定時(shí)讀取串口緩沖區(qū)數(shù)據(jù)
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]);
//上傳到數(shù)據(jù)庫(kù)
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;
}
//向下位機(jī)發(fā)送控制命令
port1.Write(command, 0, command.Length);
}
catch (Exception ee)
{
Debug.WriteLine(ee.ToString());
}
}
}
//上傳數(shù)據(jù)庫(kù)
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); //創(chuàng)建OleDbConnection對(duì)象,連接
con.Open();
string strSql = "Insert Into temp(yali) Values('" + newv.ToString() + "')";
OleDbCommand oleDbCommand = new OleDbCommand(strSql, con);
oleDbCommand.ExecuteNonQuery();
con.Close();//關(guān)閉連接
}
catch (Exception ee)
{
Debug.WriteLine(ee.ToString());
}
}
//從數(shù)據(jù)庫(kù)獲取最新的數(shù)據(jù)
public void GetRecord()
{
try
{
string reportPath = "temp.accdb";
string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source=" + reportPath;
OleDbConnection con = new OleDbConnection(ConStr); //創(chuàng)建OleDbConnection對(duì)象,連接
con.Open();
string sql = "select top 50 * from temp order by ID desc ";
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(sql, con); //創(chuàng)建適配對(duì)象
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());
}
}
}
}
復(fù)制代碼
單片機(jī)源程序如下:
#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); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級(jí),2位響應(yīng)優(yōu)先級(jí)
delay_init(); //延時(shí)函數(shù)初始化
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(); //稱(chēng)毛皮重量
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;//得到此次接收到的數(shù)據(jù)長(zhǎng)度
if(USART_RX_BUF[0] == 0x01)
{
BUZZER_ON();
LED_ON();
}
else
{
BUZZER_OFF();
LED_OFF();
}
USART_RX_STA=0;
}
}
}
復(fù)制代碼
所有資料51hei提供下載:
下位機(jī)-壓力計(jì).7z
(226.99 KB, 下載次數(shù): 26)
2019-6-11 03:26 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
coursedesign_1012.zip
(206.44 KB, 下載次數(shù): 31)
2019-6-10 18:58 上傳
點(diǎn)擊文件名下載附件
上位機(jī)代碼
下載積分: 黑幣 -5
作者:
lt32210529
時(shí)間:
2019-6-24 19:45
學(xué)習(xí)學(xué)習(xí)~~~~~~~~~~~~~~~~~~~~
作者:
channol
時(shí)間:
2019-7-13 13:31
學(xué)習(xí)
~~~~~~~~~~~~~~~~~~~~
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1