ich will mit Visual Basic 2010 Express eine Modbus verbindung über rs232 aufbauen. Was muss ich dafür machen bzw. hat jemand ein Beispiel für mich ^^
Hier das was ich schon habe:
Code: Alles auswählen
Imports VB = Microsoft.VisualBasic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Msg(6) As Byte
Msg(0) = 1 'red-y Adresse
Msg(1) = 3 'ModBus-Funktion: Lesen
Msg(2) = 0 'Speicherstelle: 0xFF00
Msg(3) = 0 'Speicherstelle: 0x00FF
Msg(4) = 0
Msg(5) = 2 'Anzahl Register (= Byte / 2)
chkSum = Crc_(Msg(), 5)
MsgBox("chkSum")
'T_checksum.Text = chkSum
For i = 0 To 5
sendestring = sendestring & Msg(i)
Next i
For j = 1 To Len(sendestring) 'Befehl Senden an MFC
sendCHR = Mid$(sendestring, j, 1)
For i = 1 To 100
Debug.Print(sendCHR)
MSComm1.
Output = Chr(sendCHR)
If MSComm1.OutBufferCount = 0 Then Exit For
Next i
Next j
For j = 0 To 2 Step 2 'ChkSumme senden
sendCHR = Mid$(chkSum, j + 1, 2)
sendCHR = CInt("&H" & sendCHR)
For i = 1 To 100
MSComm1.Output = Chr$(sendCHR)
If MSComm1.OutBufferCount = 0 Then Exit For
Next i
Next j
End Sub
'****************************************************************
'* 4 Byte-Array aus red-y smart in 32 bit Fliesskomma umwandeln *
'****************************************************************
Function Float32(ByVal Msg() As Byte) As Double
Dim Exponent, Mantisse As Double
Dim Binaer, tmp As String
'*** 4 Byte-Array in 32 bit-String wandeln ***
Binaer = ""
For i = 0 To 3
tmp = CStr(Msg(i)) 'Asc(Zeichen)
For j = 0 To 7
Binaer = (tmp Mod 2) & Binaer 'Ist bit = 0 oder = 1?
tmp = Int(tmp / 2) 'Restliche bits rechts schieben
Next j
Next i
'*** Exponent extrahieren ***
Exponent = 0
For i = 0 To 7
Exponent = Exponent + 2 ^ (Val(Mid(Binaer, 9 - i, 1)) * i)
Next i
Exponent = Exponent - 127
'*** Mantisse extrahieren ***
Mantisse = 0
For i = 10 To 32
Mantisse = Mantisse + 0.5 ^ (Val(Mid(Binaer, i, 1)) * i)
Next i
Float32 = (-1) ^ (Val(VB.Left(Binaer, 1)) * 2 ^ Exponent * (1 + Mantisse))
End Function
'CRC berechnen
'*******************************
'* CRC-Prüfsumme bereitstellen *
'*******************************
Function Crc_(ByVal Msg() As Byte, ByVal n As Byte) As Long
Dim i, j As Byte
Dim crc As Long
Dim CRC_String As String
crc = 65535 '&HFFFF
For j = 0 To n
crc = crc Xor Msg(j)
For i = 1 To 8
If (crc And 1) = 1 Then '&H0001
crc = Int(crc / 2) Xor 40961 '&HA001
Else
crc = Int(crc / 2)
End If
Next i
Next j
'Achtung: Die folgenden beiden Zeilen tauschen hi- und lo-Byte
CRC_String = "000" + Hex(crc)
Crc_ = VB.Right(CRC_String, 2) & VB.Right(VB.Left(CRC_String, Len(CRC_String) - 2), 2)
End Function
End Class
Gruß Zim