2010年8月12日 星期四

LPT1 port (vb.net)

Public Class Win32API

Public Const GENERIC_WRITE = &H40000000
Public Const CREATE_ALWAYS = 2
Public Const OPEN_EXISTING = 3

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(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 Function CloseHandle Lib "kernel32" Alias "CloseHandle" _
(ByVal hObject As Long) As Long

End Class

Public Sub WriteToPort(ByRef sPort As String, ByRef sText As String)

Dim FileHandle As Long
Dim FileHandleIntPtr As IntPtr
Dim FStream As FileStream
Dim SWriter As StreamWriter

FileHandle = Win32API.CreateFile(sPort, Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)

FileHandleIntPtr = New IntPtr(FileHandle)

FStream = New FileStream(FileHandleIntPtr, FileAccess.Write, False)

FStream.Flush()

SWriter = New StreamWriter(FStream)

SWriter.WriteLine(sText)

FStream.Flush()

SWriter.Close()

FStream.Close()

Win32API.CloseHandle(FileHandle)

End Sub

沒有留言:

張貼留言