国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

JustinIO的vb.NET版本!(串口操作類)

2024-07-10 13:00:52
字體:
來源:轉載
供稿:網友
imports system
imports system.runtime.interopservices

namespace justinio
class commport
public portnum as string
public baudrate as integer
public bytesize as byte
public parity as byte '// 0-4=no,odd,even,mark,space
public stopbits as byte '// 0,1,2 = 1, 1.5, 2
public readtimeout as integer '//comm port win32 file handle
private hcomm as integer = -1
public opened as boolean = false
'//win32 api constants
private const generic_read as int64 = &h80000000
private const generic_write as int64 = &h40000000
private const open_existing as integer = 3
private const invalid_handle_value as integer = -1

#region "struct"

<structlayout(layoutkind.sequential)> _
public structure dcb
'//taken from c struct in platform sdk
public dcblength as integer '// sizeof(dcb)
public baudrate as integer '// 指定當前波特率 current baud rate
'// these are the c struct bit fields, bit twiddle flag to set
public fbinary as integer '// 指定是否允許二進制模式,在windows95中必須主true binary mode, no eof check
public fparity as integer '// 指定是否允許奇偶校驗 enable parity checking
public foutxctsflow as integer '// 指定cts是否用于檢測發送控制,當為true是cts為off,發送將被掛起。 cts output flow control
public foutxdsrflow as integer '// 指定cts是否用于檢測發送控制 dsr output flow control
public fdtrcontrol as integer '// dtr_control_disable值將dtr置為off, dtr_control_enable值將dtr置為on, dtr_control_handshake允許dtr"握手" dtr flow control type
public fdsrsensitivity as integer '// 當該值為true時dsr為off時接收的字節被忽略 dsr sensitivity
public ftxcontinueonxoff as integer '// 指定當接收緩沖區已滿,并且驅動程序已經發送出xoffchar字符時發送是否停止。true時,在接收緩沖區接收到緩沖區已滿的字節xofflim且驅動程序已經發送出xoffchar字符中止接收字節之后,發送繼續進行。 false時,在接收緩沖區接收到代表緩沖區已空的字節xonchar且驅動程序已經發送出恢復發送的xonchar之后,發送繼續進行。xoff continues tx
public foutx as integer '// true時,接收到xoffchar之后便停止發送接收到xonchar之后將重新開始 xon/xoff out flow control
public finx as integer '// true時,接收緩沖區接收到代表緩沖區滿的xofflim之后,xoffchar發送出去接收緩沖區接收到代表緩沖區空的xonlim之后,xonchar發送出去 xon/xoff in flow control
public ferrorchar as integer '// 該值為true且fparity為true時,用errorchar 成員指定的字符代替奇偶校驗錯誤的接收字符 enable error replacement
public fnull as integer '// etrue時,接收時去掉空(0值)字節 enable null stripping
public frtscontrol as integer '// rts flow control
'/*rts_control_disable時,rts置為off
' *   rts_control_enable時, rts置為on
' *    rts_control_handshake時,
' *    當接收緩沖區小于半滿時rts為on
' *     當接收緩沖區超過四分之三滿時rts為off
' *    rts_control_toggle時,
' *    當接收緩沖區仍有剩余字節時rts為on ,否則缺省為off*/
public fabortonerror as integer '// true時,有錯誤發生時中止讀和寫操作 abort on error
public fdummy2 as integer '// 未使用 reserved
public flags as int64
public wreserved as uint16 '// 未使用,必須為0 not currently used
public xonlim as uint16 '// 指定在xon字符發送這前接收緩沖區中可允許的最小字節數 transmit xon threshold
public xofflim as uint16 '// 指定在xoff字符發送這前接收緩沖區中可允許的最小字節數 transmit xoff threshold
public bytesize as byte '// 指定端口當前使用的數據位 number of bits/byte, 4-8
public parity as byte '// 指定端口當前使用的奇偶校驗方法,可能為:evenparity,markparity,noparity,oddparity 0-4=no,odd,even,mark,space
public stopbits as byte '// 指定端口當前使用的停止位數,可能為:onestopbit,one5stopbits,twostopbits 0,1,2 = 1, 1.5, 2
public xonchar as char '// 指定用于發送和接收字符xon的值 tx and rx xon character
public xoffchar as char '// 指定用于發送和接收字符xoff值 tx and rx xoff character
public errorchar as char '// 本字符用來代替接收到的奇偶校驗發生錯誤時的值 error replacement character
public eofchar as char '// 當沒有使用二進制模式時,本字符可用來指示數據的結束 end of input character
public evtchar as char '// 當接收到此字符時,會產生一個事件 received event character
public wreserved1 as uint16 '// 未使用 reserved; do not use
end structure
<structlayout(layoutkind.sequential)> _
private structure commtimeouts
public readintervaltimeout as integer
public readtotaltimeoutmultiplier as integer
public readtotaltimeoutconstant as integer
public writetotaltimeoutmultiplier as integer
public writetotaltimeoutconstant as integer
end structure
<structlayout(layoutkind.sequential)> _
private structure overlapped
public internal as integer
public internalhigh as integer
public offset as integer
public offsethigh as integer
public hevent as integer
end structure

#end region

#region "windows api"
<dllimport("kernel32.dll", charset:=charset.auto)> _
private shared function createfile( _
byval lpfilename as string, _
byval dwdesiredaccess as uint32, _
byval dwsharemode as integer, _
byval lpsecurityattributes as integer, _
byval dwcreationdisposition as integer, _
byval dwflagsandattributes as integer, _
byval htemplatefile as integer) as integer
end function
'參數介紹:
'lpfilename 要打開的串口名稱
'dwdesiredaccess 指定串口的訪問方式,一般設置為可讀可寫方式
'dwsharemode 指定串口的共享模式,串口不能共享,所以設置為0
'lpsecurityattributes 設置串口的安全屬性,win9x下不支持,應設為null
'dwcreationdisposition 對于串口通信,創建方式只能為open_existing
'dwflagsandattributes 指定串口屬性與標志,設置為file_flag_overlapped(重疊i/o操作),指定串口以異步方式通信
'htemplatefile 對于串口通信必須設置為null
<dllimport("kernel32.dll")> _
private shared function getcommstate(byval hfile as integer, byref lpdcb as dcb) as boolean
end function
'參數介紹
'hfile 通信設備句柄
'dcb lpdcb 設備控制塊dcb

<dllimport("kernel32.dll")> _
private shared function buildcommdcb(byval lpdef as string, byref lpdcb as dcb) as boolean
end function
'參數介紹
'lpdef 設備控制字符串
'lpdcb 設備控制塊dcb
<dllimport("kernel32.dll")> _
private shared function setcommstate(byval hfile as integer, byref lpdcb as dcb) as boolean
end function
'參數說明
'hfile 通信設備句柄
'lpdcb 設備控制塊
<dllimport("kernel32.dll")> _
private shared function getcommtimeouts(byval hfile as integer, byref lpcommtimeouts as commtimeouts) as boolean
end function
'參數說明
'hfile 通信設備句柄 handle to comm device
'lpcommtimeouts 超時時間 time-out values
<dllimport("kernel32.dll")> _
private shared function setcommtimeouts(byval hfile as integer, byref lpcommtimeouts as commtimeouts) as boolean
end function
'參數說明
'hfile 通信設備句柄 handle to comm device
'lpcommtimeouts 超時時間 time-out values
<dllimport("kernel32.dll")> _
private shared function readfile(byval hfile as integer, byval lpbuffer() as byte, _
byval nnumberofbytestoread as integer, byref lpnumberofbytesread as integer, _
byref lpoverlapped as overlapped) as boolean
end function
'參數說明
'hfile 通信設備句柄 handle to file
'lpbuffer 數據緩沖區 data buffer
'nnumberofbytestoread 多少字節等待讀取 number of bytes to read
'lpnumberofbytesread 讀取多少字節 number of bytes read
'lpoverlapped 溢出緩沖區 overlapped buffer
<dllimport("kernel32.dll")> _
private shared function writefile(byval hfile as integer, byval lpbuffer() as byte, _
byval nnumberofbytestoread as integer, byref lpnumberofbyteswritten as integer, _
byref lpoverlapped as overlapped) as boolean
end function
'參數介紹
'hfile 通信設備句柄 handle to file
'lpbuffer 數據緩沖區 data buffer
'nnumberofbytestowrite 多少字節等待寫入 number of bytes to write
'lpnumberofbyteswritten 已經寫入多少字節 number of bytes written
'lpoverlapped 溢出緩沖區 overlapped buffer
<dllimport("kernel32.dll")> _
private shared function closehandle(byval hobject as integer) as boolean
end function
'參數hobject 要被關閉的句柄 handle to object
<dllimport("kernel32.dll")> _
private shared function getlasterror() as uint32
end function
#end region
public sub open()
dim dcbcommport as new dcb
dim ctocommport as new commtimeouts
'// 打開串口 open the comm port.
hcomm = createfile(portnum, uint32.parse(generic_read or generic_write), 0, 0, open_existing, 0, 0)
'// 如果串口沒有打開,就打開 if the port cannot be opened, bail out.
if hcomm = invalid_handle_value then
throw (new applicationexception("非法操作,不能打開串口!"))
end if
'// 設置通信超時時間 set the comm timeouts.
getcommtimeouts(hcomm, ctocommport) 'ref
ctocommport.readtotaltimeoutconstant = readtimeout
ctocommport.readtotaltimeoutmultiplier = 0
ctocommport.writetotaltimeoutmultiplier = 0
ctocommport.writetotaltimeoutconstant = 0
setcommtimeouts(hcomm, ctocommport) 'ref
'// 設置串口 set baud rate, parity, word size, and stop bits.
getcommstate(hcomm, dcbcommport) 'ref
dcbcommport.baudrate = baudrate
dcbcommport.flags = 0
'//dcb.fbinary=1;
dcbcommport.flags = dcbcommport.flags or (1)
if parity > 0 then
'//dcb.fparity=1
dcbcommport.flags = dcbcommport.flags or (2)
end if
dcbcommport.parity = parity
dcbcommport.bytesize = bytesize
dcbcommport.stopbits = stopbits
if setcommstate(hcomm, dcbcommport) = false then
'//uint errornum=getlasterror();
throw (new applicationexception("非法操作,不能打開串口!"))
end if
'//unremark to see if setting took correctly
'//dcb dcbcommport2 = new dcb();
'//getcommstate(hcomm, ref dcbcommport2);
opened = true
end sub
public sub close()
if hcomm <> invalid_handle_value then
closehandle(hcomm)
end if
end sub
public function read(byval numbytes as integer) as byte()
dim bufbytes(numbytes) as byte
dim outbytes(0) as byte
if (hcomm <> invalid_handle_value) then
dim ovlcommport as new overlapped
dim bytesread as integer = 0
readfile(hcomm, bufbytes, numbytes, bytesread, ovlcommport)
array.copy(bufbytes, outbytes, bytesread)
else
throw (new applicationexception("串口未打開!"))
end if
return outbytes
end function

public sub write(byval writebytes as byte())
if (hcomm <> invalid_handle_value) then
dim ovlcommport as new overlapped
dim byteswritten as integer
writefile(hcomm, writebytes, writebytes.length, byteswritten, ovlcommport)
else
throw (new applicationexception("串口未打開!"))
end if
end sub
end class
end namespace




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄陵县| 阿拉善左旗| 宣汉县| 泰和县| 开化县| 宣武区| 仙居县| 昌平区| 镇坪县| 辽阳县| 洛阳市| 麻城市| 赤水市| 临湘市| 衡阳市| 灌云县| 年辖:市辖区| 凌云县| 洛南县| 安阳县| 乃东县| 浮山县| 甘孜县| 科尔| 曲沃县| 浑源县| 嘉定区| 德昌县| 贺州市| 东阳市| 西峡县| 文山县| 达日县| 信宜市| 仙居县| 鹤峰县| 灵寿县| 肥城市| 宝兴县| 长兴县| 当阳市|