Friday_yy 2020-08-04 10:33 采纳率: 0%
浏览 118

指定的函数与委托类型不匹配(出错的地方是 //定义DataReceived事件,当串口收到数据后触发事件)

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports;
using namespace System::Linq;
using namespace System::Text;

namespace serial_communication
{

/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
        SerialPort sp ;
        bool isOpen ;
        bool isSetProperty ;
         bool isHex ;

public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: 在此处添加构造函数代码
        //
    }

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
this->MaximizeBox = false;
this->MaximumSize = this->Size;
this->MinimumSize = this->Size;

         for (int i = 0; i < 10; i++)    //最大支持10个串口,可根据自己需求增加
        { 
            cbxCOMPort->Items->Add("COM"+(i+1).ToString());
        }
         cbxCOMPort->SelectedIndex = 0;


        //列出常用的波特率
        cbxBaudRate->Items->Add("1200");
        cbxBaudRate->Items->Add("2400");
        cbxBaudRate->Items->Add("4800");
        cbxBaudRate->Items->Add("9600");
        cbxBaudRate->Items->Add("19200");
        cbxBaudRate->Items->Add("38400");
        cbxBaudRate->Items->Add("115200");
        cbxBaudRate->SelectedIndex = 5;

        //列出停止位
        cbxStopBits->Items->Add("0");
        cbxStopBits->Items->Add("1");
        cbxStopBits->Items->Add("1.5");
        cbxStopBits->Items->Add("2");
        cbxStopBits->SelectedIndex = 1;

        //列出数据位
        cbxDataBits->Items->Add("8");
        cbxDataBits->Items->Add("7");
        cbxDataBits->Items->Add("6");
        cbxDataBits->Items->Add("5");
        cbxDataBits->SelectedIndex = 0;

        //列出奇偶校验位
        cbxParity->Items->Add("无");
        cbxParity->Items->Add("奇校验");
        cbxParity->Items->Add("偶校验");
        cbxParity->SelectedIndex = 0;

        //默认为char显示
        rbnChar->Checked = true; 
     }

private: System::Void btnCheckCom_Click(System::Object^ sender, System::EventArgs^ e)
{
bool comExistence = false; //有可用串口标志位
cbxCOMPort->Items->Clear(); //清除当前串口号中的所有串口名称
for (int i = 0; i < 10; i++)
{
try
{
sp.PortName = ("COM" + (i + 1).ToString()); //可能有问题
sp.Open();
sp.Close();
cbxCOMPort->Items->Add("COM" + (i + 1).ToString());
comExistence = true;
throw 1;
}
catch(int& value)
{
continue;
}
}
if (comExistence)
{
cbxCOMPort->SelectedIndex = 0;
}
else
{
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() //设置串口的属性
{
// sp = gcnew SerialPort();
sp.PortName = cbxCOMPort->Text->Trim(); //设置串口名
sp.BaudRate = Int32::Parse (cbxBaudRate->Text->Trim()); //设置串口的波特率

        if (cbxStopBits->Text->Trim() == "0")    //设置停止位
        {
            sp.StopBits = StopBits::None;
        }
        else if (cbxStopBits->Text->Trim() == "1.5")
        {
            sp.StopBits = StopBits::OnePointFive;
        }
        else if (cbxStopBits->Text->Trim() == "2")
        {
            sp.StopBits = StopBits::Two;
        }
        else 
        {
            sp.StopBits = StopBits::One;
        }

        sp.DataBits = Int32::Parse(cbxDataBits->Text->Trim());    //设置数据位

        if (cbxParity->Text->Trim() == "奇校验")    //设置校验
        {
            sp.Parity = Parity::Odd;
        }
        else if (cbxParity->Text->Trim() == "偶校验")
        {
            sp.Parity = Parity::Even;
        }
        else
        {
            sp.Parity = Parity::None;
        }

        sp.ReadTimeout = -1;    //设置超时读取时间
        sp.RtsEnable = true;

        //定义DataReceived事件,当串口收到数据后触发事件
         sp.DataReceived +=   gcnew SerialDataReceivedEventHandler( sp,&serial_communication::Form1::sp_DataReceived);
        //this->btnCheckCom->Click += gcnew System::EventHandler(this, &Form1::btnCheckCom_Click);
        //mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        if (rbnHex->Checked)
        {
            isHex = true;
        }
        else
        {
            isHex = false;
        }
    }

private: void btnOpenCom_Click(Object sender, EventArgs e) //打开串口,btnopencom点击处理函数
{
if (isOpen == false)
{
if (!CheckPortSetting()) //检测串口设置
{
MessageBox::Show("串口未设置,错误提示");
return;
}
if (!isSetProperty)
{
SetPortProperty();
isSetProperty = true;
}
try
{
sp.Open();
isOpen = true;
btnOpenCom->Text = "关闭串口";
//串口打开后,相关的串口设置按钮不可再用
cbxCOMPort->Enabled = false;
cbxBaudRate->Enabled = false;
cbxDataBits->Enabled = false;
cbxStopBits->Enabled = false;
cbxParity->Enabled = false;
rbnChar->Enabled = false;
rbnHex->Enabled = false;
throw 1;
}
catch(int& value)
{
//打开串口失败后,相应标志位取消
//isSetProperty = false;
// isOpen = false;
// MessageBox::Show("串口无效或已被占用", "错误提示");
}
}
else
{
try
{
sp.Close();
isOpen = false;
isSetProperty = false;
btnOpenCom->Text = "打开串口";
//关闭串口后,串口设置选项可以继续使用
cbxCOMPort->Enabled = true;
cbxBaudRate->Enabled = true;
cbxDataBits->Enabled = true;
cbxStopBits->Enabled = true;
cbxParity->Enabled = true;
rbnChar->Enabled = true;
rbnHex->Enabled = true;
throw 1;
}
catch(int& value)
{
// MessageBox.Show("关闭串口时发生错误", "错误提示");
}
}
}

private: void btnSendData_Click(Object sender, EventArgs e) //发送数据,btnsenddata点击处理函数
{
if (isOpen)
{
try
{
sp.WriteLine(tbxSendData->Text);
throw 1;
}
catch(int& value)
{
//MessageBox::Show("发送数据时发生错误!", "错误提示");
//return;
}
}
else
{
MessageBox::Show("串口未打开", "错误提示");
return;
}
if (!CheckSendData())
{
MessageBox::Show("请输入要发送的数据!", "错误提示");
return;
}
}

private: void sp_DataReceived(Object sender, SerialDataReceivedEventArgs e) /////////
{

        System::Threading::Thread::Sleep(100);    //延时100ms等待接收完数据
        //this.Invoke就是跨线程访问ui的方法,也是文本的范例


            if (isHex == false)
            {
                tbxRecvData->Text += sp.ReadLine();
            }
            else
            {
                array<String^>^ ReceivedData = gcnew array<String^> (sp.BytesToRead);
               // sp.Read(ReceivedData, 0, ReceivedData->Length);
                String^ RecvDataText = nullptr;
                RecvDataText = sp.ReadExisting();
                for (int i = 0; i < ReceivedData->Length; i++)
                {
                    RecvDataText += ( ReceivedData[i] + " ");
                }
                tbxRecvData->Text += RecvDataText;
            }
            sp.DiscardInBuffer();

    }

private: void btnClearData_Click(Object sender, EventArgs e)
{
tbxRecvData->Text = "";
tbxSendData->Text = "";
}

protected:
    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::ComboBox^  cbxCOMPort;
protected: 
private: System::Windows::Forms::ComboBox^  cbxBaudRate;
private: System::Windows::Forms::ComboBox^  cbxStopBits;
private: System::Windows::Forms::ComboBox^  cbxParity;
private: System::Windows::Forms::ComboBox^  cbxDataBits;
private: System::Windows::Forms::Label^  label1;
private: System::Windows::Forms::Label^  label2;
private: System::Windows::Forms::Label^  label3;
private: System::Windows::Forms::Label^  label4;
private: System::Windows::Forms::Label^  label5;
private: System::Windows::Forms::GroupBox^  groupBox1;
private: System::Windows::Forms::TextBox^  tbxSendData;
private: System::Windows::Forms::GroupBox^  groupBox2;
private: System::Windows::Forms::TextBox^  tbxRecvData;
private: System::Windows::Forms::Button^  btnCheckCom;
private: System::Windows::Forms::Button^  btnOpenCom;
private: System::Windows::Forms::Button^  btnClearData;
private: System::Windows::Forms::Button^  btnSendData;
private: System::Windows::Forms::RadioButton^  rbnChar;
private: System::Windows::Forms::RadioButton^  rbnHex;

private: System::ComponentModel::IContainer^ components;

private:
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>

#pragma region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->cbxCOMPort = (gcnew System::Windows::Forms::ComboBox());
this->cbxBaudRate = (gcnew System::Windows::Forms::ComboBox());
this->cbxStopBits = (gcnew System::Windows::Forms::ComboBox());
this->cbxParity = (gcnew System::Windows::Forms::ComboBox());
this->cbxDataBits = (gcnew System::Windows::Forms::ComboBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->tbxSendData = (gcnew System::Windows::Forms::TextBox());
this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
this->tbxRecvData = (gcnew System::Windows::Forms::TextBox());
this->btnCheckCom = (gcnew System::Windows::Forms::Button());
this->btnOpenCom = (gcnew System::Windows::Forms::Button());
this->btnClearData = (gcnew System::Windows::Forms::Button());
this->btnSendData = (gcnew System::Windows::Forms::Button());
this->rbnChar = (gcnew System::Windows::Forms::RadioButton());
this->rbnHex = (gcnew System::Windows::Forms::RadioButton());
this->groupBox1->SuspendLayout();
this->groupBox2->SuspendLayout();
this->SuspendLayout();
//
// cbxCOMPort
//
this->cbxCOMPort->FormattingEnabled = true;
this->cbxCOMPort->Location = System::Drawing::Point(675, 50);
this->cbxCOMPort->Name = L"cbxCOMPort";
this->cbxCOMPort->Size = System::Drawing::Size(121, 20);
this->cbxCOMPort->TabIndex = 0;
//
// cbxBaudRate
//
this->cbxBaudRate->FormattingEnabled = true;
this->cbxBaudRate->Location = System::Drawing::Point(675, 89);
this->cbxBaudRate->Name = L"cbxBaudRate";
this->cbxBaudRate->Size = System::Drawing::Size(121, 20);
this->cbxBaudRate->TabIndex = 1;
//
// cbxStopBits
//
this->cbxStopBits->FormattingEnabled = true;
this->cbxStopBits->Location = System::Drawing::Point(675, 127);
this->cbxStopBits->Name = L"cbxStopBits";
this->cbxStopBits->Size = System::Drawing::Size(121, 20);
this->cbxStopBits->TabIndex = 2;
//
// cbxParity
//
this->cbxParity->FormattingEnabled = true;
this->cbxParity->Location = System::Drawing::Point(675, 172);
this->cbxParity->Name = L"cbxParity";
this->cbxParity->Size = System::Drawing::Size(121, 20);
this->cbxParity->TabIndex = 3;
//
// cbxDataBits
//
this->cbxDataBits->FormattingEnabled = true;
this->cbxDataBits->Location = System::Drawing::Point(675, 214);
this->cbxDataBits->Name = L"cbxDataBits";
this->cbxDataBits->Size = System::Drawing::Size(121, 20);
this->cbxDataBits->TabIndex = 4;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(626, 57);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(41, 12);
this->label1->TabIndex = 5;
this->label1->Text = L"串口号";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(628, 96);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(41, 12);
this->label2->TabIndex = 6;
this->label2->Text = L"波特率";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(628, 134);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(41, 12);
this->label3->TabIndex = 7;
this->label3->Text = L"停止位";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(628, 179);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(41, 12);
this->label4->TabIndex = 8;
this->label4->Text = L"校验位";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(628, 221);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(41, 12);
this->label5->TabIndex = 9;
this->label5->Text = L"数据位";
//
// groupBox1
//
this->groupBox1->Controls->Add(this->tbxSendData);
this->groupBox1->Location = System::Drawing::Point(29, 36);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(551, 100);
this->groupBox1->TabIndex = 10;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"发送数据";
//
// tbxSendData
//
this->tbxSendData->Location = System::Drawing::Point(16, 21);
this->tbxSendData->Multiline = true;
this->tbxSendData->Name = L"tbxSendData";
this->tbxSendData->Size = System::Drawing::Size(507, 59);
this->tbxSendData->TabIndex = 0;
//
// groupBox2
//
this->groupBox2->Controls->Add(this->tbxRecvData);
this->groupBox2->Location = System::Drawing::Point(29, 250);
this->groupBox2->Name = L"groupBox2";
this->groupBox2->Size = System::Drawing::Size(638, 326);
this->groupBox2->TabIndex = 11;
this->groupBox2->TabStop = false;
this->groupBox2->Text = L"接收数据";
//
// tbxRecvData
//
this->tbxRecvData->Location = System::Drawing::Point(16, 35);
this->tbxRecvData->Multiline = true;
this->tbxRecvData->Name = L"tbxRecvData";
this->tbxRecvData->ReadOnly = true;
this->tbxRecvData->Size = System::Drawing::Size(597, 285);
this->tbxRecvData->TabIndex = 0;
//
// btnCheckCom
//
this->btnCheckCom->Location = System::Drawing::Point(675, 267);
this->btnCheckCom->Name = L"btnCheckCom";
this->btnCheckCom->Size = System::Drawing::Size(75, 23);
this->btnCheckCom->TabIndex = 12;
this->btnCheckCom->Text = L"检测串口";
this->btnCheckCom->UseVisualStyleBackColor = true;
this->btnCheckCom->Click += gcnew System::EventHandler(this, &Form1::btnCheckCom_Click);
//
// btnOpenCom
//
this->btnOpenCom->Location = System::Drawing::Point(675, 312);
this->btnOpenCom->Name = L"btnOpenCom";
this->btnOpenCom->Size = System::Drawing::Size(75, 23);
this->btnOpenCom->TabIndex = 13;
this->btnOpenCom->Text = L"打开串口";
this->btnOpenCom->UseVisualStyleBackColor = true;
//
// btnClearData
//
this->btnClearData->Location = System::Drawing::Point(675, 361);
this->btnClearData->Name = L"btnClearData";
this->btnClearData->Size = System::Drawing::Size(75, 23);
this->btnClearData->TabIndex = 14;
this->btnClearData->Text = L"清空数据";
this->btnClearData->UseVisualStyleBackColor = true;
//
// btnSendData
//
this->btnSendData->Location = System::Drawing::Point(675, 404);
this->btnSendData->Name = L"btnSendData";
this->btnSendData->Size = System::Drawing::Size(75, 23);
this->btnSendData->TabIndex = 15;
this->btnSendData->Text = L"发送数据";
this->btnSendData->UseVisualStyleBackColor = true;
//
// rbnChar
//
this->rbnChar->AutoSize = true;
this->rbnChar->Checked = true;
this->rbnChar->Location = System::Drawing::Point(63, 172);
this->rbnChar->Name = L"rbnChar";
this->rbnChar->Size = System::Drawing::Size(71, 16);
this->rbnChar->TabIndex = 16;
this->rbnChar->TabStop = true;
this->rbnChar->Text = L"字符显示";
this->rbnChar->UseVisualStyleBackColor = true;
//
// rbnHex
//
this->rbnHex->AutoSize = true;
this->rbnHex->Location = System::Drawing::Point(182, 172);
this->rbnHex->Name = L"rbnHex";
this->rbnHex->Size = System::Drawing::Size(65, 16);
this->rbnHex->TabIndex = 17;
this->rbnHex->Text = L"HEX显示";
this->rbnHex->UseVisualStyleBackColor = true;
//
// Form1
//
this->AcceptButton = this->btnSendData;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(828, 611);
this->Controls->Add(this->rbnHex);
this->Controls->Add(this->rbnChar);
this->Controls->Add(this->btnSendData);
this->Controls->Add(this->btnClearData);
this->Controls->Add(this->btnOpenCom);
this->Controls->Add(this->btnCheckCom);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->groupBox1);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->cbxDataBits);
this->Controls->Add(this->cbxParity);
this->Controls->Add(this->cbxStopBits);
this->Controls->Add(this->cbxBaudRate);
this->Controls->Add(this->cbxCOMPort);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
this->groupBox2->ResumeLayout(false);
this->groupBox2->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();

    }

#pragma endregion

};
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-08-04 10:52
    关注
    private: void sp_DataReceived(Object sender, SerialDataReceivedEventArgs e)
    ->:
    private: void sp_DataReceived(Object^ sender, SerialDataReceivedEventArgs^ e)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画
  • ¥15 hadoop中启动hive报错如下怎么解决