概要
通过串口方式与下位机通信,通信协议自定上位机可以实时显示下位机采集到的温度数据
C#上位机程序
Form.cs部分代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
//SerialPort serialPort1 = new SerialPort();
GreenHouse greenHouse = new GreenHouse();
private bool serial_open = false;
public Form1()
{
InitializeComponent();
serialPort1.DataReceived += serialPort1_DataReceived;
greenHouse.RequestSendData += Greenhouse1_RequestSendData;
}
private void Greenhouse1_RequestSendData(byte[] data)
{
serialPort1.Write(data, 0, data.Length);
}
private void Form1_Load(object sender, EventArgs e)
{
uart_name();
cb_bot.SelectedIndex = 1;
Cb_databit.SelectedIndex = 0;
cb_jiaoyan.SelectedIndex = 2;
cb_stopbit.SelectedIndex = 0;
}
private void suo_uart_Click(object sender, EventArgs e)
{
uart_name();
}
private void open_uart_Click(object sender, EventArgs e)
{
if (serial_open == true)
{
usart_portclose();
}
else