日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

winform访问被拒绝_c#串口提示端口访问被拒绝

發布時間:2024/3/12 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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#串口提示端口访问被拒绝的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。