您的位置:首页 > 编程语言 > C#

c#串口温度数据的波形采集

2016-05-13 22:07 1256 查看

该上位机是由串口和波形显示两部分组成。废话少说先上传上位机的图片如下:



1.串口的配置

通过“端口设置”来设置串口的相应参数,“端口检测”用来自动检测当前计算机可用的串口,端口号会显示例如“COM1 、COM2......”的端口号,本上位机最大只设置到COM10。波特率可以配置“1200 2400 4800 9600.......”

“ClrWave ”按键用来清除已画的波形,“ClrData”按键用来清除接收区和发送区的数据。数据发送区和数据接收区用来发送和接收数据;

在Visual Studio 2012 菜单栏点击“文件”>“新建项目”,创建Visual C#下的windows窗体应用程序:

在窗体中上添加5个“GroupBox”控件,分别将窗体分为“端口设置”,“波形显示”,“数据接收区”,“数据发送区”,“当前温度显示”;

添加label控件显示“端口号”,“波特率”;添加若干Button控件;添加若干TextBox控件用来数据的收发;

添加checkBox控件用来设置“自动换行”和“接收换行”;

双击控件可以在程序中添加相应的代码,另外可以使用USB转串口模块来测试,将串口的TX和RX用杜邦线连接,这样串口发出的信息就能被自己接收到了。

布置好控件后如下图:

串口部分的代码如下:

/*************************************************************************************************************

串口配置

************************************************************************************************************/

SerialPort sp = null;

bool isOpen = false;

bool isSetProperty = false;

bool Enter_flag = false;

bool Rece_flag = false;

//窗口加载

private void Form1_Load(object sender, EventArgs e)

{

this.MaximumSize = this.Size;

this.MinimumSize = this.Size;

this.MinimizeBox = false;

for (int i = 0; i < 10; i++)

{

comboBox1.Items.Add("COM" + Convert.ToString(i + 1));

}

comboBox2.Items.Add("1200");

comboBox2.Items.Add("2400");

comboBox2.Items.Add("4800");

comboBox2.Items.Add("9600");

comboBox2.Items.Add("19200");

comboBox2.Items.Add("38400");

comboBox2.Items.Add("43000");

comboBox2.Items.Add("115200");

comboBox2.SelectedIndex = 3;

}

//设置串口属性

void SetPortProperty()

{

sp = new SerialPort();

sp.PortName = comboBox1.Text.Trim();

sp.BaudRate = Convert.ToInt32(comboBox2.Text.Trim());

sp.StopBits = StopBits.One;

sp.Parity = Parity.None;

sp.DataBits = 8;

sp.RtsEnable = true;

sp.ReadTimeout = -1;

sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

}

//接收数据

private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

this.Invoke((EventHandler)(delegate

{

try

{

float data;

string str = sp.ReadExisting();

textBox1.AppendText(str+" ");

textBox3.Text = str;

data = Convert.ToSingle(str);

AddData(data);

}

catch (Exception)

{

MessageBox.Show("读数据出错", "错误提示!");

}

if (Rece_flag)

textBox1.Text += "\r\n";

this.textBox1.Focus();

this.textBox1.Select(this.textBox1.TextLength, 0);

this.textBox1.ScrollToCaret();

sp.DiscardInBuffer();

}));

}

private void button1_Click(object sender, EventArgs e)

{

bool comExistence = false;

comboBox1.Items.Clear();

for (int i = 0; i < 10; i++)

{

try

{

SerialPort sp = new SerialPort("COM" + Convert.ToString(i + 1));

sp.Open();

sp.Close();

comboBox1.Items.Add("COM" + Convert.ToString(i + 1));

comExistence = true;

}

catch (Exception)

{

continue;

}

}

if (comExistence)

{

comboBox1.SelectedIndex = 0;

}

else

{

MessageBox.Show("没有找到可用串口!", "错误提示");

}

}

private void button2_Click(object sender, EventArgs e)

{

if (isOpen == false)

{

if (!isSetProperty)

{

isSetProperty = true;

SetPortProperty();

}

try

{

sp.Open();

isOpen = true;

button1.Enabled = false;

button2.BackColor = Color.Red;

button2.Text = "关闭";

}

catch (Exception)

{

isSetProperty = false;

isOpen = false;

MessageBox.Show("串口无效或已被占用!", "错误提示");

}

}

else

{

try

{

sp.Close();

isOpen = false;

isSetProperty = false;

button1.Enabled = true;

button2.BackColor = Color.White;

button2.Text = "开";

}

catch (Exception)

{

MessageBox.Show("串口异常断开!", "错误提示");

}

}

}

private void button3_Click(object sender, EventArgs e)

{

byte[] Data = new byte[1];

if (isOpen)

{

try

{

byte[] data = Encoding.Default.GetBytes(textBox2.Text);

sp.Write(data, 0, data.Length);

}

catch (Exception)

{

MessageBox.Show("数据发送是发生错误!", "错误提示");

return;

}

if (Enter_flag)

sp.Write("\r\n");

}

else

{

MessageBox.Show("串口未打开!", "错误提示");

return;

}

}

2.波形的显示

初始化参数:
private const int Unit_length = 50;//单位格大小

private const int StartPrint = 50;//点坐标偏移量

private List<float> DataList = new List<float>();//数据结构----线性链表

private Pen TablePen = new Pen(Color.FromArgb(0xff, 0xff, 0xff));//轴线颜色

private Pen LinesPen = new Pen(Color.FromArgb(0xff, 0x00, 0x00));//波形颜色
波形网格图绘制:
波形的显示,我们采用绘制直线的方式,绘制网格图,使用Graphics对象的Drawline()方法.

pubulic void Drawline(Pen pen,int x1,int y1,int x2,int y2);其中,pen对象是画线所用的画笔,它决定了线的颜色、宽度和样式;x1和y1的起点坐标,x2和y2的终点坐标;

绘制表格的代码如下:

for (int i = 0; i <=10 ; i++)

{

e.Graphics.DrawLine(TablePen, StartPrint + i * Unit_length, StartPrint - 32, StartPrint + i * Unit_length, 7* Unit_length + 18);//画线

gp.AddString((i * 10).ToString(), family, fontstyle, 18, new RectangleF(StartPrint + i * Unit_length - 7, 7 * Unit_length + 18 + 4, 400, 50), null);//添加文字

}

gp.AddString("时间", family, fontstyle, 18, new RectangleF(groupBox4.ClientRectangle.Width/2 - StartPrint, 7 * Unit_length+36, 400, 50), null);

////Draw X 横向轴绘制

for (int i = 0; i < 8; i++)

{

e.Graphics.DrawLine(TablePen, StartPrint, i *Unit_length+18, StartPrint + 10 * Unit_length, i* Unit_length+18);//画线

Str = Convert.ToString((7 - i) * 10);

if (i == 0)

Str = "70";

if (i == 7)

break;

gp.AddString(Str, family, fontstyle, 18, new RectangleF(20, i * Unit_length+8, 400, 50), null);//添加文字

}

gp.AddString("温", family, fontstyle, 18, new RectangleF(0,groupBox4.ClientRectangle.Height/2-StartPrint, 400, 50), null);

gp.AddString("度", family, fontstyle, 18, new RectangleF(0,groupBox4.ClientRectangle.Height /2+18-StartPrint, 400, 50), null);

e.Graphics.DrawPath(Pens.White, gp);//写文字

画波形图代码:

for (int i = 0; i < DataList.Count - 1; i++)

{

e.Graphics.DrawLine(LinesPen, StartPrint + i * 10, 7 * Unit_length + 18 - DataList[i]*5, StartPrint + (i+1) * 10, 7 * Unit_length + 18 - DataList[i + 1]*5);

}

我们需要给线性链表datalist增加数据:

void AddData(float Data)

{

DataList.Add(Data);

groupBox4.Invalidate();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: