第一步,初始化MSComm,并引入Sleep函数

Private Declare Sub Sleep Lib "Kernel32" (ByVal NextRunProgramTimeAsMs As Long)


Private Sub Form_load()

With MSComm1
.CommPort = 3 '选择串口2
.Settings = "9600,n,8,1" '9.6kbit/s,无校验,8位数据位,1位停止位
.InputMode = comInputModeBinary '以二进制格式读取接收缓冲区
.RThreshold = 1 '接收字符数大于等于1就会产生接收中断
.InputLen = 0 '读出接收缓冲区中的所有内容
.OutBufferCount = 0 '清空发送缓冲区
.InBufferCount = 0 '清空接收缓冲区
.InputMode = comInputModeText

End With

End Sub

第二步,打开端口

Private Sub Command1_Click()

MSComm1.PortOpen = True

End Sub

第三步,向电源发送信息,注意设置间隔时间避免死机(每个命令都以 & Chr(10) 结束)

Private Sub Command2_Click()


 MSComm1.Output = "SOUR:CURR:LEV 0.10000" & Chr(10)
 Sleep (100)
 MSComm1.Output = "SOUR:CURR:LEV?" & Chr(10)
Sleep (500)
 MSComm1.Output = "SOUR:CURR:LEV 0.20000" & Chr(10)
 Sleep (100)
 MSComm1.Output = "SOUR:CURR:LEV?" & Chr(10)
Sleep (500)
 MSComm1.Output = "SOUR:CURR:LEV 0.30000" & Chr(10)
 Sleep (100)
 MSComm1.Output = "SOUR:CURR:LEV?" & Chr(10)
Sleep (500)
 MSComm1.Output = "SOUR:CURR:LEV 0.40000" & Chr(10)
 Sleep (100)
 MSComm1.Output = "SOUR:CURR:LEV?" & Chr(10)
Sleep (500)
 MSComm1.Output = "SOUR:CURR:LEV 0.50000" & Chr(10)
 Sleep (100)
 MSComm1.Output = "SOUR:CURR:LEV?" & Chr(10)
 

End Sub

第四步,等待触发OnComm事件,并将返回值写入文件(注意设置间隔时间,等待数据写入完全再读取)

Private Sub MSComm1_OnComm() 'OnComm事件


Select Case MSComm1.CommEvent
Case comEvReceive '如果接收到字符
 
Sleep (50)
Text1.Text() = MSComm1.Input
Debug.Print Text1.Text()
Open "c:\1.txt" For Append As #1

Print #1, Text1.Text()

Close #1

End Select


End Sub