成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

Pocket PC使用VB.NET操作串口的說明

2010-08-28 10:50:35來源:西部e網(wǎng)作者:

首先,從coredll.dll中導(dǎo)入下列函數(shù),其中有文件讀寫的函數(shù)例如CreateFile、ReadFile、WriteFile以及CloseHandle,分別用來打開、讀、寫和關(guān)閉文件。因為在WinCE中,使用API操作串口的話,串口是當(dāng)作一個文件來處理的。另外主要要用的就是GetCommState和SetCommState,

用來獲取和設(shè)置串口的狀態(tài),例如波特率,數(shù)據(jù)位,停止位,奇偶校驗等等。以下是導(dǎo)入這些函數(shù)的語句。

<DllImport("coredll.dll")> _
    Private Shared Function CreateFile _
    (ByVal lpFileName As String, _
     ByVal dwDesiredAccess As Integer, _
     ByVal dwShareMode As Integer, _
     ByVal lpSecurityAttributes As Integer, _
     ByVal dwCreationDisposition As Integer, _
     ByVal dwFlagAndAttributes As Integer, _
     ByVal hTemplateFile As Integer) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function GetCommState _
    (ByVal hFile As Integer, _
     ByVal mdcb As dcb) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function SetCommState _
    (ByVal hFile As Integer, _
     ByVal mdcb As dcb) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function ReadFile _
    (ByVal hFile As Integer, _
     ByVal Buffer() As Byte, _
     ByVal nNumberOfBytesToRead As Integer, _
     ByRef lpNumberOfBytesRead As Integer, _
     ByRef lpOverlapped As Integer) As Integer
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function WriteFile _
    (ByVal hFile As Integer, _
     ByVal Buffer() As Byte, _
     ByVal nNumberOfBytesToWrite As Integer, _
     ByRef lpNumberOfBytesWritten As Integer, _
     ByVal lpOverlapped As Integer) As Boolean
    End Function

    <DllImport("coredll.dll")> _
    Private Shared Function CloseHandle _
    (ByVal hObject As Integer) As Integer
    End Function

另外,需要定義一個dcb結(jié)構(gòu)體,用來存儲串口的狀態(tài)。

Public Class dcb
        Friend DCBlength As UInt32
        Friend BaudRate As UInt32 波特率
        Friend fBinary As UInt32
        Friend fParity As UInt32
        Friend fOutxCtsFlow As UInt32
        Friend fOutxDsrFlow As UInt32
        Friend fDtrControl As UInt32
        Friend fDsrSensitivity As UInt32
        Friend fTXContinueOnXoff As UInt32
        Friend fOutX As UInt32
        Friend fInX As UInt32
        Friend fErrorChar As UInt32
        Friend fNull As UInt32
        Friend fRtsControl As UInt32
        Friend fAbortOnError As UInt32
        Friend fDummy2 As UInt32
        Friend wReserved As UInt32
        Friend XonLim As UInt32
        Friend XoffLim As UInt32
        Friend ByteSize As Byte  數(shù)據(jù)位
        Friend Parity As Byte  奇偶校驗
        Friend StopBits As Byte  停止位
        Friend XonChar As Char
        Friend XoffChar As Char
        Friend ErrorChar As Char
        Friend EofChar As Char
        Friend EvtChar As Char
        Friend wReserved1 As UInt16
    End Class

以上只標(biāo)注了主要的參數(shù),其他參數(shù)可以依據(jù)需要進(jìn)行查看,包含一些控制和讀取Modem狀態(tài)的參數(shù)等等。

1.打開串口
CreateFile(文件名,讀寫方式,共享模式,安全屬性,對文件是否已存在的處理,文件屬性以及標(biāo)記,模板文件)
該函數(shù)如果打開成功,將返回一個已經(jīng)打開的文件的句柄,可以使用這個句柄對文件進(jìn)行其他操作。
在WinCE中,第四個參數(shù)是忽略了的。其他的數(shù)值在處理文件的時候會依據(jù)需要有所不同,處理串口則使用一種參數(shù)就基本可以。
打開串口,用這樣的語句就可以了
CreateFile ("COM1:", &HC0000000, 0, 0, 3, 0, 0)

2.設(shè)置串口參數(shù)
使用GetCommState和SetCommState函數(shù)
因為dcb這個結(jié)構(gòu)體定義的變量的初始值和當(dāng)前串口的參數(shù)是不同的,我們修改串口參數(shù)并不希望將不需要的參數(shù)也改動,甚至是改錯.所以先用
GetCommState將當(dāng)前串口狀態(tài)讀入這個變量,然后再選擇需要的參數(shù)進(jìn)行修改,并用SetCommState設(shè)置回去。
示例如下:
'設(shè)置波特率為115200
GetCommState(inoutfileHandler, pdcb)
pdcb.BaudRate.Parse("115200")
SetCommState(inoutfileHandler, pdcb)
第一個參數(shù)是已經(jīng)打開的串口的句柄,第二個參數(shù)是dcb(設(shè)備控制設(shè)置變量)

3.從串口讀數(shù)據(jù)
使用ReadFile
ReadFile(文件句柄,用來存放讀取到的數(shù)據(jù)的變量,需要讀取的字節(jié)數(shù),用來存放實際讀取到的字節(jié)數(shù)的變量,存放重疊參數(shù)的變量指針)
注意最后一個對文件的重疊操作是WinCE不支持的,設(shè)為0。
從串口讀取數(shù)據(jù)到一個數(shù)組中,使用如下語句就可以
ReadFile (inoutfileHandler, inbuff, inbuff.Length, numReadWrite, 0)
這樣的語句執(zhí)行之后,inbuff中存儲的就是讀取到的數(shù)據(jù),numReadWrite中就是剛才讀取的字節(jié)數(shù)

4.向串口寫數(shù)據(jù)
和讀類似,使用WriteFile函數(shù)
WriteFile (inoutfileHandler, stringToByteArray(value), value.Length(), numReadWrite, 0)

5.關(guān)閉串口
CloseHandle(inoutfileHandler)

上面的描述可能和正規(guī)的技術(shù)描述有一定偏差,所以遇到不清楚的地方,還是查MSDN吧!我已經(jīng)盡力描述清楚了。

原文: http://bbs.oorroo.com/viewthread.php?tid=154187&extra=&page=2

關(guān)鍵詞:VS.NET
主站蜘蛛池模板: 青海省| 民丰县| 迭部县| 太仓市| 通河县| 大兴区| 崇阳县| 曲阳县| 奉节县| 泽普县| 西青区| 罗定市| 绥中县| 应用必备| 微山县| 卢湾区| 莱阳市| 双柏县| 蛟河市| 肃宁县| 右玉县| 卢氏县| 永春县| 阳谷县| 达日县| 开远市| 祥云县| 德安县| 高邑县| 博野县| 淳化县| 桃源县| 白水县| 米林县| 方山县| 龙南县| 宜君县| 高雄市| 福安市| 江源县| 衡阳县|