c# codeusing 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 demo
public partial class myform : form
//private int receivedata = new int[4];
//private int receivedataindex = 0;
string receivedata;
public myform()
initializecomponent();
private void myform_load(object sender, eventargs e)
comboboxportname.selectedindex = 0;
comboboxbaudrate.selectedindex = 5;
comboboxparity.selectedindex = 0;
comboboxdatabits.selectedindex = 0;
comboboxstopbits.selectedindex = 0;
btnclose.enabled = false;
btnopen.enabled = true;
textboxinformation.text = "系統初始化成功!\r\n";
private void btnopen_click(object sender, eventargs e)
string myparity;
string mystopbits;
myparity = comboboxparity.selecteditem.tostring();
mystopbits = comboboxstopbits.selecteditem.tostring();
//設定埠號
myserialport.portname = comboboxportname.selecteditem.tostring();
//設定波特率
myserialport.baudrate = convert.toint32(comboboxbaudrate.selecteditem);
//設定校驗位
switch (myparity)
case "none":
myserialport.parity = parity.none;
break;
case "even":
myserialport.parity = parity.even;
break;
case "odd":
myserialport.parity = parity.odd;
break;
default:
myserialport.parity = parity.none;
break;
//設定資料位
myserialport.databits = convert.toint32(comboboxdatabits.selecteditem);
//設定停止位
switch(mystopbits)
case "1":
myserialport.stopbits = stopbits.one;
break;
case "2":
myserialport.stopbits = stopbits.two;
break;
default:
myserialport.stopbits = stopbits.one;
break;
//採用ascii編碼方式
myserialport.encoding = encoding.ascii;
//接收到乙個字元就出發接收事件
myserialport.receivedbytesthreshold = 1;
//試圖開啟指定串列埠
tryif (myserialport.isopen == false)
myserialport.open(); //開啟串列埠
btnclose.enabled = true; //關閉按鍵失能
btnopen.enabled = false; //開啟按鍵失能
textboxinformation.scrolltocaret();
//開啟異常,輸出提示資訊
catch
messagebox.show("串列埠開啟異常","提示資訊",messageboxbuttons.ok,messageboxicon.warning);
private void btnclose_click(object sender, eventargs e)
if (myserialport.isopen == true)
myserialport.close(); //關閉串列埠
btnclose.enabled = false;
btnopen.enabled = true;
textboxinformation.scrolltocaret();
private void myserialport_datareceived(object sender, serialdatareceivedeventargs e)
string ch = myserialport.readexisting();
switch (ch)
case "\x12" :
//接收到串口頭
//清空接收陣列
receivedata = string.empty;
break;
case "\x14" :
//接收到串列埠尾
//在擁有此控制項的基礎視窗控制代碼的執行緒上執行委託invoke(delegate)
//即在控制項textboxinformation的父視窗form中執行委託.
textboxinformation.invoke
new methodinvoker
delegate
textboxreceivedata.text = receivedata;
break;
default:
receivedata += ch;
break;
c 口口亂碼 C 執行控制台程式中文亂碼
開始以為是控制台的原因,費了點勁 網上的修改登錄檔等方法都不行,後面發現 c windows system32 cmd.exe 視窗,直接輸入 chcp 65001會直接儲存。然後process的屬性裡加上路徑 process.startinfo.filename c windows system3...
LabView中串列埠通訊讀取資料出現亂碼的情況說明
因為通訊過程中不定時,偶爾,隨機出現傳送資料亂碼,困擾了我好多天,在各大論壇上也未找到解決辦法,問了很多於是精簡自己的程式框圖,從最基本的串列埠讀寫入手,尋找問題,發現是因為自己把串列埠配置函式放在了大迴圈while裡面,而且等待時間設定不合理,導致每次迴圈時,又會重新去配置串列埠,造成了延時,導致...
C 非同步資料接收串列埠操作類
使用c 呼叫傳統32位api實現串列埠操作,整個結構特別的簡單。接收資料只需要定義資料接收事件即可。using system using system.runtime.interopservices namespace ibms.tool.io this.recvdata recvdata 返回接收...