c winform 串列埠程式設計

2021-09-30 10:18:22 字數 1272 閱讀 4679

這裡結合看到的一些知識和在實際專案中應用的一些方法說明一下如何在.net平台下使用c#建立串列埠通訊程式。在.net 2.0中提供了串列埠通訊的功能,其功能的實現主要是system.io.ports 命名空間下實現的。可以通過加入這個命名空間來實現串列埠通訊方面的程式。

首先system.io.ports命名空間中最重用的是serialport 類。

1、通過例項化乙個serialport 類物件可以控制以後的通訊過程。

code:

//create a serial port object

serialport sp = new serialport ();

2、設定串列埠屬性

code:

sp.portname = "com2";

//串口號

sp.baudrate 

=9600;//

波特率sp.parity  

=parity.none;

//無奇偶校驗位

sp.stopbits 

=stopbits.two;

//兩個停止位

sp.databits

= 8 //資料位 

其他屬性可以參考 msdn 、、、                    

3、開啟串列埠

code:

if(!sp.isopen)

4、讀取串列埠資料

sp.readbyte();讀取乙個位元組的資料

sp.readchar();讀取乙個字元的資料

sp.readline();一直讀取到輸入緩衝區的newline值:返回string型別

sp.readexisting();讀取可用的位元組

sp.readto(string value);讀取資料,直到讀到該value時停止

5、向串列埠傳送資料

sp.write(string);寫入資料

sp.write(byte, int32, int32);寫入byte 陣列

sp.write(char, int32, int32); 寫入字串陣列

sp.writeline(string value);將指定的value值與換行符一起寫入

6、關閉串列埠

sp.close()

在屬性設定時 也可以設定

sp.datareceived += new serialdatareceivedevenhandler(datareceive_method);這種方式可以在接收到資料時呼叫datareceive_method方法。當然也可以手動去控制讀取的方式。。。。。

C winform串列埠使用及串列埠除錯工具

串列埠傳輸serialport 定時器 timer c weightconfig.xml檔案寫法,串列埠配置檔案 xml version 1.0 encoding utf 8 com2 portname s 串列埠名稱 u w startstring s 串列埠讀取到的資料格式,既電子稱返回的資料格...

C winform 程式設計例項

一,textbox實現autocomplete功能 原始碼 包含自動完成的字串集合 autocompletestringcollection strings new autocompletestringcollection strings.add a strings.add abc strings....

基於C Winform的串列埠資料接收

首先新增串列埠 自行拖拽 csharp view plain copy serialport serialport1 new serialport com2 9600,parity.none,8,stopbits.one 初始化串列埠設定 建立乙個資料接收方法 csharp view plain c...