|
樓主 |
發表於 2018-6-11 11:58:30
|
顯示全部樓層
這一版本是可以讀取送出的資料,還有讀回儀器反映的資料 回應的都是"原始資料"
初學者可以利用它來 驗證 與電源供應的 通訊狀況,進而作為擴展的參考!!
- Imports System.IO.Ports
- Imports System.Text
- 'OPEN "COM1:9600,n,8,2,rs,cd,lf,pe"---忽視request to send , carrier detect ; send line feed;enable perity check
- '"SYST:REM"------不能有" 符號,否則會異常
- '"*RST;*CLS" ---reset and clear the power supply------不能有" 符號,否則會異常
- '"*IND?"------不能有" 符號,否則會異常
- '"APPL P6V, 3.0, 3.0"------不能有" 符號,否則會異常
- '"OUTP ON"------不能有" 符號,否則會異常
- '這一版本是可以讀取送出的資料,還有讀回儀器反映的資料 回應的都是"原始資料"
- Public Class Form1
- Dim IOflag As Boolean
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- For Each sp As String In SerialPort.GetPortNames
- cmbCOM.Items.Add(sp)
- Next
- cmbCOM.Sorted = True
- cmbCOM.SelectedIndex = 0
- IOflag = False
- IOenable.BackColor = Color.Blue
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
- RS232.PortName = cmbCOM.SelectedItem.ToString
- RS232.BaudRate = 9600
- RS232.Parity = Parity.None
- RS232.DataBits = 8
- RS232.StopBits = StopBits.Two
- RS232.Encoding = Encoding.ASCII
- If Not RS232.IsOpen Then
- RS232.Open()
- btnSend.Enabled = True
- RS232.DtrEnable = True
- RS232.Write("SYST:REM" & vbCrLf)
- Else
- MsgBox("~~通訊埠開啟錯誤(通信埠已開啟)~~", MsgBoxStyle.Critical Or MsgBoxStyle.OkCancel)
- End
- End If
- End Sub
- Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
- Dim Buf As String
- Buf = "APPL " & cmbVOLT.SelectedItem.ToString & "," '電壓範圍
- Buf = Buf & " " & voltInput.Text & "," '設定電壓值
- Buf = Buf & " " & currInput.Text '設定電流值
- RS232.DtrEnable = True
- RS232.Write(Buf & vbCrLf)
- TimeDelay(200)
- txtReceive.Text &= Buf & vbCrLf
- txtReceive.SelectionStart = txtReceive.Text.Length
- txtReceive.ScrollToCaret()
- txtReceive.Focus()
- End Sub
- Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
- If Not RS232 Is Nothing Then
- If RS232.IsOpen Then RS232.Close()
- End If
- End
- End Sub
- '*****************
- 'timeDelay
- '*****************
- Private Sub TimeDelay(ByVal DT As Integer)
- Dim startTick As Integer
- startTick = Environment.TickCount()
- Do
- If Environment.TickCount() - startTick >= DT Then Exit Do
- Application.DoEvents()
- Loop
- End Sub
- Private Sub IOenable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IOenable.Click
- Dim BBuf As String
- IOflag = Not IOflag
- If IOflag Then
- IOenable.BackColor = Color.Red
- 'RS232.DtrEnable = True
- RS232.Write("OUTP ON" & vbCrLf)
- txtReceive.Text &= Chr(34) & "OUTPut ON" & Chr(34) & vbCrLf
- Else
- IOenable.BackColor = Color.Green
- 'RS232.DtrEnable = True
- RS232.Write("OUTP OFF" & vbCrLf)
- txtReceive.Text &= Chr(34) & "OUTPut OFF" & Chr(34) & vbCrLf
- End If
- TimeDelay(200)
- BBuf = RS232.ReadExisting()
- txtReceive.Text &= BBuf & vbCrLf
- txtReceive.SelectionStart = txtReceive.Text.Length
- txtReceive.ScrollToCaret()
- txtReceive.Focus()
- End Sub
-
- Private Sub btmCMD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmCMD.Click
- Dim cmdd As String
- Dim DataStr As String
- cmdd = TextInput.Text
- 'RS232.DtrEnable = True
- RS232.Write(cmdd & vbCrLf)
- txtReceive.Text &= Chr(34) & cmdd & Chr(34) & vbCrLf
- DataStr = RS232.ReadExisting()
- txtReceive.Text &= DataStr & vbCrLf
- txtReceive.SelectionStart = txtReceive.Text.Length '此三行讓文字往上滾動,網路上學到的指令
- txtReceive.ScrollToCaret()
- txtReceive.Focus()
- End Sub
- Private Sub btnRstatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRstatus.Click
- Dim StaCmd As String
- Dim DisStatus As String
- If RadioBtnI.Checked Then
- StaCmd = StatusInput.Text
- RS232.Write(StaCmd & vbCrLf)
- End If
- If RadioBtnS.Checked Then
- StaCmd = StatusDisplay1.SelectedItem.ToString
- RS232.Write(StaCmd & vbCrLf)
- End If
- TimeDelay(200)
- DisStatus = RS232.ReadExisting()
- StatusDisplay.Text &= DisStatus & vbCrLf
- StatusDisplay.SelectionStart = StatusDisplay.Text.Length '此三行讓文字往上滾動,網路上學到的指令
- StatusDisplay.ScrollToCaret()
- StatusDisplay.Focus()
- End Sub
-
- End Class
複製代碼 |
|