c#串口提示端口访问被拒绝

如下代码请教大神
====
使用是串口调试助手进行调试,但是在调试的时候出现老是提示“端口访问拒绝”, 如果把串口调试助手关掉就不出现这个问题,请问如何解决
====
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
SerialPort serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); //初始化串口设置
public delegate void Displaydelegate(byte[] InputBuf);
Byte[] OutputBuf = new Byte[128];
public Displaydelegate disp_delegate;

public Form1()
{
disp_delegate = new Displaydelegate(DispUI);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(Comm_DataReceived);

InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
if (button1.Text == "打开")
{
if (!serialPort1.IsOpen)
{ serialPort1.Open(); }
button1.Text = "关闭";
}
else
{
serialPort1.Close();
button1.Text = "打开";
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}

void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

Byte[] InputBuf = new Byte[128];

try
{
serialPort1.Read(InputBuf, 0, serialPort1.BytesToRead); //读取缓冲区的数据直到“}”即0x7D为结束符
//InputBuf = UnicodeEncoding.Default.GetBytes(strRD); //将得到的数据转换成byte的格式
System.Threading.Thread.Sleep(50);
this.Invoke(disp_delegate, InputBuf);

}
catch (TimeoutException ex) //超时处理
{
MessageBox.Show(ex.ToString());
}
}

public void DispUI(byte[] InputBuf)
{
//textBox1.Text = Convert.ToString(InputBuf);

ASCIIEncoding encoding = new ASCIIEncoding();
richTextBox1.Text = encoding.GetString(InputBuf);
}
}
}

第1个回答  2018-04-13
一个串口号只能由一个程序开启使用,调试运行程序前把串口调试助手关闭就行
我用的Techcon串口大师,开启关闭很方便
第2个回答  2018-04-13
串口端口是独占的,同一时刻只能由一个程序使用本回答被提问者和网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网