通过C#中的串口接收数据或访问端口' COM5'被拒绝

时间:2015-07-12 22:40:07

标签: c# serial-port

我想从串口接收数据进入我的c#程序的富文本框但是当我选择端口并点击阅读按钮时,它会给我这个错误:访问端口' COM1&# 39;被拒绝我不知道我的错误是什么,请帮助我即时回复将不胜感激

using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

namespace Serial_receive
{
    public partial class Form1 : Form
    {
        private SqlConnection xConn;
        public Form1()
        {
            InitializeComponent();
            xConn = new SqlConnection("Server=.; UID=sa; PWD=123; DataBase=ComportDB;");
            viewdata();
        }
        private void viewdata()
        {
            DataTable xtable = new DataTable();
            new SqlDataAdapter("select * from tbldata", xConn).Fill(xtable);
            dataGridView1.DataSource = xtable;
            //dataGridView1.Columns[0].Width = 150;
            //dataGridView1.Columns[1].Width = 137;
        }
        private void SaveData()
        {
            using (var con = xConn)
            using (var cmd = con.CreateCommand())
            {
                cmd.CommandText = "insert into tbldata values(@date, @text)";
                string[] lines = richTextBox1.Lines;

                foreach (var line in lines)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now;
                    // Change your first column type to datetime or datetime2
                    cmd.Parameters.Add("@text", SqlDbType.NVarChar).Value = line;
                    // I assume your second column is nvarchar

                    if (con.State != ConnectionState.Open)
                        con.Open();

                    cmd.ExecuteNonQuery();
                }
            }
        }
       //search button  
        private void button1_Click(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }
       /// read button
        string t;

        private void button2_Click(object sender, EventArgs e)
        {
            t = comboBox1.Text.ToString();
            sErial(t);
        }
       //method
        SerialPort sp;
        void sErial(string Port_name)
        {
            sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
            sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            sp.Open();
        }
//
        private  void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
    {

        SerialPort sp = (SerialPort)sender;

                string w = sp.ReadLine();

                //string msg = sp.ReadExisting();
                if (w != String.Empty)
                {
                    Invoke(new Action(() => richTextBox1.AppendText(w)));
                }


        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveData();     
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            String a;
            a = serialPort1.ReadByte().ToString();
        }


    }
}

0 个答案:

没有答案