发表于:2004/5/21 13:03:00
#0楼
不知道是哪里错了,请指出,我对vb不熟
Dim keyin$
Dim SXD$
Dim RXD$
Private Sub Command1_Click()
keyin$ = InputBox$("输入通讯命令(不必输入SUM)", "输入命令对话盒", "")
MsgBox keyin$
If keyin$ = "" Then GoTo null_keyin
STX$ = Chr$(2): ETX$ = Chr$(3)
T$ = keyin$ + ETX$
Sum$ = chksum(T$)
SXD$ = STX$ + "E11805C40" + T$ + Sum$
Text1.Text = "命令字串:" + SXD$
RXD$ = send_recieve(SXD$)
Text2.Text = "回应字串:" + RXD$
null_keyin:
End Sub
Sub Option1_Click()
' 物件函数:致能通讯埠。
MSComm1.PortOpen = True
End Sub
Private Sub delay(delay_N%)
For i% = 1 To delay_N%
For J% = 1 To 30
xa! = 10 * Sin(10 * J% / 20)
Next J%
Next i%
End Sub
Private Function chksum$(T$)
L = Len(T$)
a = 0
For J = 1 To L
TJ$ = Mid$(T$, J, 1)
a = Asc(TJ$) + a
Next J
Sum$ = Hex$(a)
If Len(Sum$) = 1 Then Sum$ = "0" + Sum$
chksum$ = Right$(Sum$, 2)
End Function
Private Function resp_length%(SXD$)
'取得命令in/out
comx$ = Mid$(SXD$, 2, 1)
' 若是读入命令,取得位元组数
bytelenth% = CInt(Mid$(SXD$, 2, 1))
' 读入装置状态
If CInt(comx$) = 0 Then L% = bytelenth% * 2 + 4
' 写出装置状态
If CInt(comx$) = 1 Then L% = 1
If CInt(comx$) = 7 Then L% = 1
If CInt(comx$) = 8 Then L% = 1
resp_length% = L%
End Function
Private Sub Form_Load()
Form1.Caption = "FX2的通讯"
Command1.Caption = "输入命令按钮"
Option1.Caption = "开启通讯埠"
Option2.Caption = "关闭通讯埠"
Label1.Caption = "命令字串:"
Label2.Caption = "回应字串:"
Text1.Text = "": Text2.Text = ""
MSComm1.CommPort = 2
MSComm1.Settings = "9600,e,7,1"
MSComm1.RTSEnable = True '请求要送资料
' 启动通讯埠
Option1.Value = True
End Sub
Private Sub Option2_Click()
MSComm1.PortOpen = False
End Sub
Private Function send_recieve(SXD$)
MSComm1.Output = SXD$ ' 从通讯埠输出命令字串。
Call delay(1000) ' 延时一些时间。
resp_len% = resp_length%(SXD$)
MSComm1.InputLen = resp_len%
RXD$ = MSComm1.Input ' 读取回应字串。
send_recieve = RXD$
End Function
Dim keyin$
Dim SXD$
Dim RXD$
Private Sub Command1_Click()
keyin$ = InputBox$("输入通讯命令(不必输入SUM)", "输入命令对话盒", "")
MsgBox keyin$
If keyin$ = "" Then GoTo null_keyin
STX$ = Chr$(2): ETX$ = Chr$(3)
T$ = keyin$ + ETX$
Sum$ = chksum(T$)
SXD$ = STX$ + "E11805C40" + T$ + Sum$
Text1.Text = "命令字串:" + SXD$
RXD$ = send_recieve(SXD$)
Text2.Text = "回应字串:" + RXD$
null_keyin:
End Sub
Sub Option1_Click()
' 物件函数:致能通讯埠。
MSComm1.PortOpen = True
End Sub
Private Sub delay(delay_N%)
For i% = 1 To delay_N%
For J% = 1 To 30
xa! = 10 * Sin(10 * J% / 20)
Next J%
Next i%
End Sub
Private Function chksum$(T$)
L = Len(T$)
a = 0
For J = 1 To L
TJ$ = Mid$(T$, J, 1)
a = Asc(TJ$) + a
Next J
Sum$ = Hex$(a)
If Len(Sum$) = 1 Then Sum$ = "0" + Sum$
chksum$ = Right$(Sum$, 2)
End Function
Private Function resp_length%(SXD$)
'取得命令in/out
comx$ = Mid$(SXD$, 2, 1)
' 若是读入命令,取得位元组数
bytelenth% = CInt(Mid$(SXD$, 2, 1))
' 读入装置状态
If CInt(comx$) = 0 Then L% = bytelenth% * 2 + 4
' 写出装置状态
If CInt(comx$) = 1 Then L% = 1
If CInt(comx$) = 7 Then L% = 1
If CInt(comx$) = 8 Then L% = 1
resp_length% = L%
End Function
Private Sub Form_Load()
Form1.Caption = "FX2的通讯"
Command1.Caption = "输入命令按钮"
Option1.Caption = "开启通讯埠"
Option2.Caption = "关闭通讯埠"
Label1.Caption = "命令字串:"
Label2.Caption = "回应字串:"
Text1.Text = "": Text2.Text = ""
MSComm1.CommPort = 2
MSComm1.Settings = "9600,e,7,1"
MSComm1.RTSEnable = True '请求要送资料
' 启动通讯埠
Option1.Value = True
End Sub
Private Sub Option2_Click()
MSComm1.PortOpen = False
End Sub
Private Function send_recieve(SXD$)
MSComm1.Output = SXD$ ' 从通讯埠输出命令字串。
Call delay(1000) ' 延时一些时间。
resp_len% = resp_length%(SXD$)
MSComm1.InputLen = resp_len%
RXD$ = MSComm1.Input ' 读取回应字串。
send_recieve = RXD$
End Function