基于【EVAL-ADICUP360】的毫欧计-上位机设计
[复制链接]
本来是准备用一个4.3寸的触摸屏来显示实时结果的,但由于出了点问题,只能做上位机,这样进度能够快点。
界面如图所示:
通过串口将数据显示出来。
代码比较简单:
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.IO.Ports;
using System.Threading;
using System.Reflection.Emit;
namespace Aducm360毫欧计
{
public partial class Form1 : Form
{
List<string> bf = new List<string>();
int offset;
int LastAD = 0;
SerialPort port;
public Form1()
{
InitializeComponent();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
ReloadComs();
}
void ReloadComs() {
cmbCom.Items.Clear();
cmbCom.Items.AddRange(SerialPort.GetPortNames());
if (cmbCom.Items.Count > 0)
{
cmbCom.SelectedIndex = 0;
}
}
private void Form1_Load(object sender, EventArgs e)
{
ReloadComs();
}
private void btnConnction_Click(object sender, EventArgs e)
{
if (cmbCom.Text == "")
return;
if (port != null&&port.IsOpen)
port.Close();
port = new SerialPort(cmbCom.Text);
port.BaudRate = 115200;
port.DataReceived += Port_DataReceived;
port.Open();
}
private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
bf.Add(port.ReadExisting());
var temp = new string(bf.SelectMany(n => n).ToArray());
if (temp.EndsWith("\r\n")) {
var temp2 = temp.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
try
{
var num= UInt32.Parse(temp2.Last());
//128
LastAD =(int) num;
bf.Clear();
}
catch { }
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (LastAD == 0x001FFFFF)
{
labAD.Text = "开路";
labRes.Text = "开路";
return;
}
var new_value = LastAD - offset;
labAD.Text = LastAD.ToString();
labCal.Text = new_value.ToString();
var res = 1200.0f / (float)0x001FFFFF * new_value; //mV
//实测电流 1.081mA
labRes.Text =(0.001081f*res).ToString("0.0000毫欧 ")+ res.ToString("0.0000毫伏"); //1.081*r=v
}
private void btnZero_Click(object sender, EventArgs e)
{
offset = LastAD;
}
}
}
|