标题:
STM32压力计程序与C#上位机代码
[打印本页]
作者:
HX1342005186
时间:
2019-6-10 18:59
标题:
STM32压力计程序与C#上位机代码
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
作者:
lt32210529
时间:
2019-6-24 19:45
学习学习~~~~~~~~~~~~~~~~~~~~
作者:
channol
时间:
2019-7-13 13:31
学习
~~~~~~~~~~~~~~~~~~~~
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1