How to send commands to SCPI Terminal for a power supply
Hi I'm trying to write a program to send commands to SCPI Terminal for a HMP2030 power supply with a USB Interface. So far my approach has been writing a code using serial ports. I have written a code that is able to open the comport, but when I write to it, and then read from it, I get an empty string. I'm wondering if using serial port is the correct approach to this?
Code:
Private comm As New CommManager()
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim read As String
System.IO.Ports.SerialPort.GetPortNames()
comm.StopBits = 1
comm.DataBits = 8
comm.BaudRate = 9600
comm.Parity = "None"
comm.PortName = "COM18"
comm.OpenPort()
comm.Message = "*IDN?"
comm.Type = CommManager.MessageType.Normal
comm.WriteData("*IDN?")
read = comm.ReceiveSerialData()
End Sub
Code:
#Region "WriteData"
Public Sub WriteData(ByVal msg As String)
Select Case CurrentTransmissionType
Case TransmissionType.Text
'first make sure the port is open
'if its not open then open it
If Not (comPort.IsOpen = True) Then
comPort.Open()
End If
'send the message to the port
comPort.Write(msg)
'display the message
_type = MessageType.Outgoing
_msg = msg + "" + Environment.NewLine + ""
'MsgBox(_type, _msg)
Exit Select
Case TransmissionType.Hex
Try
'convert the message to byte array
Dim newMsg As Byte() = HexToByte(msg)
If Not write Then
MsgBox(_type, _msg)
Exit Sub
End If
'send the message to the port
comPort.Write(newMsg, 0, newMsg.Length)
'convert back to hex and display
_type = MessageType.Outgoing
_msg = ByteToHex(newMsg) + "" + Environment.NewLine + ""
MsgBox(_type, _msg)
Catch ex As FormatException
'display error message
_type = MessageType.Error
_msg = ex.Message + "" + Environment.NewLine + ""
MsgBox(_type, _msg)
Finally
' _displayWindow.SelectAll()
End Try
Exit Select
Case Else
'first make sure the port is open
'if its not open then open it
If Not (comPort.IsOpen = True) Then
comPort.Open()
End If
'send the message to the port
comPort.Write(msg)
'display the message
_type = MessageType.Outgoing
_msg = msg + "" + Environment.NewLine + ""
MsgBox(MessageType.Outgoing, msg + "" + Environment.NewLine + "")
Exit Select
End Select
End Sub
#End Region
Code:
#Region "Read Data"
Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""
Try
comPort.ReadTimeout = 10000
Do
Dim Incoming As String = comPort.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
Catch ex As TimeoutException
returnStr = "Error: Serial Port read timed out."
Finally
If comPort IsNot Nothing Then comPort.Close()
End Try
Return returnStr
End Function
#End Region
*REPHRASE: How to send SCPI commands to serial ports?