STM32单片机上位机程序代码(供参考)(基于C#开发)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO.Ports;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UpperComputerForSTM32
{
#region CLASS DATADEAL
    public class DataDeal
    {
        SerialPort sp=null;
        string sendData;
        string messageBoxTitle = "Message";
        MessageBoxImage messageBoxImageWarning = MessageBoxImage.Warning;
        MessageBoxButton messageBoxButton = MessageBoxButton.OK;
        MessageBoxImage messageBoxImage = MessageBoxImage.Error;
        #region FUNCTION CONSTRUCTOR
        public DataDeal(SerialPort sp,string sendData)
        {
            this.sp = sp;
            this.sendData = sendData;
        }
        #endregion
        #region FUNCTION SEND DATA
        public void DataSend_isHex()                                               //send Hex datas
        {
            try
            {
                if (sp.IsOpen && (sendData != "") && (sp != null))
                {
                    // sp.DiscardOutBuffer();
                    sendData = sendData.Replace("0x", "");                     //去掉0x,0X字符串;
                    sendData = sendData.Replace("0X", "");
                    sendData = sendData.Replace(" ", "");                         //去掉空格;
                    byte[] sendBytes = new byte[sendData.Length / 2];
                    int j = 0;
                    for (int i = 0; i < sendData.Length; i += 2)
                    {
                        sendBytes[j++] = Convert.ToByte(Int32.Parse(sendData.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));  //将输入的十六进制String转化为Byte数组;
                    }
                    sp.Write(sendBytes, 0, sendData.Length / 2);
                    MessageBox.Show("Send Successfully!\n", messageBoxTitle, messageBoxButton);
                }
                else if (sp.IsOpen == false)
                {
                    MessageBox.Show("Please Open The Port!\n", messageBoxTitle, messageBoxButton, messageBoxImage);
                }
                else
                {
                    MessageBox.Show("Please Input The Sending Data!\n", messageBoxTitle, messageBoxButton, messageBoxImage);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Unable To Send Data!\n", messageBoxTitle, messageBoxButton, messageBoxImage);
            }
        }
        public void DataSend_notHex()                                  //send datas in string
        {
            try
            {
                if ((sp.IsOpen) && (sendData != ""))                   //Send Data;
                {
                    try
                    {
                        sp.DiscardOutBuffer();
                        sp.WriteLine(sendData);
                        MessageBox.Show("Successfully Send!\n", messageBoxTitle, messageBoxButton);
                        //       System.Threading.Thread.Sleep(10);
                        //DataRecieveBox.Text= sp.ReadLine();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Send Failed!\n", messageBoxTitle, messageBoxButton, messageBoxImage);
                        return;
                    }
                }
                else if (sendData == "")                                       //Data is empty;
                {
                    MessageBox.Show("Illegal Sending Data. Input Again!\n", messageBoxTitle, messageBoxButton, messageBoxImage);
                }
                else
                {
                    MessageBox.Show("Port Is Not Opened Normally!\n", messageBoxTitle, messageBoxButton, messageBoxImageWarning);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Send Failed!\n", messageBoxTitle, messageBoxButton, messageBoxImage);
            }
        }
#endregion
    }
#endregion

    /// <summary>
    /// The Upper Computer For STM32
    /// </summary>
    #region CLASS MAINWINDOW
    public partial class MainWindow : Window
    {
        #region VERIABLE DEFINITION
        string messageBoxTitle = "Message";
        string recvDataStore = null;
        MessageBoxButton messageBoxButton = MessageBoxButton.OK;
        MessageBoxImage messageBoxImage = MessageBoxImage.Error;
        MessageBoxImage messageBoxImageWarning = MessageBoxImage.Warning;
        SerialPort sp=new SerialPort() ;
        bool isOpen = false;
        bool isRe
  • 10
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值