winform访问被拒绝_c#串口提示端口访问被拒绝
如下代碼請教大神====使用是串口調試助手進行調試,但是在調試的時候出現老是提示“端口訪問拒絕”,如果把串口調試助手關掉就不出現這個問題,請問如何解決====usingSystem;usingSyst...
如下代碼請教大神
====
使用是串口調試助手進行調試,但是在調試的時候出現老是提示“端口訪問拒絕”, 如果把串口調試助手關掉就不出現這個問題,請問如何解決
====
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);
}
}
}
展開
總結
以上是生活随笔為你收集整理的winform访问被拒绝_c#串口提示端口访问被拒绝的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 严援朝的一句名言
- 下一篇: 万博武汉与中关村信息谷签订战略协议,协力
