'■VB.net RS232c通信を行うクラス
'
'使用方法
'--------------------------------------------------------------------------------
'Dim obj As New Rs232c()
'
''オープンします
'Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' If (obj.Connect("COM1", 9600, 8, Rs232c.NOPARITY, Rs232c.ONESTOPBIT)) Then MsgBox("接続成功")
'End Sub
'
''文字列を送信します
'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' If (obj.Send("今日は")) Then MsgBox("送信成功")
'End Sub
'
''タイマーを使い定期的に読み出します
'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Dim str As String
' obj.Read(str)
' Me.Text = Me.Text + str
'End Sub
'
''クローズします
'Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
' If (obj.Close()) Then MsgBox("クローズ成功")
'End Sub
'
'---------------------------------------------------------------------------------
Public Class Rs232c
Public Const NOPARITY As Int32 = 0 'パリティなし: NOPARITY
Public Const EVENPARITY As Int32 = 2 '偶数パリティ: EVENPARITY
Public Const ODDPARITY As Int32 = 1 '奇数パリティ: ODDPARITY
Public Const ONESTOPBIT As Int32 = 0 '1ビット: ONESTOPBIT
Public Const ONE5STOPBITS As Int32 = 1 '1.5ビット: ONE5STOPBITS
Public Const TWOSTOPBITS As Int32 = 2 '2ビット: TWOSTOPBITS
Private Structure DCB
Public DCBlength As Int32
Public BaudRate As Int32
Public fBitFields As Int32
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
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 wReserved1 As Int16 '予約されています。使用しないでください。
End Structure
Private Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Int32
Public ReadTotalTimeoutMultiplier As Int32
Public ReadTotalTimeoutConstant As Int32
Public WriteTotalTimeoutMultiplier As Int32
Public WriteTotalTimeoutConstant As Int32
End Structure
Private Const GENERIC_READ As Int32 = &H80000000
Private Const GENERIC_WRITE As Int32 = &H40000000
Private Const OPEN_EXISTING As Int32 = 3
Private Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
Private Declare Auto Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr
Private Declare Auto Function GetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean
Private Declare Auto Function SetCommState Lib "kernel32.dll" (ByVal nCid As IntPtr, _
ByRef lpDCB As DCB) As Boolean
Private Declare Auto Function GetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Private Declare Auto Function SetCommTimeouts Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByRef lpCommTimeouts As COMMTIMEOUTS) As Boolean
Private Declare Auto Function WriteFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToWrite As Int32, _
ByRef lpNumberOfBytesWritten As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Private Declare Auto Function ReadFile Lib "kernel32.dll" (ByVal hFile As IntPtr, _
ByVal lpBuffer As Byte(), ByVal nNumberOfBytesToRead As Int32, _
ByRef lpNumberOfBytesRead As Int32, ByVal lpOverlapped As IntPtr) As Boolean
Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
Private Declare Auto Function ClearCommError Lib "kernel32.dll" (ByVal hFile As Int32, ByVal lpErrors As Int32, ByVal l As Int32) As Int32
Private hSerialPort As IntPtr
Private oEncoder As New System.Text.ASCIIEncoding()
Private oEnc As System.Text.Encoding = oEncoder.Default()
'*****************************************************************
'ポートに接続
'*****************************************************************
Public Function Connect() As Boolean
Return Connect("COM1", 9600, 8, NOPARITY, ONESTOPBIT)
End Function
Public Function Connect(ByVal PortNum As String) As Boolean
Return Connect(PortNum, 9600, 8, NOPARITY, ONESTOPBIT)
End Function
Public Function Connect(ByVal PortNum As String, ByVal BaudRate As Integer, ByVal ByteSize As Byte, ByVal Parity As Integer, ByVal StopBit As Integer) As Boolean
Dim MyDCB As DCB
Dim MyCommTimeouts As COMMTIMEOUTS
'シリアル ポートへのハンドルを取得します。
hSerialPort = CreateFile(PortNum, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, _
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
'取得したハンドルが有効かどうか確認します。
If hSerialPort.ToInt32 = -1 Then Return False
'現在のコントロールの設定を取得します。
If Not (GetCommState(hSerialPort, MyDCB)) Then Return False
'必要に応じて MyDCB のプロパティを変更します。
'警告 : 各プロパティでサポートされている値に応じて変更を行うようにしてください。
MyDCB.BaudRate = BaudRate
MyDCB.ByteSize = ByteSize
MyDCB.Parity = Parity
MyDCB.StopBits = StopBit
'MyDCB のプロパティに基づいて COM? を再構成します。
If Not (SetCommState(hSerialPort, MyDCB)) Then Return False
'現在のタイムアウトの設定を取得します。
If Not (GetCommTimeouts(hSerialPort, MyCommTimeouts)) Then Return False
' MyCommTimeouts のプロパティを必要に応じて変更します。
' 警告 : プロパティでサポートされている値に応じて変更を行うようにしてください。
MyCommTimeouts.ReadIntervalTimeout = -1
MyCommTimeouts.ReadTotalTimeoutConstant = 0
MyCommTimeouts.ReadTotalTimeoutMultiplier = 0
MyCommTimeouts.WriteTotalTimeoutConstant = 0
MyCommTimeouts.WriteTotalTimeoutMultiplier = 0
' MyCommTimeouts のプロパティに基づいてタイムアウトの設定を再構成します。
If Not (SetCommTimeouts(hSerialPort, MyCommTimeouts)) Then Return False
Return True
End Function
'*****************************************************************
'送信
'*****************************************************************
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
Public Function Send(ByRef strSend As String) As Boolean
Dim Buffer() As Byte
Dim BytesWritten As Int32
Buffer = oEnc.GetBytes(strSend)
If Not (WriteFile(hSerialPort, Buffer, Buffer.Length, BytesWritten, IntPtr.Zero)) Then Return False
Return True
End Function
'*****************************************************************
'読み出し
'*****************************************************************
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
Public Function Read(ByRef str As String) As Boolean
Dim BytesWritten, BytesRead As Int32
BytesWritten = 1024
Dim Buffer(1024) As Byte
If (Read = Not ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, IntPtr.Zero)) Then
str = oEnc.GetString(Buffer)
Else
str = ""
End If
End Function
'*****************************************************************
'ポートを閉じる
'*****************************************************************
<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.Synchronized)> _
Public Function Close() As Boolean
Return CloseHandle(hSerialPort)
End Function
End Class