Public Class Form1
Inherits System.Windows.Forms.Form'フォームがコンポーネントの一覧をクリーンアップするために dispose をオーバーライドします。
<System.Diagnostics.DebuggerNonUserCode()> _Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Thencomponents.Dispose()
End IfFinally
MyBase.Dispose(disposing)
End Try
End Sub'Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer
Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()''ListBox1'Me.ListBox1.Dock = System.Windows.Forms.DockStyle.FillMe.ListBox1.FormattingEnabled = True
Me.ListBox1.ItemHeight = 12
Me.ListBox1.Location = New System.Drawing.Point(0, 0)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(292, 256)
Me.ListBox1.TabIndex = 0''Form1'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Private WithEvents Com1 As System.IO.Ports.SerialPort
Private byteBuffers() As Byte
Private MeasurementValue As String = ""Private recieveState As Boolean = True
Public Sub New()' この呼び出しは、Windows フォーム デザイナで必要です。
InitializeComponent()' InitializeComponent() 呼び出しの後で初期化を追加します。
InitializeIT007R("COM1")
End Sub' 接続するシリアルポートを指定

' シリアルポートの初期設定
Private Sub InitializeIT007R(ByVal tgtPortname As String)
If Com1 Is Nothing Then
' ここで tgtPortname が有効なシリアルポート名であることを検証すること' このサンプルでは省略
Com1 = New System.IO.Ports.SerialPort(tgtPortname)
Com1.BaudRate = 2400
' ボーレートは 2400bps
Com1.DataBits = 8' データビットは 8 ビット
Com1.StopBits = 1' ストップビットは 1 ビット
Com1.Parity = System.IO.Ports.Parity.None' パリティ無し
Com1.DiscardNull = False
Com1.Encoding = System.Text.Encoding.ASCII
byteBuffers = Nothing
End IfEnd Sub' フォームのロードイベント
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Com1 Is Nothing Then Return
If Com1.IsOpen Then Return
Try
Com1.Open()
Catch ex As Exception
Console.Write(ex.Message)
ReturnEnd TryCom1.DtrEnable = True
Com1.RtsEnable = True
Com1.DiscardInBuffer()
Com1.DiscardOutBuffer()
byteBuffers = NothingrecieveState = True
End Sub' フォームのクローズイベント
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
Handles MyBase.FormClosed
If Com1 IsNot Nothing Then
Com1.DtrEnable = False
Com1.RtsEnable = False
If Com1.IsOpen Then Com1.Close()
Com1.Dispose()
End IfEnd Sub' シリアルポート受信イベント
Private Sub OnCom1DataRecieved(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
Com1.DataReceived
Dim workByte As Byte
Do While (Com1.BytesToRead > 0)
workByte = Com1.ReadByte
If workByte = 13 Then
If byteBuffers.Length > 0 Then
Dim workEncoding As System.Text.Encoding = System.Text.Encoding.Default
MeasurementValue = workEncoding.GetString(byteBuffers, 0, byteBuffers.Length)
byteBuffers = Nothing
Me.Invoke(New MethodInvoker(AddressOf ListBox1ItemAdd))
End IfrecieveState = True
ElseIf workByte <> 10 Then
If recieveState Then
ReDim byteBuffers(0)byteBuffers(0) = workByte
recieveState = False
Else
ReDim Preserve byteBuffers(byteBuffers.Length)
byteBuffers(byteBuffers.Length - 1) = workByte
End IfEnd IfLoop
End Sub
Private Sub ListBox1ItemAdd()
Me.ListBox1.Items.Add(MeasurementValue)
Me.ListBox1.SelectedIndex = Me.ListBox1.Items.Count - 1
End Sub
End Class