SerialPort類的常用屬性及方法

2021-06-02 03:01:11 字數 4420 閱讀 3461

分類: 排隊叫號

vb.net

2011-04-05 21:17

36人閱讀收藏

舉報 一、  serialport類的常用屬性

名  稱

說  明

basestream

獲取 serialport 物件的基礎 stream 物件

baudrate

獲取或設定序列波特率

breakstate

獲取或設定中斷訊號狀態

bytestoread

獲取接收緩衝區中資料的位元組數

bytestowrite

獲取傳送緩衝區中資料的位元組數

cdholding

獲取埠的載波檢測行的狀態

ctsholding

獲取「可以傳送」行的狀態

databits

獲取或設定每個位元組的標準資料位長度

discardnull

獲取或設定乙個值,該值指示 null 位元組在埠和接收緩衝區之間傳輸時是否被忽略

dsrholding

獲取資料設定就緒 (dsr) 訊號的狀態

dtrenable

獲取或設定乙個值,該值在序列通訊過程中啟用資料終端就緒 (dtr) 訊號

encoding

獲取或設定傳輸前後文字轉換的位元組編碼

handshake

獲取或設定串列埠資料傳輸的握手協議

isopen

獲取乙個值,該值指示 serialport 物件的開啟或關閉狀態

newline

獲取或設定用於解釋 readline( )和writeline( )方法呼叫結束的值

parity

獲取或設定奇偶校驗檢查協議

名  稱

說  明

parityreplace

獲取或設定乙個位元組,該位元組在發生奇偶校驗錯誤時替換資料流中的無效位元組

portname

獲取或設定通訊埠,包括但不限於所有可用的 com 埠

readbuffersize

獲取或設定 serialport 輸入緩衝區的大小

readtimeout

獲取或設定讀取操作未完成時發生超時之前的毫秒數

receivedbytesthreshold

獲取或設定 datareceived 事件發生前內部輸入緩衝區中的位元組數

rtsenable

獲取或設定乙個值,該值指示在序列通訊中是否啟用請求傳送 (rts) 訊號

stopbits

獲取或設定每個位元組的標準停止位數

writebuffersize

獲取或設定串列埠輸出緩衝區的大小

writetimeout

獲取或設定寫入操作未完成時發生超時之前的毫秒數

二、方法

方 法 名 稱

說  明

close

關閉埠連線,將 isopen 屬性設定為false,並釋放內部 stream 物件

open

開啟乙個新的串列埠連線

read

從 serialport 輸入緩衝區中讀取

readbyte

從 serialport 輸入緩衝區中同步讀取乙個位元組

readchar

從 serialport 輸入緩衝區中同步讀取乙個字元

readline

一直讀取到輸入緩衝區中的 newline 值

readto

一直讀取到輸入緩衝區中指定 value 的字串

write

已過載。將資料寫入串列埠輸出緩衝區

writeline

將指定的字串和 newline 值寫入輸出緩衝區

三、資料傳送示例

private sub fasong()

serialport1.portname = "com1"

serialport1.baudrate = 9600

serialport1.open()

dim data as byte() = encoding.unicode.getbytes(textbox1.text)

dim str as string = convert.tobase64string(data)

serialport1.writeline(str)

messagebox.show("資料傳送成功!", "系統提示")

end sub

四、接受示例

private sub jieshou()

dim data as byte() = convert.frombase64string(serialport1.readline())

textbox2.text = encoding.unicode.getstring(data)

serialport1.close()

messagebox.show("資料接收成功!", "系統提示")

end sub

五、通過串列埠關閉對方計算機

(1)新建乙個專案,命名為ex13_02,預設窗體為form1。

(2)在form1窗體中,主要新增兩個button控制項,分別用於開啟通訊串列埠和關閉對方計算機。

(3)主要程式**。

private sub button1_click(sender as object, e as eventargs)

'開啟串列埠

serialport1.portname = "com1"

serialport1.open()

button1.enabled = false

button2.enabled = true

end sub

'資料接收事件,等待接收關機命令

private sub serialport1_datareceived(sender as object, e as serialdatareceivedeventargs)

dim data as byte() = convert.frombase64string(serialport1.readline())

dim str as string = encoding.unicode.getstring(data)

serialport1.close()

if str = "關機" then

dim p as new process()

p.startinfo.filename = "cmd.exe"

p.startinfo.useshellexecute = false

p.startinfo.redirectstandardinput = true

p.startinfo.redirectstandardoutput = true

p.startinfo.redirectstandarderror = true

p.startinfo.createnowindow = true

p.start()

p.standardinput.writeline("shutdown /s")

p.standardinput.writeline("exit")

end if

end sub

'傳送關機命令

private sub button2_click(sender as object, e as eventargs)

if button2.text = "關閉計算機" then

'傳送關機命令資料

dim data as byte() = encoding.unicode.getbytes("關機")

dim str as string = convert.tobase64string(data)

serialport1.writeline(str)

button2.text = "取消關機"

else

button2.text = "關閉計算機"

button1.enabled = true

button2.enabled = false

'取消關機

dim p as new process()

p.startinfo.filename = "cmd.exe"

p.startinfo.useshellexecute = false

p.startinfo.redirectstandardinput = true

p.startinfo.redirectstandardoutput = true

p.startinfo.redirectstandarderror = true

p.startinfo.createnowindow = true

p.start()

p.standardinput.writeline("shutdown /a")

p.standardinput.writeline("exit")

end if

end sub

SerialPort類讀取資料的方法

同步方法 read readline readbyte readchar 同步就是和主程式保持一致,只有執行完了readbyte之後才能執行程式之後的 非同步方法 readexisting readto 非同步就是重新開啟乙個執行緒來處理這些問題,主程式不受到干擾,繼續執行。一般選擇的是read同步...

C 中SerialPort類 隨筆

有時,對串列埠供電需要設定dtrenable 和rtsenable 兩個屬 在開發中有些串列埠裝置需要串列埠供電,使用c 中的serialport類預設情況下不會出發 datareceived函式,但使用超級終端卻可以接收到資料,這是因為 serialport 類的dtrenable 和rtsena...

C Json JsonProperty 常用屬性

public class testdatalistentity jsonproperty nullvaluehandling nullvaluehandling.ignore public string testname jsonproperty nullvaluehandling nullvalu...