“COMFunctions”不包含“ConfigurationAndOpen”的定义

问题描述 投票:0回答:1

我正在尝试创建一个 comport 类和表单 1,最后创建其他表单,并能够在 fomrs 中实例化 comport 类,并能够从这两个类发送数据和接收数据。但我收到错误!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Management;
using System.Windows.Forms;
using System.Configuration;


namespace FindAvailableSerial_Port1
{

    #region Docs
    #endregion
    public class COMFunctions
    {
        #region Fields

        public string portNum;
        public int baudRate;
        public SerialPort port = new SerialPort();
        public List<string> receivedStrings = new List<string>();
        #endregion

        #region Constructor
        #endregion

        #region Methods
        public List<string> GetPorts()
        {
            using (var searcher = new ManagementObjectSearcher("SELECT * FROM" +
             "Win32_PnPEntity WHERE cAPTION LIKE '%(com%'"))
            {
                string[] portnames = SerialPort.GetPortNames();

                List<string> list = portnames.Select(n => n + "-" + searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString()).FirstOrDefault(s => s.Contains(n))).ToList();
                List<string> portList = list;
                return portList;

            }
        }

        public void ConfigureAndOpen(string portNum, int baudRate)
        {
            port.BaudRate = baudRate;
            port.PortName = "COM" + portNum;

            while (port.IsOpen==false)
            {
                try
                {
                    port.Open();
                }
                catch (System.Exception)
                {
                    MessageBox.Show("Device is not connected!");
                }

            }

        }
        #endregion
        public void Send(string command)
        {
            port.WriteLine(command);
        }
        public string Read()
        {
            if(port.IsOpen)
            {
                string receivedStr = port.ReadExisting();
                return receivedStr;
            }
            else
            {
                return "";  
            }
        }

        //internal void ConfigurationAndOpen(string comNumber, int v)
        //{
            //throw new NotImplementedException();
        //}
    }
}
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.Management;
using System.IO.Ports;
using System.Security.Cryptography.X509Certificates;

namespace FindAvailableSerial_Port1
{
    public partial class Form1 : Form
    {
        //Instantiating the COMFunction class
        COMFunctions myCOM = new COMFunctions();

        public Form1()
        {
            InitializeComponent();
            txtCOM.Text = "7";
            List<string> ports = myCOM.GetPorts();

            foreach (string s in ports)
            {
                txtCOM.AppendText(s + "\r\n");
            }
        }
        #region Event Handlers
        private void btnOpen_Click(object sender, EventArgs e)
        {
            string comNumber = txtCOM.Text;
            myCOM.ConfigurationAndOpen(comNumber, 9600);

            txbPortNames.AppendText("COM"+ comNumber + "open!" + "\r\n");
            txbPortNames.AppendText("waiting for data..."+ "\r\n");

            timer1.Enabled = true;
        }
        #endregion
        private void btnSend_Click(object sender, EventArgs e)
        {
            string cmd = textBox2.Text;
            myCOM.Send(cmd);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string receivedStr = myCOM.Read();
            if (string.IsNullOrEmpty(receivedStr)== false)
            {
                txbPortNames.AppendText(receivedStr + "\r\n|)");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

这是我从 form1 得到的错误:

“COMFunctions”不包含“ConfigurationAndOpen”的定义,并且找不到接受“COMFunctions”类型的第一个参数的可访问扩展方法“ConfigurationAndOpen”(您是否缺少 using 指令或程序集引用?)

c# .net visual-studio com-port
1个回答
0
投票

修复代码部分

  public List<string> GetPorts()
  {
      using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
      {
          string[] portnames = SerialPort.GetPortNames();

          var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

          var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();

          return portList;

      }
  }
© www.soinside.com 2019 - 2024. All rights reserved.