zigbee协议栈雷达 上位机部分

声明:本人初学zigbee,很多代码引用自他人,因此本文仅作参考


前言

本人这学期课设,要求使用zigbee完成设计。在学习中颇有收获和心得,因此写下本文记录,并分享一些经验。

以下是上位机部分。硬件部分详见zigbee协议栈雷达 硬件部分


一、效果图

因为打算延伸这个课题,所以上位机是在我原来的小车上位机的基础上开发的
zigbee雷达上位机

二、初始化

public Form1()
{
    InitializeComponent();
    serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.Visible = false;
    radioButton1.Checked = false;
    radioButton2.Checked = false;
    radioButton3.Checked = true;
    handOFF();
   SearchAndAddSerialToComboBox(comboBox1);

   my_Paint = get_class_My_Paint(pictureBox1);

}

三、串口

//更新串口列表 
private void SearchAndAddSerialToComboBox(ComboBox MyBox)
{
    MyBox.Items.Clear();
    comboBox1.Text = "";
    foreach (string s in SerialPort.GetPortNames())
    {
        MyBox.Items.Add(s);
    }
}
//串口发送数据
private void WriteByteToSerialPort(byte data)
{
    byte[] buffer = new byte[3];
    buffer[0] = 0x4F;//"O"
    buffer[1] = 0x4E;//"N"
    buffer[2] = data;
    if (serialPort1.IsOpen)
    {
        try
        {
            serialPort1.Write(buffer, 0, 3);
        }
        catch
        {
            MessageBox.Show("串口数据发送出错,请检查", "错误");
        }
    }
}
//控制串口打开和关闭按钮
private void button6_Click(object sender, EventArgs e) 
        {
            if (serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Close();
                    button6.Text = "打开串口";
                    comboBox1.Text = "";
                    button5.Enabled = true;
                    pictureBox1.Visible = false;
                    pictureBox2.Visible = true;
                    handOFF();
                }
                catch
                {

                }
            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
                    serialPort1.Open();
                    button6.Text = "关闭串口";
                    button5.Enabled = false;
                    pictureBox1.Visible = true;
                    pictureBox2.Visible = false;
                    if (radioButton3.Checked == true)
                    {
                        handON();
                    }
                    else
                    {
                        handOFF();
                    }
                }
                catch
                {
                    MessageBox.Show("串口打开失败", "错误");
                }
            }
        }
//串口接收数据
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{                
    int len = serialPort1.BytesToRead;
    double d;//超声波测量距离
    int a;//舵机当前角度
    if (len != 0)
    {
        byte[] buff = new byte[len];
        serialPort1.Read(buff, 0, len);
        if (len == 4)//接收到终端1数据(具体注释详见四、绘制雷达)
        {
            d = buff[0] * 100 + buff[1] * 10 + buff[2];
            a = buff[3];
            textBox1.Text = Convert.ToString(a)+"°";
            textBox3.Text = Convert.ToString(d) + "cm";
            d = d / 2.3;
            int flag = 0;
            if (a == 180) { flag = 1; class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Clear"); }
            else if (a == 0) { flag = 0; class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Clear"); }

            if (flag == 0)
            {
                double y = d * Math.Cos(a * Math.PI / 180);
                double x = d * Math.Sin(a * Math.PI / 180);
                class_My_Paint.Point_set(x, y, "Draw");
                class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Draw");
            }
            else if (flag == 1)
            {
                double y = d * Math.Cos(a * Math.PI / 180);
                double x = d * Math.Sin(a * Math.PI / 180);
                class_My_Paint.Point_set(x, -y, "Draw");
                class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Draw");
            }
        }
        else if (len == 1)//接收到终端2数据
        {
            if (buff[0] == 0x31)
            {
                textBox2.Text = "空闲";
            }
            else if (buff[0] == 0x30)
            {
                textBox2.Text = "检测到";
            }
        }
    }
}

四、绘制雷达

此处代码参考C#上位机开发(三) 软件图形绘制方法

//创建画板
public Class_My_Paint get_class_My_Paint(Control Paint_Boxset)
{
    class_My_Paint = new Class_My_Paint(pictureBox3);
    return class_My_Paint;
}
//创建时绘制雷达
private void Point_Box_Paint(object sender, PaintEventArgs e)
{
   my_Paint.PointBox_Paint(e.Graphics, "Draw");
}
//清屏
private void button8_Click(object sender, EventArgs e)
{
    class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Clear");
}
//串口接收数据时绘制
    int len = serialPort1.BytesToRead;
    double d;//超声波测量距离
    int a;//舵机当前角度
    
        byte[] buff = new byte[len];
        serialPort1.Read(buff, 0, len);
        if (len == 4)//接收到终端1数据
        {
        	//处理接收的数据
            d = buff[0] * 100 + buff[1] * 10 + buff[2];
            a = buff[3];
            //显示接收的数据
            textBox1.Text = Convert.ToString(a)+"°";
            textBox3.Text = Convert.ToString(d) + "cm";
            d = d / 2.3;
            int flag = 0;//控制雷达绘图方向
            //雷达每转180°清屏一次
            if (a == 180) { flag = 1; class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Clear"); }
            else if (a == 0) { flag = 0; class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Clear"); }

            if (flag == 0)//正转
            {
            	//计算点的坐标
                double y = d * Math.Cos(a * Math.PI / 180);
                double x = d * Math.Sin(a * Math.PI / 180);
                //创建新的点
                class_My_Paint.Point_set(x, y, "Draw");
                //更新雷达图
                class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Draw");
            }
            else if (flag == 1)//反转
            {
            	//计算点的坐标
                double y = d * Math.Cos(a * Math.PI / 180);
                double x = d * Math.Sin(a * Math.PI / 180);
                //创建新的点
                class_My_Paint.Point_set(x, -y, "Draw");
                //更新雷达图
                class_My_Paint.PointBox_Paint(pictureBox3.CreateGraphics(), "Draw");
            }
        }

五、完整代码

链接:https://pan.baidu.com/s/1nsFz_u1_ovUWgngYoPi36Q
提取码:wdcw

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青右

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值