 |
|
 |
iman0207 Level: Protégé
 Registered: 06-07-2010 Posts: 6
|
Re: 3964 protocol
Yes I did. I can communicate with my device. When i send STX character the device response with DLE character. This is part of code i got from this topic:
http://www.andreavb.com/forum/viewtopic.php?TopicID=7949
Option Explicit
Public DLE As String
Public STX As String
Public NAK As String
Public ETX As String
Private Sub Form_Load()
DLE = Chr(16) '10 esadecimale
STX = Chr(2)
NAK = Chr(21) '15 esadecimale
ETX = Chr(3)
MSComm1.CommPort = 1
MSComm1.Settings = "9600,e,8,1"
End Sub
Private Function SendCommand(strCommand As String) As String
Dim InBuffer As String
Dim tmp As Double
Dim i As Integer
Dim BCC As Integer
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
'send start transmission character
MSComm1.Output = STX
'loop until you receive response
tmp = Timer
Do
InBuffer = InBuffer & MSComm1.Input
If Timer - tmp > 2 Then
'wait response for 2 seconds
MsgBox "TimeOut", vbCritical
Exit Function
End If
Loop Until Left(InBuffer, 1) = DLE Or Left(InBuffer, 1) = NAK
'you received negative aknowledgement
If InBuffer = NAK Then
MsgBox "Negative Acknowledgement", vbCritical
Exit Function
End If
'OK
BCC = 0
'add DLE and ETX before calculating BCC
strCommand = strCommand & DLE & ETX
'calculate BCC
For i = 1 To Len(strCommand)
BCC = BCC Xor Asc(Mid(strCommand, i, 1))
Next
'add BCC to string command
strCommand = strCommand & Chr(BCC)
'send command
MSComm1.Output = strCommand
InBuffer = ""
'wait for DLE and STX
tmp=Timer
Do
InBuffer = InBuffer & MSComm1.Input
If Timer - tmp > 2 Then
'wait response for 2 seconds
MsgBox "TimeOut", vbCritical
Exit Function
End If
Loop Until Left(InBuffer,2)=DLE & STX
InBuffer=""
'Send DLE
MSComm1.Output = DLE
tmp = Timer
Do
InBuffer = InBuffer & MSComm1.Input
Loop Until Timer - tmp > 5
'wait response for 5 seconds
SendCommand = InBuffer
'Send DLE after receving message
MSComm1.Output = DLE
End Function
Private Sub Command1_Click()
Dim strResponse As String
Dim strCommand As String
Dim strMessage As String
Dim i As Integer
'compose TL command to read from memory register 0000 - per 5 registers long
'strCommand = Chr(7) & "TL" & Chr(1) & Chr(0) & Chr(0) & Chr(5)
'== Little change for command code========
strCommand = Chr(0) & Chr(0) & "ED" & hex(150) & hex(0) & Hex(64) & Hex(0) & Chr(12) & Chr(12)
'===============================
strResponse = SendCommand(strCommand)
For i = 1 To Len(strResponse)
strMessage = strMessage & Right("00" & Hex(Asc(Mid(strResponse, i, 1))), 2) & "-"
Next
MsgBox "Response: (" & strMessage & ")", vbInformation
End Sub
And, enclosed is my device manual on communicate with 3964r protocol:
[Edited by iman0207 on 15-07-2010 at 02:55 PM GMT]
____________________________
regards
____________________________ Attached:
S5RK12.zip 157 KB (Downloads: 2)
|
|
15-07-2010 at 07:29 AM |
|
|
admin Level: Administrator

 Registered: 04-04-2002 Posts: 637
|
Re: 3964 protocol
create a form with two buttons and this code:
Option Explicit
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, hpvSource As Any, ByVal cbCopy&)
'convert ASCII to real IEEE
Public Function ASCIIToReal(ByVal sValue As String) As Single
Dim b(0 To 3) As Byte
Dim x As Single
b(0) = Val(Format("&h" & Mid(sValue, 7, 2)))
b(1) = Val(Format("&h" & Mid(sValue, 5, 2)))
b(2) = Val(Format("&h" & Mid(sValue, 3, 2)))
b(3) = Val(Format("&h" & Mid(sValue, 1, 2)))
RtlMoveMemory x, b(0), 4
ASCIIToReal = x
End Function
'convert real IEEE to ASCII
Public Function RealToASCII(ByVal rValue As Single) As String
Dim b(0 To 3) As Byte
Dim nb(0 To 3) As Byte
Dim i As Integer
Dim x As Single
x = rValue
RtlMoveMemory b(0), x, 4
nb(0) = b(3)
nb(1) = b(2)
nb(2) = b(1)
nb(3) = b(0)
For i = 0 To 3
RealToASCII = RealToASCII & Right(String(2, "0") & Hex(nb(i)), 2)
Next
End Function
Private Sub Command1_Click()
MsgBox ASCIIToReal("42C80000")
End Sub
Private Sub Command2_Click()
MsgBox RealToASCII(100)
End Sub |
You can also use this two function to convert strings from/to chr() buffer to readable ASCII string. Hope it helps
'convert pait ASCII buffer to chr() string
Public Function ASCIIToString(s As String) As String
Dim i As Integer
For i = 1 To Len(s) Step 2
ASCIIToString = ASCIIToString & Chr(Format("&h" & Mid(s, i, 2)))
Next
End Function
'convert chr() string buffer in ASCII readable string
Public Function StringToASCII(ByVal s As String, ByVal NumWord As Long) As String
Dim i As Integer
For i = 1 To Len(s)
StringToASCII = StringToASCII & Right("00" & Hex(Asc(Mid(s, i, 1))), 2)
Next
End Function |
____________________________
AndreaVB
|
|
28-07-2010 at 06:32 AM |
|
|
|
|
 |
 |