none
PocketPC CF介面程式 RRS feed

  • 問題

  • 帶人詢問~~

    求助各位高手
    現在我有一個開發需求
    要在Pocket上面開發一個程式
    去連線CF CARD介面的讀卡機
    在連線方面實在找不到範例或相關的教學(以前沒有學過..在.NET Compact Framewok去與硬體連線溝通)
    請各位大大指點一下迷津或是介紹一下相關的[中文書籍](~"~小弟英文不好)
    2006年4月7日 上午 06:24

解答

所有回覆

  • 我不知道你要做什麼樣的應用,但我有看到一些英文網站的介紹關於Storage Card, 像 CF card. 因為有很多應用程式,是需要和Sql Server CE database 或是其他程式一起部署到你的Pocket PC,所以為了方便起見,有個Autorun的機制由此而生.也就是,當你插入一張 storage card時, Pocket PC 會自動去尋找一個 Autorun.exe 的執行檔,而這個執行檔會根據你的Pocket PC CUP 的不同,放在不同的檔案夾裡,例如, 如果你的CUP 是ARM,那這個檔案可能就會放在 這張卡裡的\Storage card\ARM\Autorun.exe.一旦找到這個檔,它就會被Copy到 \Windows 檔案夾裡,然後被自動執行.這是storage card應用的 大致原理.

    2006年4月7日 上午 10:05
  •  一般也是會在PPC上面模擬一個SerialPort出來,對那個Port操作就行了,但也要看廠商提供甚麼樣的東西;

    如果有協定就可以自己操作,dll的話又是一另一種方式,SerialPort的部份可以參考MSND上的資訊

    一些api,CreateFile,SetCommState,GetCommState,WriteFile,ReadFile,SetupComm

    ,PurgeComm,EscapeCommFunction,SetCommMask都會用到,記得"文魁"有出一本.Net寫SerialPort的書,http://www.kingsinfo.com.tw/item_detail.asp?pro_id=5622

    可以去參考,而在compact framework的話呼叫的dll不一樣,大多都在"coredll.dll";如果不想自己操作API,可以參考看看OpenNetCF

    http://www.opennetcf.org/CategoryView.aspx?category=Home

    另外如果用VS2005開發在compact framework 2.0上面跑的話,內建有serialport的控制項,比較方便一些;

    http://msdn2.microsoft.com/zh-tw/library/system.io.ports.serialport(VS.80).aspx

    2006年4月7日 下午 02:38
  • 感謝兩位大大的熱心解答,屆時我將會選一位理想的答案..謝謝
    2006年4月7日 下午 09:31
  • @.@a

    這一篇不是我PO在小舖上面的嗎...好眼熟..哈哈哈

    前面的大大提到的那本書我在日前有購買[ Visual Studio.NET自動化系統監控 ]這本...相當不錯但是

    再運用上有一點問題想要請教一下

    在書中第三章的部份有製作一個小小的通訊程式程式範例名稱為TxRx(如附件)

    此範例我在WindowForm介面製作皆無問題

    但是我想將此範本套用置PDA上使用

    在參考網路上的文章絕大多數都是說將原本引用的API  kernel32.dll

    要換成 “coredll.dll”但是皆未有提到更詳細的內容

    像在原本書中的範例中所以提到的一些API宣告式在PDA所使用的.NET Compact Framework中皆不支援

    像在mRS232.vb Module  中的Public Declare Aout ..Auto關鍵字 與MarshalAs陳述式 及在Structures< StructLayout(LayoutKind.Sequential, Pack:=1)>Pack屬性也是不支援

    本想說將不支援的部分刪除看看能否執行..結果想當然爾..是失敗的,無法建立連線

    殊不知該如何處理,望能指點一下迷津

      

    2006年5月3日 上午 06:50
  • '找不到地方夾檔案所以只好PO整個模組上來

    下面這段程式就是

    範例程式中所用的kernel32.dlAPI模組但是是使用在.NET Framework上

    如果要單單只有改引用的API name "coredll.dl"

    然後使用在.NET Compact Framework上就會出現我前面所提的問題

    '=============================

    Imports System.Runtime.InteropServices
    Imports System.Text


    Module mRS232
        Public mhRS As Integer = -1            '串列埠的handle         
        Public miPort As Integer = 1           '預設通訊埠為COM1 
        Public miBaudRate As Integer = 9600    '9600bps
        Public meParity As DataParity = DataParity.Parity_None      'None parity Check
        Public meStopBit As DataStopBit = 1    '停止位元為1
        Public miDataBit As Integer = 8        '資料長度是8位元
        Public miBufferSize As Integer = 512   '預設緩衝區512 bytes

    #Region "Enums"
        '極性位元設定
        Public Enum DataParity
            Parity_None = 0
            Pariti_Odd
            Parity_Even
            Parity_Mark
        End Enum
        '停止位元設定
        Public Enum DataStopBit
            StopBit_1 = 0
            StopBit_2 = 2
        End Enum
        '緩衝區清除設定
        Public Enum PurgeBuffers
            RXAbort = &H2
            RXClear = &H8
            TxAbort = &H1
            TxClear = &H4
        End Enum
        '串列通訊硬體線路設定(EscapeFunction)
       Public Enum Lines
            SetXoff = 1  '當接收到Xoff字元時啟動傳輸動作。
            SetXon = 2   '當接收到Xon字元時啟動傳輸動作。
            SetRts = 3  '將RTS線路升成高電位。
            ClearRts = 4  '將RTS線路降成低電位。
            SetDtr = 5  '將DTR線路升成高電位。
            ClearDtr = 6 '將DTR線路降成低電位。
            ResetDev = 7 '重置設備
            SetBreak = 8  '設定通訊狀態為中斷(送出Break訊號)。
            ClearBreak = 9 '清除Break訊號,使傳輸動作繼續。
        End Enum

        '數據機專用狀態位元設定
        <Flags()> Public Enum ModemStatusBits
            ClearToSendOn = &H10
            DataSetReadyOn = &H20
            RingIndicatorOn = &H40
            CarrierDetect = &H80
        End Enum
        '事件遮罩位元設定
        <Flags()> Public Enum EventMasks
            RxChar = &H1   '輸入緩衝區已收到一個字元。
            RXFlag = &H2   '使用SetCommState函式設定的DCB結構中的等待字元已被傳入輸入緩衝區。
            TxBufferEmpty = &H4  '在輸出緩衝區中的資料已被完全送出。
            ClearToSend = &H8  'CTS(Clear To Send)線路發生變化。
            DataSetReady = &H10  'DSR(Data Set Ready)線路發生變化。
            ReceiveLine = &H20  'CD(Carrier Detect)線路訊號發生變化。
            Break = &H40   '收到Break訊號
            StatusError = &H80  '線路狀態錯誤,包括了CE_FRAME、CE_OVERRUN、CE_RXPARITY三種錯誤。
            Ring = &H100  '偵測到響鈴訊號。
        End Enum
     'DCB設定常數
     <Flags()> Public Enum BitDef
        dcb_Binary = &H1
        dcb_ParityCheck = &H2
        dcb_OutxCtsFlow = &H4
        dcb_OutxDsrFlow = &H8
        dcb_DtrControlMask = &H30
        dcb_DtrControlDisable = &H0
        dcb_DtrControlEnable = &H10
        dcb_DtrControlHandshake = &H20
        dcb_DsrSensivity = &H40
        dcb_TXContinueOnXoff = &H80
        dcb_OutX = &H100
        dcb_InX = &H200
        dcb_ErrorChar = &H400
        dcb_NullStrip = &H800
        dcb_RtsControlMask = &H3000
        dcb_RtsControlDisable = &H0
        dcb_RtsControlEnable = &H1000
        dcb_RtsControlHandShake = &H2000
        dcb_RtsControlToggle = &H3000
        dcb_AbortOnError = &H4000
        dcb_Reserveds = &HFFFF8000
       End Enum
        '通訊埠錯誤常數
        <Flags()> Public Enum CommError
            CE_BREAK = &H10
            CE_DNS = &H800
            CE_FRAME = &H8
            CE_IOE = &H400
            CE_MODE = &H8000
            CE_OOP = &H1000
            CE_OVERRUN = &H2
            CE_PTO = &H200
            CE_RXOVER = &H1
        End Enum
        '通訊埠線路狀態偵測常數
        <Flags()> Public Enum CommErrorLine
            fCtsHold = &H1 '等待CTS訊號準備傳送
            fDsrHold = &H2 '等待DSR訊號準備傳送
            fRlsdHold = &H4 '等待RLSD訊號準備傳送 
            fXoffHold = &H8 '收到XOFF字元,停止傳送
            fXoffSent = &H10 '已送出XOFF字元,停止傳送
            fEof = &H20      ' EOF字元已送出
            fTxim = &H40     '字元等待傳送
        End Enum


    #End Region
    #Region "Structures"
        'Device Control Block結構宣告
        <StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure DCB
            Public DCBlength As Integer  '結構長度
            Public BaudRate As Integer
            Public Bits1 As Integer
            Public wReserved As Short
            Public XonLim As Short
            Public XoffLim As Short
            Public ByteSize As Byte
            Public Parity As Byte
            Public StopBits As Byte
            Public XonChar As Byte
            Public XoffChar As Byte
            Public ErrorChar As Byte
            Public EofChar As Byte
            Public EvtChar As Byte
            Public wReserved2 As Short
        End Structure
        <StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure COMSTAT
            Public StateFlag As Integer
            Public cbInQue As Integer
            Public cbOutQue As Integer
        End Structure

        <StructLayout(LayoutKind.Sequential, Pack:=1)> Public 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, Pack:=1)> Public Structure COMMCONFIG
            Public dwSize As Integer
            Public wVersion As Short
            Public wReserved As Short
            Public dcbx As DCB
            Public dwProviderSubType As Integer
            Public dwProviderOffset As Integer
            Public dwProviderSize As Integer
            Public wcProviderData As Byte
        End Structure
        <StructLayout(LayoutKind.Sequential, Pack:=1)> Public 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 "Constants"
        Public Const GENERIC_READ As Integer = &H80000000
        Public Const GENERIC_WRITE As Integer = &H40000000
        Public Const OPEN_EXISTING As Integer = 3
    #End Region
    #Region "Win32API"
        '// Win32 API
        Public Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
        Public Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
        Public Declare Auto Function BuildCommDCB Lib "kernel32.dll" (ByVal lpDef As String, ByRef lpDCB As DCB) As Integer
        Public Declare Auto Function SetupComm Lib "kernel32.dll" (ByVal hFile As Integer, ByVal dwInQueue As Integer, ByVal dwOutQueue As Integer) As Integer
        Public Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Integer
        Public Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Integer
        Public Declare Auto Function ClearCommError Lib "kernel32.dll" (ByVal hFile As Integer, ByVal lpErrors As Integer, ByRef lpCS As COMSTAT) As Integer
        Public Declare Auto Function PurgeComm Lib "kernel32.dll" (ByVal hFile As Integer, ByVal dwFlags As Integer) As Integer
        Public Declare Auto Function EscapeCommFunction Lib "kernel32.dll" (ByVal hFile As Integer, ByVal ifunc As Long) As Boolean
        Public Declare Auto Function WaitCommEvent Lib "kernel32.dll" (ByVal hFile As Integer, ByRef Mask As EventMasks, ByRef lpOverlap As OVERLAPPED) As Integer
        Public Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer
        Public Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer
        Public Declare Auto Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
        Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Integer) As Integer
        Public Declare Auto Function FormatMessage Lib "kernel32.dll" (ByVal dwFlags As Integer, ByVal lpSource As Integer, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal lpBuffer As String, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer
        Public Declare Auto Function GetCommModemStatus Lib "kernel32.dll" (ByVal hFile As Integer, ByRef lpModemStatus As Integer) As Boolean
        Public Declare Auto Function CreateEvent Lib "kernel32.dll" (ByVal lpEventAttributes As Integer, ByVal bManualReset As Integer, ByVal bInitialState As Integer, <MarshalAs(UnmanagedType.LPStr)> ByVal lpName As String) As Integer
        Public Declare Auto Function GetLastError Lib "kernel32.dll" () As Integer
        Public Declare Auto Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer
        Public Declare Auto Function GetOverlappedResult Lib "kernel32.dll" (ByVal hFile As Integer, ByRef lpOverlapped As OVERLAPPED, ByRef lpNumberOfBytesTransferred As Integer, ByVal bWait As Integer) As Integer
        Public Declare Auto Function SetCommMask Lib "kernel32.dll" (ByVal hFile As Integer, ByVal lpEvtMask As EventMasks) As Integer
    #End Region
        '******************************************
        '開啟通訊埠
        '******************************************
        Public Overloads Sub OpenCOM()
            Dim uDcb As DCB, iRc As Integer
            Dim iMode As Integer = 0
            Dim sDCBState As String
            If miPort > 0 Then
                '開啟通訊埠
                mhRS = CreateFile("COM" & miPort.ToString, _
                GENERIC_READ Or GENERIC_WRITE, 0, 0, _
                OPEN_EXISTING, iMode, 0)
                If mhRS <> -1 Then
                    '清除通訊錯誤
                    Dim lpErrCode As Integer
                    iRc = ClearCommError(mhRS, lpErrCode, Nothing)
                    '清除緩衝區
                    iRc = PurgeComm(mhRS, PurgeBuffers.RXClear Or PurgeBuffers.TxClear)
                    '設定輸出入緩衝區的大小
                    iRc = SetupComm(mhRS, miBufferSize, miBufferSize)
                    '取得通訊埠設定值,並填入DCB結構
                    iRc = GetCommState(mhRS, uDcb)
                    '以下將新的通訊埠設定值填入
                    uDcb.BaudRate = miBaudRate
                    uDcb.ByteSize = miDataBit
                    uDcb.Parity = meParity
                    uDcb.StopBits = meStopBit
                    iRc = SetCommState(mhRS, uDcb)  '將DCB設定進去
                    '檢查設定是否成功  
                    If iRc = 0 Then
                        Dim lCode As Integer
                        lCode = GetLastError()  '取得錯誤碼
                        MsgBox("DCB結構建立發生錯誤,錯誤代碼=" & lCode.ToString)
                        End
                    End If
                Else
                    '開啟錯誤
                    MsgBox("開啟通訊埠錯誤!", MsgBoxStyle.Critical + MsgBoxStyle.OKOnly)
                End If
            Else
                Throw New ApplicationException("系統中的串列埠未定義!")
            End If
        End Sub

        '******************************************
        '關閉通訊埠
        '******************************************
        Public Sub CloseCOM()
            If mhRS <> -1 Then
                CloseHandle(mhRS)
                mhRS = -1
            End If
        End Sub

        '******************************************
        '將位元組資料傳送出去
        '多載函式之一,需傳入位元組陣列
        '******************************************
        Public Overloads Sub Write(ByVal Buffer As Byte())
            Dim iBytesWritten, iRc As Integer
            '---------------------------------
            If mhRS = -1 Then
                Throw New ApplicationException("必須先開啟通訊埠,才能作輸出。")
            Else
                Try
                    iRc = WriteFile(mhRS, Buffer, Buffer.Length, iBytesWritten, Nothing) '輸出陣列
                    If iRc = 0 Then
                        Throw New ApplicationException("傳輸失敗 - 已傳輸 " & iBytesWritten.ToString & " Bytes/欲傳輸" & Buffer.Length.ToString & "Bytes")
                    End If
                Catch Ex As Exception
                    Throw
                End Try
            End If
        End Sub

        '******************************************
        '將位元組資料傳送出去
        '多載函式之一,需傳入字串
        '首先將字串分解成陣列,以便WriteFile函式可以使用
        '******************************************
        Public Overloads Sub Write(ByVal Buffer As String)
            Dim oEncoder As New System.Text.UTF8Encoding
            Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
            Write(aByte) '呼叫另一個傳輸的多載函式
        End Sub

        '******************************************
        '將位元組資料接收進來
        '多載函式之一,第一個參數是讀取的位元組數,第二個參數是傳回的位元組陣列
        '如果傳入的參數使用ByVal,原始的數據將不會被更改,故在此需用ByRef
        '在呼叫的地方才能得到此函式所傳回的結果
        '******************************************
        Public Overloads Sub Read(ByRef Bytes2Read As Integer, ByRef InputByte() As Byte)
            Dim iReadChars, iRc, lpErrors As Integer
            Dim mabtRxBuf As Byte()             '接收緩衝區
            '設定讀取資料的長度
            If mhRS = -1 Then
                Throw New ApplicationException("尚未開啟通訊埠,不能讀取資料!")
            Else
                '讀取位元組
                Try
                    Dim cs As COMSTAT
                    ClearCommError(mhRS, lpErrors, cs)  '取得緩衝區內的位元組數
                    iReadChars = cs.cbInQue
                    ReDim mabtRxBuf(iReadChars - 1)
                    iRc = ReadFile(mhRS, mabtRxBuf, cs.cbInQue, iReadChars, Nothing)
                    If iRc = 0 Then
                        '讀取錯誤
                        Throw New ApplicationException("讀取錯誤: " & iRc.ToString)
                    Else
                        Bytes2Read = iReadChars
                        InputByte = mabtRxBuf
                    End If
                Catch Ex As Exception
                    '其他的讀取錯誤狀態
                    Throw New ApplicationException("讀取錯誤: " & Ex.Message, Ex)
                End Try
            End If
        End Sub

        '******************************************
        '將字串資料接收進來
        '多載函式之一,第一個參數是讀取的位元組數,第二個參數是傳回的字串
        '如果傳入的參數使用ByVal,原始的數據將不會被更改,故在此需用ByRef
        '在呼叫的地方才能得到此函式所傳回的結果
        '******************************************
        Public Overloads Sub Read(ByRef Char2Read As Integer, ByRef InputString As String)
            Dim iReadChars, iRc, lpErrors As Integer
            Dim mabtRxBuf As Byte()   '接收緩衝區
            '設定讀取資料的長度
            If mhRS = -1 Then
                Throw New ApplicationException("尚未開啟通訊埠,不能讀取資料!")
            Else
                '讀取位元組
                Try
                    Dim cs As COMSTAT
                    ClearCommError(mhRS, lpErrors, cs)  '取得緩衝區內的位元組數
                    iReadChars = cs.cbInQue
                    ReDim mabtRxBuf(iReadChars - 1)
                    iRc = ReadFile(mhRS, mabtRxBuf, cs.cbInQue, iReadChars, Nothing)
                    If iRc = 0 Then
                        '讀取錯誤
                        Throw New ApplicationException("讀取錯誤: " & iRc.ToString)
                    Else
                        Char2Read = iReadChars
                        '轉換位元組陣列為字串
                        Dim oEncoder As New System.Text.UTF8Encoding
                        InputString = oEncoder.GetString(mabtRxBuf)
                    End If
                Catch Ex As Exception
                    '其他的讀取錯誤狀態
                    Throw New ApplicationException("讀取錯誤: " & Ex.Message, Ex)
                End Try
            End If

        End Sub


    End Module

    2006年5月3日 上午 06:58
  • Private Declare Function SetCommState Lib "coredll.dll" (ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
    Private Declare Function GetCommState Lib "coredll.dll" (ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
    Private Declare Function CreateFile Lib "coredll.dll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
    Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As Integer) As Integer
    Private Declare Function WriteFile Lib "coredll.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToWrite As Integer, ByRef lpNumberOfBytesWritten As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer
    Private Declare Function ReadFile Lib "coredll.dll" (ByVal hFile As Integer, ByVal Buffer As Byte(), ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer
    Private Declare Function SetupComm Lib "coredll.dll" (ByVal hFile As Integer, ByVal dwInQueue As Integer, ByVal dwOutQueue As Integer) As Integer
    Private Declare Function PurgeComm Lib "coredll.dll" (ByVal hFile As Integer, ByVal dwFlags As Integer) As Integer
    Private Declare Function EscapeCommFunction Lib "coredll.dll" (ByVal hFile As Integer, ByVal ifunc As Integer) As Boolean
    Private Declare Function SetCommMask Lib "coredll.dll" (ByVal hcomm As Integer, ByVal lpEvtMask As EventMasks) As Integer
    Private Declare Function WaitCommEvent Lib "coredll.dll " (ByVal hComm As Integer, ByRef Mask As EventMasks, ByRef lpoverlap As OVERLAPPED) As Integer
    Private Declare Function ClearCommError Lib "coredll.dll " (ByVal hComm As Integer, ByRef lpErrors As Integer, ByRef lpStat As COMSTAT) As Boolean

    #Region "Consts"
       
        '''<summary>
        '''Serial Port Consts
        '''</summary>
        Private Const GENERIC_READ As Integer = &H80000000
        Private Const GENERIC_WRITE As Integer = &H40000000
        Private Const OPEN_EXISTING As Integer = 3
        Private Const AF_INET = 2
        Private Const SOCK_STREAM = 1
        Private Const SOCK_DGRAM = 2
        Private Const IPPROTO_IP = 0
        Private Const IPPROTO_TCP = 6
        Private Const IPPROTO_UDP = 17
        Private Const INADDR_ANY = &H0
        Private Const SOL_SOCKET = &HFFFF&
        Private Const FD_READ = &H1
        Private Const FD_WRITE = &H2
        Private Const FD_ACCEPT = &H8
        Private Const FD_CONNECT = &H10
        Private Const FD_CLOSE = &H20
        Private Const INVALID_SOCKET = -1
        Private Const INVALID_PORT = -1
        Private Const INADDR_NONE = &HFFFFFFFF
        Private Const sockaddr_size = 16
        Private Const SOCKET_ERROR = -1
        Private Const IOCTL_HAL_REBOOT As Integer = &H101003C
        Private Const bytesReturned As Integer = 0
    #End Region

    #Region "Enums"
        Private Enum PurgeBuffers
            RXAbort = &H2
            RXClear = &H8
            TxAbort = &H1
            TxClear = &H4
        End Enum
        Private Enum Lines
            SetXoff = 1
            SetXon = 2
            SetRts = 3
            ClearRts = 4
            SetDtr = 5
            ClearDtr = 6
            ResetDev = 7
            SetBreak = 8
            ClearBreak = 9
        End Enum
        <Flags()> Private Enum EventMasks
            RxChar = &H1
            RxFlag = &H2
            TxBufferEmpty = &H4
            ClearToSend = &H8
            DataSetReady = &H10
            ReceiveLine = &H20
            Break = &H40
            StatusError = &H80
            Ring = &H100
        End Enum
        Private Enum RegValueType
            REG_NONE = 0
            REG_SZ = 1
            REG_EXPAND_SZ = 2
            REG_BINARY = 3
            REG_DWORD = 4
            REG_DWORD_BIG_ENDIAN = 5
            REG_MULTI_SZ = 7
        End Enum
        Private Enum CommErrorFlag
            CE_RXOVER = &H1              ''''' Receive Queue overflow
            CE_OVERRUN = &H2             ''''' Receive Overrun Error
            CE_RXPARITY = &H4            ''''' Receive Parity Error
            CE_FRAME = &H8               ''''' Receive Framing error
            CE_BREAK = &H10              ''''' Break Detected
            CE_TXFULL = &H100            ''''' TX Queue is full
            CE_PTO = &H200               ''''' LPTx Timeout
            CE_IOE = &H400               ''''' LPTx I/O Error
            CE_DNS = &H800               ''''' LPTx Device not selected
            CE_OOP = &H1000              ''''' LPTx Out-Of-Paper
            CE_MODE = &H8000             ''''' Requested mode unsupported

            IE_BADID = (-1)              ''''' Invalid or unsupported id
            IE_OPEN = (-2)               ''''' Device Already Open
            IE_NOPEN = (-3)              ''''' Device Not Open
            IE_MEMORY = (-4)             ''''' Unable to allocate queues
            IE_DEFAULT = (-5)            ''''' Error in default parameters
            IE_HARDWARE = (-10)          ''''' Hardware Not Present
            IE_BYTESIZE = (-11)          ''''' Illegal Byte Size
            IE_BAUDRATE = (-12)          ''''' Unsupported BaudRate
        End Enum
    #End Region

    #Region "Structures"
        Private Structure DCB
            Public DCBlength As Integer  
            Public BaudRate As Integer
            Public Bits1 As Integer
            Public wReserved As Short
            Public XonLim As Short
            Public XoffLim As Short
            Public ByteSize As Byte
            Public Parity As Byte
            Public StopBits As Byte
            Public XonChar As Byte
            Public XoffChar As Byte
            Public ErrorChar As Byte
            Public EofChar As Byte
            Public EvtChar As Byte
            Public wReserved2 As Short
        End Structure
        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
        Private Structure COMSTAT
            Public fCtsHold As Int32 
            Public fDsrHold As Int32 
            Public fRlsdHold As Int32 
            Public fXoffHold As Int32 
            Public fXoffSent As Int32 
            Public fEof As Int32 
            Public fTxim As Int32 
            Public fReserved As Integer 
            Public cbInQue As Integer 
            Public cbOutQue As Integer 
        End Structure
    #End Region

    ClearCommError????????,????????,???????~

    2006年5月3日 上午 08:23