上位机读取下位机的数据,并且显示
public partial class SerialportSampleForm : Form
{
private SerialPort comm = new SerialPort();
private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。
private long received_count = 0;//接收计数
private long send_count = 0;//发送计数
private bool Listening = false;//是否没有执行完invoke相关操作
private bool Closing = false;//是否正在关闭串口,执行Application.DoEvents,并阻止再次invoke
private List<byte> buffer = new List<byte>(4096);//默认分配1页内存,并始终限制不允许超过
private byte[] binary_data_1 = new byte[9];//AA 44 05 01 02 03 04 05 EA
public delegate void InvokeDelegate();//定义委托
PointPairList list1 = new PointPairList();
PointPairList list2 = new PointPairList();
string strReceive;
public SerialportSampleForm()
{
InitializeComponent();
}
//窗体初始化,串口初始化
private void Form1_Load(object sender, EventArgs e)
{
//初始化下拉串口名称列表框
string[] ports = SerialPort.GetPortNames();
Array.Sort(ports);
comboPortName.Items.AddRange(ports);
comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
comboBaudrate.SelectedIndex = comboBaudrate.Items.IndexOf("9600");
//初始化SerialPort对象
comm.NewLine = "\r\n";
comm.RtsEnable = true;//根据实际情况吧。
//添加事件注册
comm.DataReceived += comm_DataReceived;
CreateGraph(zgc);
//SetSize();
private void CreateGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
myPane.Title.Text = "温湿度数据曲线图";
myPane.XAxis.Title.Text = "个数";
myPane.YAxis.Title.Text = "温湿度(℃/%)";
// Build a PointPairList with points based on Sine wave
// Hide the legend
//myPane.Legend.IsVisible = false;
// Add a curve
//用钻石符合形状生成红色的曲线和图例
LineItem myCurve = myPane.AddCurve("温度",
list1, Color.Red, SymbolType.Diamond);
//利用圆型的符号生成蓝色的曲线和图例
LineItem myCurve2 = myPane.AddCurve("湿度",
list2, Color.Blue, SymbolType.Circle);
//告诉ZedGraph 去重新描绘坐标轴当数据变化时
//curve.Line.Width = 2F;
//curve.Symbol.Fill = new Fill(Color.White);
//curve.Symbol.Size = 5;
myPane.XAxis.Scale.Max = 100000000;
myPane.XAxis.Scale.Min = 0;
myPane.YAxis.Scale.Max = 100;
myPane.YAxis.Scale.Min = -50;
// Make the XAxis start with the first label at 50
// myPane.XAxis.Scale.BaseTic = 50;
// Fill the axis background with a gradient
myPane.Chart.Fill = new Fill(Color.White, Color.SteelBlue, 23.0F);
// Calculate the Axis Scale Ranges
zgc.AxisChange();
}