上位机读取下位机的数据,并且显示

shuibei987 2010-11-10 04:35:47
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();

}
...全文
550 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
csrwgs 2010-11-10
  • 打赏
  • 举报
回复
LZ是要分享吗?

//初始化SerialPort对象
之前要对comm进行初始化,并Open()

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧