发表于:2007-02-14 10:42:00
5楼
请问我这个程序和LG的iG5变频器通讯为什么接受到的都是乱码呢?
Private Sub Command1_Click() '发送按钮
Dim dat As String
dat = Text1.Text & Text2.Text & Text3.Text & Text4.Text
MSComm1.Output = Chr$(5) + dat + SumChk(dat) + Chr(4)
End Sub
-----------------------------------------------------
Private Sub Command2_Click() '接收缓冲区清空
Text7.Text = ""
End Sub
-------------------------------------------------------
Private Function SumChk(Dats$) As String '和校验
Dim i& '定义i为long型
Dim CHK&
For i = 1 To Len(Dats)
CHK = CHK + Asc(Mid(Dats, i, 1))
Next i
SumChk = Right(Hex$(CHK), 2)
End Function
-------------------------------------------------------
Private Sub Form_Load() '窗体程序
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.CommPort = 1 '用Com1口
MSComm1.Settings = "9600,n,8,1" '通讯参数 波特率 奇偶校验 数据位 停止位
MSComm1.PortOpen = True '开串口
MSComm1.RThreshold = 0 '当接收缓冲区的数据达到RThreshold所设定的字节数时产生OnComm事件
MSComm1.InputLen = 14 '表示用Input函数从接收缓冲区读取的字节数
End Sub
----------------------------------------------------------
Private Sub MSComm1_OnComm() ,通讯事件
a = MSComm1.Input
Text7.Text = a
End Sub
------------------------------------------------------