马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
- Dim ts_i As Integer
- Dim ts_str As String
- If (intCount <> 0) Then
- ts_str = "receive:"
- For ts_i = 0 To UBound(r_input)
- ts_str = ts_str + CStr(Hex(r_input(ts_i))) + " "
- Next ts_i
- frm_main.Txt_msg.Text = ts_str + Chr(13) + Chr(10) + frm_main.Txt_msg.Text
- Else
- ts_str = "no receive:"
- frm_main.Txt_msg.Text = ts_str + Chr(13) + Chr(10) + frm_main.Txt_msg.Text
- End If
- End Function
- Public Function writewords(ByVal slv_id As Byte, ByVal addr As Long, ByVal lenth As Byte, ByRef lng_data() As Long, ByVal int_time As Integer) As Integer
- Dim ret As Integer
- Dim lenth1 As Integer
- Dim lenth2 As Integer
- Dim addr1 As Integer
- Dim addr2 As Integer
- Dim lng_data1() As Long
- Dim lng_data2() As Long
- Dim i As Integer
- If (lenth > 100) Then
- ReDim lng_data1(99)
- ReDim lng_data2(lenth - 100 - 1)
- For i = 0 To 99
- lng_data1(i) = lng_data(i)
- Next i
- For i = 100 To lenth - 1
- lng_data2(i - 100) = lng_data(i)
- Next i
- addr1 = addr
- addr2 = addr + 100
- lenth1 = 100
- lenth2 = lenth - 100
- ret = writewords1(slv_id, addr1, lenth1, lng_data1, int_time)
- If (ret <> 0) Then
- writewords = ret
- Exit Function
- End If
- ret = writewords1(slv_id, addr2, lenth2, lng_data2, int_time)
- If (ret <> 0) Then
- writewords = ret
- Exit Function
- End If
- Else
- writewords = writewords1(slv_id, addr, lenth, lng_data, int_time)
- End If
- End Function
- Public Function writewords1(ByVal slv_id As Byte, ByVal addr As Long, ByVal lenth As Byte, ByRef lng_data() As Long, ByVal int_time As Integer)
- 'On Error GoTo wrong
- Dim lngStartTimer As Long
- Dim lnginval As Long
- Dim bln_cx As Boolean
- Dim byt_data() As Byte
- Dim r_input() As Byte
- Dim CRC() As Byte
- Dim i As Integer
- ReDim byt_data(lenth * 2 - 1) As Byte
- For i = 0 To lenth - 1
- byt_data(2 * i) = lng_data(i) \ 256
- byt_data(2 * i + 1) = lng_data(i) Mod 256
- Next i
- 'Do Until bln_busy = False Or (Abs(timeGetTime - lng_time > 200))
- ' DoEvents
- ' DoEvents
- ' DoEvents
- ' DoEvents
- ' DoEvents
- 'Loop
- 'bln_busy = True
- frm_main.com_modbus.InBufferCount = 0
- If (lenth > 1) Then
- tran_modbus_order slv_id, 16, addr, byt_data
- Else
- tran_modbus_order slv_id, 6, addr, byt_data
- End If
- lngStartTimer = timeGetTime
- bln_success = False
- Dim intCount As Integer
- Do Until Abs(timeGetTime - lngStartTimer) > int_time Or bln_success
- DoEvents
- DoEvents
- DoEvents
- DoEvents
- DoEvents
- DoEvents
- intCount = frm_main.com_modbus.InBufferCount
- If intCount = 8 Then
- bln_success = True
- frm_main.com_modbus.InputLen = 0
- r_input = frm_main.com_modbus.Input
- frm_main.com_modbus.InBufferCount = 0
- End If
- Loop
-
- If bln_success And intCount = 8 Then
- CRC = CRC16(r_input)
- If CRC(0) = r_input(UBound(r_input) - 1) And CRC(1) = r_input(UBound(r_input)) Then
- writewords1 = 0 '通讯成功
- Else
- writewords1 = 2 '通讯错误
- End If
- ElseIf intCount <> 0 Then
- writewords1 = 2 '通讯错误
- Else
- writewords1 = 1 '通讯失败
- End If
复制代码 |