該上位機(jī)是由串口和波形顯示兩部分組成。廢話少說(shuō)先上傳上位機(jī)的圖片如下:
20160513211301550.png (20.39 KB, 下載次數(shù): 191)
下載附件
2017-1-1 15:54 上傳
1.串口的配置通過(guò)“端口設(shè)置”來(lái)設(shè)置串口的相應(yīng)參數(shù),“端口檢測(cè)”用來(lái)自動(dòng)檢測(cè)當(dāng)前計(jì)算機(jī)可用的串口,端口號(hào)會(huì)顯示例如“COM1 、COM2......”的端口號(hào),本上位機(jī)最大只設(shè)置到COM10。波特率可以配置“1200 2400 4800 9600.......”
“ClrWave ”按鍵用來(lái)清除已畫的波形,“ClrData”按鍵用來(lái)清除接收區(qū)和發(fā)送區(qū)的數(shù)據(jù)。數(shù)據(jù)發(fā)送區(qū)和數(shù)據(jù)接收區(qū)用來(lái)發(fā)送和接收數(shù)據(jù);
在Visual Studio 2012 菜單欄點(diǎn)擊“文件”>“新建項(xiàng)目”,創(chuàng)建Visual C#下的windows窗體應(yīng)用程序:

在窗體中上添加5個(gè)“GroupBox”控件,分別將窗體分為“端口設(shè)置”,“波形顯示”,“數(shù)據(jù)接收區(qū)”,“數(shù)據(jù)發(fā)送區(qū)”,“當(dāng)前溫度顯示”;
添加label控件顯示“端口號(hào)”,“波特率”;添加若干Button控件;添加若干TextBox控件用來(lái)數(shù)據(jù)的收發(fā);
添加checkBox控件用來(lái)設(shè)置“自動(dòng)換行”和“接收換行”;
雙擊控件可以在程序中添加相應(yīng)的代碼,另外可以使用USB轉(zhuǎn)串口模塊來(lái)測(cè)試,將串口的TX和RX用杜邦線連接,這樣串口發(fā)出的信息就能被自己接收到了。
布置好控件后如下圖:

串口部分的代碼如下:
- /*************************************************************************************************************
- 串口配置
- ************************************************************************************************************/
- 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;
- }
- //設(shè)置串口屬性
- 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);
- }
- //接收數(shù)據(jù)
- 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("讀數(shù)據(jù)出錯(cuò)", "錯(cuò)誤提示!");
- }
- 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("沒(méi)有找到可用串口!", "錯(cuò)誤提示");
- }
- }
-
- 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 = "關(guān)閉";
- }
- catch (Exception)
- {
- isSetProperty = false;
- isOpen = false;
- MessageBox.Show("串口無(wú)效或已被占用!", "錯(cuò)誤提示");
- }
- }
- else
- {
- try
- {
- sp.Close();
- isOpen = false;
- isSetProperty = false;
- button1.Enabled = true;
- button2.BackColor = Color.White;
- button2.Text = "開";
- }
- catch (Exception)
- {
- MessageBox.Show("串口異常斷開!", "錯(cuò)誤提示");
- }
- }
- }
- 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("數(shù)據(jù)發(fā)送是發(fā)生錯(cuò)誤!", "錯(cuò)誤提示");
- return;
- }
-
- if (Enter_flag)
- sp.Write("\r\n");
- }
- else
- {
- MessageBox.Show("串口未打開!", "錯(cuò)誤提示");
- return;
- }
- }
- 2.波形的顯示
- 初始化參數(shù):
- private const int Unit_length = 50;//單位格大小
- private const int StartPrint = 50;//點(diǎn)坐標(biāo)偏移量
- private List<float> DataList = new List<float>();//數(shù)據(jù)結(jié)構(gòu)----線性鏈表
- private Pen TablePen = new Pen(Color.FromArgb(0xff, 0xff, 0xff));//軸線顏色
- private Pen LinesPen = new Pen(Color.FromArgb(0xff, 0x00, 0x00));//波形顏色
- 波形網(wǎng)格圖繪制:
- 波形的顯示,我們采用繪制直線的方式,繪制網(wǎng)格圖,使用Graphics對(duì)象的Drawline()方法.
- pubulic void Drawline(Pen pen,int x1,int y1,int x2,int y2);其中,pen對(duì)象是畫線所用的畫筆,它決定了線的顏色、寬度和樣式;x1和y1的起點(diǎn)坐標(biāo),x2和y2的終點(diǎn)坐標(biāo);
- 繪制表格的代碼如下:
- 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("時(shí)間", 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增加數(shù)據(jù):
- void AddData(float Data)
- {
- DataList.Add(Data);
- groupBox4.Invalidate();
- }
復(fù)制代碼
|