找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3451|回复: 5
收起左侧

求代码 单片机温室监控管理系统上位机C#程序(VS2008)仿真与源程序设计

[复制链接]
ID:430780 发表于 2019-3-18 10:06 | 显示全部楼层 |阅读模式
基于51单片机的温室控制与管理系统仿真设计(源程序+仿真文件)分享!
0.png

实物电路中MAX232与8051的连接方法:
(1) MAX232的11脚(TIN)接8051的P3.1(TXD)
(2) MAX232的12脚(R1OUT)接8051的P3.0(RXD)
通过上位机控制时,设COM3,COM4对连

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
0.png 0.png 0.png

上位机C#程序(VS2008)源程序如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Microsoft.Win32;

  9. namespace 温室控制程序
  10. {
  11.     public partial class Form_Main : Form
  12.     {
  13.         public Form_Main()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.         //--------------------------------------------------------------------
  18.         // 代理函数
  19.         //--------------------------------------------------------------------
  20.         delegate void SetTextCallBack(string s);
  21.         //--------------------------------------------------------------------
  22.         // 关闭窗体时提示确认
  23.         //--------------------------------------------------------------------
  24.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  25.         {
  26.             if (MessageBox.Show("您确定要退出系统吗?", "确认",
  27.                 MessageBoxButtons.YesNo, MessageBoxIcon.Question,
  28.                 MessageBoxDefaultButton.Button2) == DialogResult.No)
  29.             {
  30.                 e.Cancel = true;
  31.             }
  32.         }
  33.         //--------------------------------------------------------------------
  34.         // 获取串口列表
  35.         //--------------------------------------------------------------------
  36.         public void GetComList()
  37.         {
  38.             RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
  39.             if (keyCom != null)
  40.             {
  41.                 string[] sSubKeys = keyCom.GetValueNames();
  42.                 comboBox1.Items.Clear();
  43.                 foreach (string sName in sSubKeys)
  44.                 {
  45.                     string sValue = (string)keyCom.GetValue(sName);
  46.                     comboBox1.Items.Add(sValue);
  47.                 }
  48.             }
  49.         }
  50.         //--------------------------------------------------------------------
  51.         // 退出系统
  52.         //--------------------------------------------------------------------
  53.         private void button_退出_Click(object sender, EventArgs e)
  54.         {        
  55.             this.Close();
  56.         }
  57.         //--------------------------------------------------------------------
  58.         //打开串口
  59.         //--------------------------------------------------------------------
  60.         private void button_打开串口_Click(object sender, EventArgs e)
  61.         {
  62.             if (button_打开串口.Text == "打开串口")
  63.             {
  64.                 if (serialPort1.IsOpen) serialPort1.Close();
  65.                 serialPort1.PortName = comboBox1.Text;
  66.                 try
  67.                 {
  68.                     serialPort1.Open();
  69.                     toolStripStatusLabel1.Text = comboBox1.Text + " 打开";
  70.                     button_打开串口.Text = "关闭串口";
  71.                 }
  72.                 catch { toolStripStatusLabel1.Text = "端口打开错误!!!"; }
  73.             }
  74.             else
  75.             {
  76.                 serialPort1.Close();
  77.                 toolStripStatusLabel1.Text = "端口关闭";
  78.                 button_打开串口.Text = "打开串口";
  79.             }
  80.         }
  81.         //--------------------------------------------------------------------
  82.         // 加载窗体时执行
  83.         //--------------------------------------------------------------------
  84.         private void Form_Main_Load(object sender, EventArgs e)
  85.         {
  86.             comboBox1.Items.Clear();
  87.             GetComList();
  88.             if (comboBox1.Items.Count != 0) comboBox1.SelectedIndex = 1;
  89.             RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\温室监控系统2010",true);
  90.             if (reg == null)
  91.             {
  92.                 reg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\温室监控系统2010");
  93.             }
  94.             if (reg != null)
  95.             {
  96.                 comboBox1.Text = (string)reg.GetValue("端口");
  97.             }
  98.         }
  99.         //--------------------------------------------------------------------
  100.         // 关闭窗体时将当前串口选项写入注册表
  101.         //--------------------------------------------------------------------
  102.         private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
  103.         {
  104.             if (serialPort1.IsOpen) serialPort1.Close();
  105.             RegistryKey reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\温室监控系统2010",true);
  106.             if (reg != null) reg.SetValue("端口", comboBox1.Text);   
  107.         }
  108.         //--------------------------------------------------------------------
  109.         // 喷灌按钮操作
  110.         //--------------------------------------------------------------------
  111.         private void button_喷灌_Click(object sender, EventArgs e)
  112.         {
  113.             if (!serialPort1.IsOpen)
  114.             { MessageBox.Show("串口未打开,控制命令无法发送!", "提示"); return; }
  115.             if (button_喷灌.Text == "喷灌")
  116.             {
  117.                 //发送控制指令 开启继电器
  118.                 serialPort1.WriteLine("$PUMP_OPEN");
  119.                 button_喷灌.Text = "停止喷灌";
  120.             }
  121.             else
  122.             {
  123.                 //发送控制指令 关闭继电器
  124.                 serialPort1.WriteLine("$PUMP_CLOSE");
  125.                 button_喷灌.Text = "喷灌";
  126.             }
  127.         }
  128.         //--------------------------------------------------------------------
  129.         // 通风按钮操作
  130.         //--------------------------------------------------------------------
  131.         private void button_通风_Click(object sender, EventArgs e)
  132.         {
  133.             if (!serialPort1.IsOpen)
  134.             { MessageBox.Show("串口未打开,控制命令无法发送!", "提示"); return; }
  135.             if (button_通风.Text == "通风")
  136.             {
  137.                 //发送控制指令 开启电机1
  138.                 serialPort1.WriteLine("$WIND_OPEN");
  139.                 button_通风.Text = "停止通风";
  140.             }
  141.             else
  142.             {
  143.                 //发送控制指令 关闭电机1
  144.                 serialPort1.WriteLine("$WIND_CLOSE");
  145.                 button_通风.Text = "通风";
  146.             }
  147.         }
  148.         //--------------------------------------------------------------------
  149.         // 采光按钮操作
  150.         //--------------------------------------------------------------------
  151.         private void button_采光_Click(object sender, EventArgs e)
  152.         {
  153.             if (!serialPort1.IsOpen)
  154.             { MessageBox.Show("串口未打开,控制命令无法发送!", "提示"); return; }
  155.             if (button_采光.Text == "采光")
  156.             {
  157.                 //发送控制指令 开启电机2
  158.                 serialPort1.WriteLine("$LIGHT_OPEN");
  159.                 button_采光.Text = "停止采光";
  160.             }
  161.             else
  162.             {
  163.                 //发送控制指令 关闭电机2
  164.                 serialPort1.WriteLine("$LIGHT_CLOSE");
  165.                 button_采光.Text = "采光";
  166.             }
  167.         }
  168.         //--------------------------------------------------------------------
  169.         // 显示代理函数
  170.         //--------------------------------------------------------------------
  171.         private void SetText(string s)
  172.         {
  173.             if (label_温度.InvokeRequired)
  174.             {
  175.                 SetTextCallBack d = new SetTextCallBack(SetText);
  176.                 Invoke(d, new object[] { s });
  177.             }
  178.             else
  179.             {
  180.                 label_温度.Text = s.Trim() + " ℃";
  181.                 label_温度.Refresh();
  182.             }
  183.         }
  184.         //--------------------------------------------------------------------
  185.         // 串口接收温度数据并通过代理程序显示
  186.         //--------------------------------------------------------------------
  187.         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  188.         {
  189.             try { SetText(serialPort1.ReadLine()); } catch { }
  190.         }
  191.     }
  192. }
复制代码


单片机源程序如下:
  1. //-----------------------------------------------------------------
  2. //  名称: 温室控制系统仿真设计
  3. //-----------------------------------------------------------------
  4. //  说明: K1~K3按键分别控制通风、采光及水泵电机开关,LCD显示当前温度值.
  5. //        上位机按钮可分别实现K1~K3的控制功能,系统运行时,下位机温度值
  6. //        将刷新显示在上位机C#窗体中.
  7. //
  8. //-----------------------------------------------------------------
  9. #include <reg51.h>
  10. #include <intrins.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #define INT8U        unsigned char
  14. #define INT16U        unsigned int
  15. #define MAX_CHAR 11                                                //允许接收并保存的最大字符个数
  16. volatile INT8U recv_buff[MAX_CHAR+1];        //串口接收缓冲
  17. volatile INT8U Buf_Index = 0;                        //缓冲索引
  18. extern INT8U Read_Temperature();                //读传感器温度函数
  19. extern void LCD_Initialize();                        //LCD初始化函数
  20. extern void LCD_ShowString(INT8U , INT8U,INT8U *);//在指定行/列显示字符串函数
  21. extern INT8U Temp_Value[];                                //从DS18B20读取的数据
  22. extern void delay_ms(INT16U x);                        //延时函数
  23. char Disp_Buffer[17];                                        //LCD显示缓冲
  24. volatile INT8U recv_OK = 0;                                //上位机命令串接收成功标识
  25. //相关按键、控制引脚等定义
  26. sbit K1 = P1^5;                                                        //通风电机开关控制按键
  27. sbit K2 = P1^6;                                                        //采光电机开关控制按键
  28. sbit K3 = P1^7;                                                        //水泵开关控制按键
  29. sbit F_IN1 = P1^0;                                                //通风电机控制端
  30. sbit F_IN2 = P1^1;
  31. sbit F_IN3 = P1^2;                                                //采光电机控制端
  32. sbit F_IN4 = P1^3;
  33. sbit RELAY = P2^4;                                                //水泵控制继电器
  34. sbit LED_1 = P2^5;                                                //通风电机开关指示灯
  35. sbit LED_2 = P2^6;                                                //采光电机开关指示灯
  36. sbit LED_3 = P2^7;                                                //水泵指示灯
  37. //-----------------------------------------------------------------
  38. // 串口输出字符串
  39. //-----------------------------------------------------------------
  40. void PutStr(char *s)
  41. {


  42. }

  43. //-----------------------------------------------------------------
  44. // 主函数
  45. //-----------------------------------------------------------------
  46. void main()
  47. {


  48. }

  49. //-----------------------------------------------------------------
  50. // INT0中断函数
  51. //-----------------------------------------------------------------
  52. void INT0_ISR() interrupt 0
  53. {


  54. ……………………

  55. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
温室监控系统仿真设计.zip (533.84 KB, 下载次数: 102)

评分

参与人数 2黑币 +80 收起 理由
1112cyq + 30 绝世好帖!
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:98880 发表于 2019-3-20 16:59 | 显示全部楼层
KEILC里的代码不知道为什么不全?是作者故意的吗?
回复

使用道具 举报

ID:308951 发表于 2019-3-21 08:32 | 显示全部楼层
代码很好,内容十分详细,已经借鉴
回复

使用道具 举报

ID:1060471 发表于 2023-1-7 12:39 | 显示全部楼层
内容非常好,但是有一些不全
回复

使用道具 举报

ID:1085531 发表于 2023-6-23 14:16 | 显示全部楼层
请问楼主:注册表如何去引用
回复

使用道具 举报

ID:1085897 发表于 2023-6-26 20:27 | 显示全部楼层
代码不全啊!!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表