c#编写的串口助手打开串口32单片机立刻黑屏,急求

using Microsoft.Win32;
using System;
using System.IO.Ports;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

namespace WindowsFormsApp
{
public partial class Form1 : Form
{
//SerialPort sp = null;//声明一个串口类
bool isOpen = false;//打开串口标志位
bool isSetProperty = false;//属性设置标志位
bool isHex = false;//十六进制显示标志位

    public Form1()
    {
        InitializeComponent();
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//设置该属性为false
    }

    //我们在form载入时,扫描识别存在的串口。
    private void Form1_Load(object sender, EventArgs e)
    {
        this.MaximumSize = this.Size;
        this.MinimumSize = this.Size;
        this.MaximizeBox = false;
        RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
        if (keyCom != null)
        {
            string[] sSubKeys = keyCom.GetValueNames();
            cbxCOMPort.Items.Clear();
            foreach (string sName in sSubKeys)
            {
                string sValue = (string)keyCom.GetValue(sName);
                cbxCOMPort.Items.Add(sValue);
            }
            if (cbxCOMPort.Items.Count > 0)
                cbxCOMPort.SelectedIndex = 0;
            else
            {
                cbxCOMPort.Text = "";
                MessageBox.Show("没有找到可用串口!", "错误提示");
            }
        }
        cbxBaudRate.SelectedIndex = 4;
        cbxStopBits.SelectedIndex = 1;
        cbxDataBits.SelectedIndex = 0;
        cbxParity.SelectedIndex = 0;
        rbnChar.Checked = true;
    }

    private void btnCheckCOM_Click(object sender, EventArgs e)//检测哪些串口可用
    {
        RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
        if (keyCom != null)
        {
            string[] sSubKeys = keyCom.GetValueNames();
            cbxCOMPort.Items.Clear();
            foreach (string sName in sSubKeys)
            {
                string sValue = (string)keyCom.GetValue(sName);
                cbxCOMPort.Items.Add(sValue);
            }
            if (cbxCOMPort.Items.Count > 0)
                cbxCOMPort.SelectedIndex = 0;
            else
            {
                cbxCOMPort.Text ="";
                MessageBox.Show("没有找到可用串口!", "错误提示");
            }
        }

    }

    private bool CheckPortSetting()//检查串口是否设置
    {
        if (cbxCOMPort.Text.Trim() == "") return false;
        if (cbxBaudRate.Text.Trim() == "") return false;
        if (cbxDataBits.Text.Trim() == "") return false;
        if (cbxParity.Text.Trim() == "") return false;
        if (cbxStopBits.Text.Trim() == "") return false;
        return true;
    }

    private bool CheckSendData()
    {
        if (tbxSendData.Text.Trim() == "") return false;
        return true;
    }

    private void SetPortProperty()//设置串口的属性
    {
        serialPort.PortName = cbxCOMPort.Text.Trim();//设置串口名

        serialPort.BaudRate = Convert.ToInt32(cbxBaudRate.Text.Trim());//设置串口的波特率

        float f = Convert.ToSingle(cbxStopBits.Text.Trim());//设置停止位
        if (f == 0)
        {
            serialPort.StopBits = StopBits.None;
        }
        else if (f == 1.5)
        {
            serialPort.StopBits = StopBits.OnePointFive;
        }
        else if (f == 1)
        {
            serialPort.StopBits = StopBits.One;
        }
        else if (f == 2)
        {
            serialPort.StopBits = StopBits.Two;
        }
        else
        {
            serialPort.StopBits = StopBits.One;
        }

        serialPort.DataBits = Convert.ToInt16(cbxDataBits.Text.Trim());//设置数据位

        string s = cbxParity.Text.Trim(); //设置奇偶校验位
        if (s.CompareTo("无") == 0)
        {
            serialPort.Parity = Parity.None;
        }
        else if (s.CompareTo("奇校验") == 0)
        {
            serialPort.Parity = Parity.Odd;
        }
        else if (s.CompareTo("偶校验") == 0)
        {
            serialPort.Parity = Parity.Even;
        }
        else
        {
            serialPort.Parity = Parity.None;
        }

        serialPort.ReadTimeout = -1;//设置超时读取时间

        serialPort.RtsEnable = true;
        //定义DataReceived 事件,当串口收到数据后触发事件
        serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        if (rbnHex.Checked)
        {
            isHex = true;
        }
        else
        {
            isHex = false;
        }
    }

    private void btnOpenCom_Click(object sender, EventArgs e)
    {
        if (!isOpen)
        {
            if (!CheckPortSetting())//检测串口设置
            {
                MessageBox.Show("串口未设置!", "错误提示"); return;
            }
            if (!isSetProperty)//串口未设置则设置串口
            {
                SetPortProperty(); isSetProperty = true;
            }             
            try
            {
                serialPort.Open();     //打开串口
                btnOpenCom.Text = "关闭串口";
                cbxCOMPort.Enabled = false;//关闭使能
                cbxBaudRate.Enabled = false;
                cbxStopBits.Enabled = false;
                cbxParity.Enabled = false;
                cbxDataBits.Enabled = false;
                rbnChar.Enabled = false;
                rbnHex.Enabled = false;
                isOpen = true;
                serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);//串口接收处理函数
            }
            catch
            {
                MessageBox.Show("串口打开失败!");
            }
        }
        else
        {
            try
            {
                serialPort.Close();     //关闭串口
                btnOpenCom.Text = "打开串口";
                cbxCOMPort.Enabled = true;//打开使能
                cbxBaudRate.Enabled = true;
                isOpen = false;
            }
            catch
            {
                MessageBox.Show("串口关闭失败!");
            }
        }

    }

    //private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    //{
    //    string str = serialPort.ReadExisting();//字符串方式读
    //    tbxRecvData.Text = "";//先清除上一次的数据
    //    tbxRecvData.Text += str;
    //}

    private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        System.Threading.Thread.Sleep(100);//延时 100ms 等待接收完数据
                                           //this.Invoke 就是跨线程访问 ui 的方法,也是本文的范例
        this.Invoke((EventHandler)(delegate
        {
            if (isHex == false)
            {
                tbxRecvData.Text += serialPort.ReadLine();
            }
            else
            {
                Byte[] ReceivedData = new Byte[serialPort.BytesToRead];     //创建接收字节数组
                serialPort.Read(ReceivedData, 0, ReceivedData.Length);  //读取所接收到的数据
                String RecvDataText = null;
                for (int i = 0; i < ReceivedData.Length - 1; i++)
                {

                    RecvDataText += ("0x" + ReceivedData[i].ToString("X2") + " ");
                }
                tbxRecvData.Text += RecvDataText;
            }
            serialPort.DiscardInBuffer();//丢弃接收缓冲区数据        
        }));
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        //发送数据
        if (serialPort.IsOpen)
        {//如果串口开启
            if (tbxSendData.Text.Trim() != "")//如果框内不为空则
            {
                serialPort.Write(tbxSendData.Text.Trim());//写数据
            }
            else
            {
                MessageBox.Show("发送框没有数据");
            }
        }
        else
        {
            MessageBox.Show("串口未打开");
        }
    }

    private void btnCleanData_Click(object sender, EventArgs e)
    {
        tbxRecvData.Text = ""; tbxSendData.Text = "";
    }
}

}

打开串口没有提示失败,但是单片机就不运行了。

c#

1个回答

用串口调试助手先调试好,如果还不行,要么是你协议的问题,要么是你下位机单片机的问题。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问