上位机显示效果:
上位机代码:
(只有一个窗口)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Queue<double> dataQueue = new Queue<double>(50);
private float Temp = 0;
private int num = 5;
private delegate void SafeCallDelegate(string text);
byte[] Arrive_time = new byte[3];
public Form1()
{
InitializeComponent();
InitChart();
serialPort1.Encoding = Encoding.GetEncoding("GB2312");
Control.CheckForIllegalCrossThreadCalls = false;
x = this.Width;
y = this.Height;
setTag(this);
this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 10; i++)
{
comboBox1.Items.Add("COM" + i.ToString());
}
}
private void button1_Click_1(object sender, EventArgs e)
{
if (open_serial_button.Text == "打开串口")
{
try
{
serialPort1.Open();
comboBox1.Enabled = false;
comboBox2.Enabled = false;
serial.ForeColor = Color.Red;
label2.ForeColor = Color.Red;
open_serial_button.Text = "关闭串口";
this.timer1.Start();
}
catch
{
MessageBox.Show("端口打开错误,请检查串口", "错误");
}
}
else
{
try
{
serialPort1.Close();
open_serial_button.Text = "打开串口";
serial.ForeColor = Color.Black;
label2.ForeColor = Color.Black;
comboBox1.Enabled = true;
comboBox2.Enabled = true;
this.timer1.Stop();
}
catch
{
MessageBox.Show("关闭串口错误");
}
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte[] data = new byte[serialPort1.ReadBufferSize];
serialPort1.Read(data, 0, serialPort1.ReadBufferSize);
string str = Encoding.Default.GetString(data);
WriteTextSafe(str);
if (str.Contains("MLX90614"))
{
string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");
Temp = Convert.ToInt32(result)% 10000;
Temp = Temp/100 + (float)(Temp%100/100);
Arrive_time[2] = 1;
}
}
private void WriteTextSafe(string text)
{
string str = DateTime.Now.ToString("\r\nHH:mm:ss->");
if (this.textBox_receive.InvokeRequired)
{
var d = new SafeCallDelegate(WriteTextSafe);
if (Arrive_time[0] == 1)
{
this.Invoke(d, new object[] { str });
}
this.Invoke(d, new object[] { text });
}
else
{
textBox_receive.AppendText(text + " ");
}
}
private void OnResizeEnd(object sender, EventArgs e)
{
}
private float x;
private float y;
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
if (con.Controls.Count > 0)
{
setTag(con);
}
}
}
private void setControls(float newx, float newy, Control cons)
{
foreach (Control con in cons.Controls)
{
if (con.Tag != null)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);
con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);
con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);
con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);
Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;
con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0)
{
setControls(newx, newy, con);
}
}
}
}
private void Form1_Resize(object sender, EventArgs e)
{
float newx = (this.Width) / x;
float newy = (this.Height) / y;
setControls(newx, newy, this);
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
serialPort1.PortName = comboBox1.Text.ToString();
}
private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
{
string str;
str = comboBox2.Text.ToString();
serialPort1.BaudRate = Convert.ToInt32(str);
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
Arrive_time[0] = 1;
}
else if (checkBox2.Checked == false)
{
Arrive_time[0] = 0;
}
}
private void chart1_Click(object sender, EventArgs e)
{
}
private void textBox_receive_TextChanged(object sender, EventArgs e)
{
}
private void InitChart()
{
this.chart1.ChartAreas.Clear();
ChartArea chartArea1 = new ChartArea("C1");
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Series.Clear();
Series series1 = new Series("温度");
series1.ChartArea = "C1";
this.chart1.Series.Add(series1);
this.chart1.ChartAreas[0].AxisY.Minimum = 0;
this.chart1.ChartAreas[0].AxisY.Maximum = 50;
this.chart1.ChartAreas[0].AxisX.Interval = num;
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
this.chart1.Titles.Clear();
this.chart1.Titles.Add("S01");
this.chart1.Titles[0].Text = "温度显示";
this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.chart1.Series[0].Color = Color.Red;
this.chart1.Series[0].ChartType = SeriesChartType.Line;
this.chart1.Series[0].Points.Clear();
}
private void UpdateQueueValue()
{
if (dataQueue.Count > 100)
{
for (int i = 0; i < num; i++)
{
dataQueue.Dequeue();
}
}
for (int i = 0; i < num; i++)
{
dataQueue.Enqueue(Temp);
}
}
private void timer1_Tick_1(object sender, EventArgs e)
{
if (Arrive_time[2] == 1)
{
Arrive_time[2] = 0;
UpdateQueueValue();
this.chart1.Series[0].Points.Clear();
for (int i = 0; i < dataQueue.Count; i++)
{
this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
}
}
}
}
}
下位机采集代码:
测试可用
MLX90614.c
#include "delay.h"
#include "MLX90614.h"
#define ACK 0
#define NACK 1
#define SA 0x00
#define RAM_ACCESS 0x00
#define EEPROM_ACCESS 0x20
#define RAM_TOBJ1 0x07
#define SMBUS_PORT GPIOB
#define SMBUS_SCK GPIO_Pin_14
#define SMBUS_SDA GPIO_Pin_13
#define RCC_APB2Periph_SMBUS_PORT RCC_APB2Periph_GPIOB
#define SMBUS_SCK_H() SMBUS_PORT->BSRR = SMBUS_SCK
#define SMBUS_SCK_L() SMBUS_PORT->BRR = SMBUS_SCK
#define SMBUS_SDA_H() SMBUS_PORT->BSRR = SMBUS_SDA
#define SMBUS_SDA_L() SMBUS_PORT->BRR = SMBUS_SDA
#define SMBUS_SDA_PIN() SMBUS_PORT->IDR & SMBUS_SDA
void SMBus_StartBit(void)
{
SMBUS_SDA_H();
SMBus_Delay(5);
SMBUS_SCK_H();
SMBus_Delay(5);
SMBUS_SDA_L();
SMBus_Delay(5);
SMBUS_SCK_L();
SMBus_Delay(5);
}
void SMBus_StopBit(void)
{
SMBUS_SCK_L();
SMBus_Delay(5);
SMBUS_SDA_L();
SMBus_Delay(5);
SMBUS_SCK_H();
SMBus_Delay(5);
SMBUS_SDA_H();
}
u8 SMBus_SendByte(u8 Tx_buffer)
{
u8 Bit_counter;
u8 Ack_bit;
u8 bit_out;
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if (Tx_buffer&0x80)
{
bit_out=1;
}
else
{
bit_out=0;
}
SMBus_SendBit(bit_out);
Tx_buffer<<=1;
}
Ack_bit=SMBus_ReceiveBit();
return Ack_bit;
}
void SMBus_SendBit(u8 bit_out)
{
if(bit_out==0)
{
SMBUS_SDA_L();
}
else
{
SMBUS_SDA_H();
}
SMBus_Delay(2);
SMBUS_SCK_H();
SMBus_Delay(6);
SMBUS_SCK_L();
SMBus_Delay(3);
return;
}
u8 SMBus_ReceiveBit(void)
{
u8 Ack_bit;
SMBUS_SDA_H();
SMBus_Delay(2);
SMBUS_SCK_H();
SMBus_Delay(5);
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L();
SMBus_Delay(3);
return Ack_bit;
}
u8 SMBus_ReceiveByte(u8 ack_nack)
{
u8 RX_buffer;
u8 Bit_Counter;
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(SMBus_ReceiveBit())
{
RX_buffer <<= 1;
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1;
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack);
return RX_buffer;
}
void SMBus_Delay(u16 time)
{
delay_us(time);
}
void SMBus_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE);
GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);
SMBUS_SCK_H();
SMBUS_SDA_H();
}
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
u16 data;
u8 Pec;
u8 DataL=0;
u8 DataH=0;
u8 arr[6];
u8 PecReg;
u8 ErrorCounter;
ErrorCounter=0x00;
slaveAddress <<= 1;
do
{
repeat:
SMBus_StopBit();
--ErrorCounter;
if(!ErrorCounter)
{
break;
}
SMBus_StartBit();
if(SMBus_SendByte(slaveAddress))
{
goto repeat;
}
if(SMBus_SendByte(command))
{
goto repeat;
}
SMBus_StartBit();
if(SMBus_SendByte(slaveAddress+1))
{
goto repeat;
}
DataL = SMBus_ReceiveByte(ACK);
DataH = SMBus_ReceiveByte(ACK);
Pec = SMBus_ReceiveByte(NACK);
SMBus_StopBit();
arr[5] = slaveAddress;
arr[4] = command;
arr[3] = slaveAddress+1;
arr[2] = DataL;
arr[1] = DataH;
arr[0] = 0;
PecReg=PEC_Calculation(arr);
}
while(PecReg != Pec);
data = (DataH<<8) | DataL;
return data;
}
u8 PEC_Calculation(u8 pec[])
{
u8 crc[6];
u8 BitPosition=47;
u8 shift;
u8 i;
u8 j;
u8 temp;
do
{
crc[5]=0;
crc[4]=0;
crc[3]=0;
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
BitPosition=47;
shift=0;
i=5;
j=0;
while((pec[i]&(0x80>>j))==0 && i>0)
{
BitPosition--;
if(j<7)
{
j++;
}
else
{
j=0x00;
i--;
}
}
shift=BitPosition-8;
while(shift)
{
for(i=5; i<0xFF; i--)
{
if((crc[i-1]&0x80) && (i>0))
{
temp=1;
}
else
{
temp=0;
}
crc[i]<<=1;
crc[i]+=temp;
}
shift--;
}
for(i=0; i<=5; i++)
{
pec[i] ^=crc[i];
}
}
while(BitPosition>8);
return pec[0];
}
float SMBus_ReadTemp(void)
{
float temp;
temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
return temp;
}
MLX96014.h
#ifndef __MLX90614_H
#define __MLX90614_H
#include "io.h"
void SMBus_StartBit(void);
void SMBus_StopBit(void);
void SMBus_SendBit(u8);
u8 SMBus_SendByte(u8);
u8 SMBus_ReceiveBit(void);
u8 SMBus_ReceiveByte(u8);
void SMBus_Delay(u16);
void SMBus_Init(void);
u16 SMBus_ReadMemory(u8, u8);
u8 PEC_Calculation(u8*);
float SMBus_ReadTemp(void);
void SMBus_DisplayTemperature(void);
#endif
测试MCU :STM32F103C8T6
测试主函数
while(1)
{
fTemperature = SMBus_ReadTemp();
printf("MLX90614 Measure Temp:%.2f ℃\r\n",fTemperature);
delay_ms(200);
}
|