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

2010-08-28 10:50:35来源:西部e网作者:

首先,从coredll.dll中导入下列函数,其中有文件读写的函数例如CreateFile、ReadFile、WriteFile以及CloseHandle,分别用来打开、读、写和关闭文件。因为在WinCE中,使用API操作串口的话,串口是当作一个文件来处理的。另外主要要用的就是GetCommState和SetCommState,

用来获取和设置串口的状态,例如波特率,数据位,停止位,奇偶校验等等。以下是导入这些函数的语句。

<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结构体,用来存储串口的状态。

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  数据位
        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

以上只标注了主要的参数,其他参数可以依据需要进行查看,包含一些控制和读取Modem状态的参数等等。

1.打开串口
CreateFile(文件名,读写方式,共享模式,安全属性,对文件是否已存在的处理,文件属性以及标记,模板文件)
该函数如果打开成功,将返回一个已经打开的文件的句柄,可以使用这个句柄对文件进行其他操作。
在WinCE中,第四个参数是忽略了的。其他的数值在处理文件的时候会依据需要有所不同,处理串口则使用一种参数就基本可以。
打开串口,用这样的语句就可以了
CreateFile ("COM1:", &HC0000000, 0, 0, 3, 0, 0)

2.设置串口参数
使用GetCommState和SetCommState函数
因为dcb这个结构体定义的变量的初始值和当前串口的参数是不同的,我们修改串口参数并不希望将不需要的参数也改动,甚至是改错.所以先用
GetCommState将当前串口状态读入这个变量,然后再选择需要的参数进行修改,并用SetCommState设置回去。
示例如下:
'设置波特率为115200
GetCommState(inoutfileHandler, pdcb)
pdcb.BaudRate.Parse("115200")
SetCommState(inoutfileHandler, pdcb)
第一个参数是已经打开的串口的句柄,第二个参数是dcb(设备控制设置变量)

3.从串口读数据
使用ReadFile
ReadFile(文件句柄,用来存放读取到的数据的变量,需要读取的字节数,用来存放实际读取到的字节数的变量,存放重叠参数的变量指针)
注意最后一个对文件的重叠操作是WinCE不支持的,设为0。
从串口读取数据到一个数组中,使用如下语句就可以
ReadFile (inoutfileHandler, inbuff, inbuff.Length, numReadWrite, 0)
这样的语句执行之后,inbuff中存储的就是读取到的数据,numReadWrite中就是刚才读取的字节数

4.向串口写数据
和读类似,使用WriteFile函数
WriteFile (inoutfileHandler, stringToByteArray(value), value.Length(), numReadWrite, 0)

5.关闭串口
CloseHandle(inoutfileHandler)

上面的描述可能和正规的技术描述有一定偏差,所以遇到不清楚的地方,还是查MSDN吧!我已经尽力描述清楚了。

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

关键词:VS.NET