Public Class Form1 Inherits System.Windows.Forms.Form Dim hComm As Integer Dim stDCB As DCB Dim timeOut As COMMTIMEOUTS Dim wDATA As String Dim rDATA(100) As Byte Dim dLen, wLen, rLen As Int32 Dim bRet As Boolean Dim CommName As String Dim Frequency As Integer #Region " Windows フォーム デザイナで生成されたコード " Public Sub New() MyBase.New() ' この呼び出しは Windows フォーム デザイナで必要です。 InitializeComponent() ' InitializeComponent() 呼び出しの後に初期化を追加します。 End Sub ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ' Windows フォーム デザイナで必要です。 Private components As System.ComponentModel.IContainer ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 'Windows フォーム デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton Friend WithEvents RadioButton5 As System.Windows.Forms.RadioButton Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button Friend WithEvents Button6 As System.Windows.Forms.Button Friend WithEvents Button7 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button10 As System.Windows.Forms.Button Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Button8 As System.Windows.Forms.Button Private Sub InitializeComponent() Me.RadioButton1 = New System.Windows.Forms.RadioButton Me.RadioButton2 = New System.Windows.Forms.RadioButton Me.RadioButton3 = New System.Windows.Forms.RadioButton Me.RadioButton4 = New System.Windows.Forms.RadioButton Me.RadioButton5 = New System.Windows.Forms.RadioButton Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.Label2 = New System.Windows.Forms.Label Me.TextBox2 = New System.Windows.Forms.TextBox Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.Button5 = New System.Windows.Forms.Button Me.Button6 = New System.Windows.Forms.Button Me.Button7 = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.Button10 = New System.Windows.Forms.Button Me.Button8 = New System.Windows.Forms.Button Me.GroupBox1.SuspendLayout() Me.SuspendLayout() ' 'RadioButton1 ' Me.RadioButton1.Location = New System.Drawing.Point(16, 16) Me.RadioButton1.Name = "RadioButton1" Me.RadioButton1.Size = New System.Drawing.Size(56, 24) Me.RadioButton1.TabIndex = 0 Me.RadioButton1.Text = "COM1" ' 'RadioButton2 ' Me.RadioButton2.Location = New System.Drawing.Point(80, 16) Me.RadioButton2.Name = "RadioButton2" Me.RadioButton2.Size = New System.Drawing.Size(56, 24) Me.RadioButton2.TabIndex = 1 Me.RadioButton2.Text = "COM2" ' 'RadioButton3 ' Me.RadioButton3.Location = New System.Drawing.Point(144, 16) Me.RadioButton3.Name = "RadioButton3" Me.RadioButton3.Size = New System.Drawing.Size(56, 24) Me.RadioButton3.TabIndex = 2 Me.RadioButton3.Text = "COM3" ' 'RadioButton4 ' Me.RadioButton4.Location = New System.Drawing.Point(216, 16) Me.RadioButton4.Name = "RadioButton4" Me.RadioButton4.Size = New System.Drawing.Size(56, 24) Me.RadioButton4.TabIndex = 3 Me.RadioButton4.Text = "COM4" ' 'RadioButton5 ' Me.RadioButton5.Checked = True Me.RadioButton5.Location = New System.Drawing.Point(288, 16) Me.RadioButton5.Name = "RadioButton5" Me.RadioButton5.Size = New System.Drawing.Size(56, 24) Me.RadioButton5.TabIndex = 4 Me.RadioButton5.TabStop = True Me.RadioButton5.Text = "COM5" ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.Label2) Me.GroupBox1.Controls.Add(Me.TextBox2) Me.GroupBox1.Controls.Add(Me.RadioButton1) Me.GroupBox1.Controls.Add(Me.RadioButton2) Me.GroupBox1.Controls.Add(Me.RadioButton3) Me.GroupBox1.Controls.Add(Me.RadioButton4) Me.GroupBox1.Controls.Add(Me.RadioButton5) Me.GroupBox1.Location = New System.Drawing.Point(16, 8) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(360, 80) Me.GroupBox1.TabIndex = 5 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "COM Port" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Label2.Location = New System.Drawing.Point(64, 48) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(96, 24) Me.Label2.TabIndex = 6 Me.Label2.Text = "Connection" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'TextBox2 ' Me.TextBox2.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.TextBox2.Location = New System.Drawing.Point(160, 48) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(48, 23) Me.TextBox2.TabIndex = 5 Me.TextBox2.Text = "False" ' 'TextBox1 ' Me.TextBox1.Font = New System.Drawing.Font("MS ゴシック", 36.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.TextBox1.Location = New System.Drawing.Point(48, 128) Me.TextBox1.MaxLength = 7 Me.TextBox1.Name = "TextBox1" Me.TextBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.TextBox1.Size = New System.Drawing.Size(152, 55) Me.TextBox1.TabIndex = 6 Me.TextBox1.Text = "123456" ' 'Button1 ' Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(64, Byte), CType(0, Byte)) Me.Button1.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button1.Location = New System.Drawing.Point(264, 136) Me.Button1.Name = "Button1" Me.Button1.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.Button1.Size = New System.Drawing.Size(112, 32) Me.Button1.TabIndex = 7 Me.Button1.Text = "正弦波出力" ' 'Button2 ' Me.Button2.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(64, Byte), CType(0, Byte)) Me.Button2.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button2.Location = New System.Drawing.Point(176, 104) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(24, 24) Me.Button2.TabIndex = 8 Me.Button2.Text = "▲" ' 'Button3 ' Me.Button3.BackColor = System.Drawing.Color.Blue Me.Button3.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button3.Location = New System.Drawing.Point(176, 184) Me.Button3.Name = "Button3" Me.Button3.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.Button3.Size = New System.Drawing.Size(24, 24) Me.Button3.TabIndex = 9 Me.Button3.Text = "▼" Me.Button3.TextAlign = System.Drawing.ContentAlignment.TopCenter ' 'Button4 ' Me.Button4.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(64, Byte), CType(0, Byte)) Me.Button4.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button4.Location = New System.Drawing.Point(128, 104) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(24, 24) Me.Button4.TabIndex = 10 Me.Button4.Text = "▲" ' 'Button5 ' Me.Button5.BackColor = System.Drawing.Color.Blue Me.Button5.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button5.Location = New System.Drawing.Point(128, 184) Me.Button5.Name = "Button5" Me.Button5.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.Button5.Size = New System.Drawing.Size(24, 24) Me.Button5.TabIndex = 11 Me.Button5.Text = "▼" Me.Button5.TextAlign = System.Drawing.ContentAlignment.TopCenter ' 'Button6 ' Me.Button6.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(64, Byte), CType(0, Byte)) Me.Button6.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button6.Location = New System.Drawing.Point(80, 104) Me.Button6.Name = "Button6" Me.Button6.Size = New System.Drawing.Size(24, 24) Me.Button6.TabIndex = 12 Me.Button6.Text = "▲" ' 'Button7 ' Me.Button7.BackColor = System.Drawing.Color.Blue Me.Button7.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button7.Location = New System.Drawing.Point(80, 184) Me.Button7.Name = "Button7" Me.Button7.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.Button7.Size = New System.Drawing.Size(24, 24) Me.Button7.TabIndex = 13 Me.Button7.Text = "▼" Me.Button7.TextAlign = System.Drawing.ContentAlignment.TopCenter ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("MS UI Gothic", 20.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Label1.Location = New System.Drawing.Point(200, 144) Me.Label1.Name = "Label1" Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.Yes Me.Label1.Size = New System.Drawing.Size(40, 32) Me.Label1.TabIndex = 16 Me.Label1.Text = "Hz" Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight ' 'Button10 ' Me.Button10.BackColor = System.Drawing.Color.Olive Me.Button10.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button10.Location = New System.Drawing.Point(264, 176) Me.Button10.Name = "Button10" Me.Button10.Size = New System.Drawing.Size(112, 32) Me.Button10.TabIndex = 17 Me.Button10.Text = "三角波出力" ' 'Button8 ' Me.Button8.BackColor = System.Drawing.Color.Navy Me.Button8.Font = New System.Drawing.Font("MS UI Gothic", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.Button8.Location = New System.Drawing.Point(264, 96) Me.Button8.Name = "Button8" Me.Button8.Size = New System.Drawing.Size(112, 32) Me.Button8.TabIndex = 18 Me.Button8.Text = "現在値入力" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12) Me.BackColor = System.Drawing.Color.Teal Me.ClientSize = New System.Drawing.Size(392, 222) Me.Controls.Add(Me.Button8) Me.Controls.Add(Me.Button10) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button7) Me.Controls.Add(Me.Button6) Me.Controls.Add(Me.Button5) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.GroupBox1) Me.ForeColor = System.Drawing.Color.White Me.Name = "Form1" Me.Text = "Signal Generator" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Text = "" End Sub Private Sub TimeOutSet() timeOut.ReadIntervalTimeout = 20 timeOut.ReadTotalTimeoutConstant = 50 timeOut.ReadTotalTimeoutMultiplier = 10 timeOut.WriteTotalTimeoutMultiplier = 10 timeOut.WriteTotalTimeoutConstant = 10 bRet = SetCommTimeouts(hComm, timeOut) End Sub Private Sub CheckConnection(ByVal CommName As String) Dim N As Integer bRet = CloseHandle(hComm) hComm = CreateFile(CommName, &HC0000000, 0, IntPtr.Zero, &H3, &H80, IntPtr.Zero) If hComm <> -1 Then stDCB.BaudRate = 19200 stDCB.fBitFields = &H1 stDCB.ByteSize = 8 stDCB.Parity = 0 stDCB.StopBits = 0 bRet = SetCommState(hComm, stDCB) TextBox2.Text = bRet TextBox1.Text = "" If bRet = True Then Application.DoEvents() wDATA = Chr(&H30) dLen = Len(wDATA) bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) bRet = ReadFile(hComm, rDATA, 6, rLen, IntPtr.Zero) If bRet = True Then For N = 0 To rLen Step 1 TextBox1.Text = TextBox1.Text & Chr(rDATA(N)) Next Frequency = Val(TextBox1.Text & Chr(0)) TimeOutSet() End If End If Else TextBox2.Text = "Fault" TextBox1.Text = "xxxxxx" End If End Sub Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged CommName = "COM1" CheckConnection(CommName) End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged CommName = "COM2" CheckConnection(CommName) End Sub Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged CommName = "COM3" CheckConnection(CommName) End Sub Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged CommName = "COM4" CheckConnection(CommName) End Sub Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged CommName = "COM5" CheckConnection(CommName) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Frequency = Frequency + 1 TextBox1.Text = Format(Frequency, "000000") End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Frequency = Frequency - 1 TextBox1.Text = Format(Frequency, "000000") End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Frequency = Frequency + 100 TextBox1.Text = Format(Frequency, "000000") End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Frequency = Frequency - 100 TextBox1.Text = Format(Frequency, "000000") End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Frequency = Frequency + 10000 TextBox1.Text = Format(Frequency, "000000") End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Frequency = Frequency - 10000 TextBox1.Text = Format(Frequency, "000000") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click wDATA = Chr(&H41) & Format(Frequency, "000000") & Chr(0) dLen = Len(wDATA) bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click wDATA = Chr(&H42) & Format(Frequency, "000000") & Chr(0) dLen = Len(wDATA) bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Dim N As Integer wDATA = Chr(&H30) dLen = Len(wDATA) bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) bRet = ReadFile(hComm, rDATA, 6, rLen, IntPtr.Zero) If bRet = True Then TextBox1.Text = "" For N = 0 To rLen Step 1 TextBox1.Text = TextBox1.Text & Chr(rDATA(N)) Next End If Frequency = Val(TextBox1.Text & Chr(0)) End Sub End Class