borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2010 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Profibus-DP)Next Topic (Network Monitor / Listview problem) New Topic New Poll Post Reply
AndreaVB Forum : VB General : 3964 protocol Solved Topic
Poster Message
iman0207
Level: Protégé

Registered: 06-07-2010
Posts: 6

Ads by Lake Quincy Media
icon 3964 protocol

Hi Admin..
I just read your comment about rs 232/3964r posted by emiliana.
I think I have same problem like her.
I need to communicate with 3964 device through PC with VB code using serial port .
I have try your code.Its works but it doesn't give me a correct answer..
My device response with 00-00-00-01-10-03-16-
Refer to device manual, the device reports error if 4th byte unequal to zero
I think my problem is device command structure that i don't know.. Please help..

____________________________
regards

Ads by Lake Quincy Media
13-07-2010 at 03:15 AM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 637
icon Re: 3964 protocol

which post are you talking about? post the link to the other thread...

It is important that you know how to talk with your device, your manual should specify how to compose a message to be sent to your device...

____________________________
AndreaVB

13-07-2010 at 06:08 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
iman0207
Level: Protégé

Registered: 06-07-2010
Posts: 6
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 637
icon Re: 3964 protocol

communication parameters are correct? baud rate, start/stop bit, data length and parity?

this is where you initialize them
MSComm1.Settings = "9600,e,8,1"


I'll take a look at your protocol...

____________________________
AndreaVB

22-07-2010 at 03:32 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 637
icon Re: 3964 protocol

are you sure that this command shouldn't be like this:

strCommand = Chr(0) & Chr(0) & "ED" & chr(150) & chr(0) & chr(64) & chr(0) & chr(12) & chr(12)


please use chr instead of hex!!

150 means the register you want to read,
0 is fixed
other 2 bytes indicate the number of registers you want to read but you should invert them...0 and then 64...I think..
try this...
strCommand = Chr(0) & Chr(0) & "ED" & chr(150) & chr(0) & chr(0) & chr(64) & chr(12) & chr(12)


for a simpler command try this to read only two registers
strCommand = Chr(0) & Chr(0) & "ED" & chr(150) & chr(0) & chr(0) & chr(2) & chr(12) & chr(12)


and let me know...
what do the last two char(12) mean?

[Edited by admin on 22-07-2010 at 04:47 PM GMT]

____________________________
AndreaVB

22-07-2010 at 03:43 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
iman0207
Level: Protégé

Registered: 06-07-2010
Posts: 6
icon Re: 3964 protocol

Binggo..
Now your code works at all
Sorry for my late reply..
Special thanks for you..   
The chr(12) meaning for data type (teleprem). It should be &HFF for 9th and 10th bytes.but i have mistakes when convert to char$..
It's used for old version of my device.Since i use the newer version, data type (teleperm) is automatically ignored by device(my device manual said).
Now I can send and fetch data from my device.
But another problem is present..  
When I fetch the measurement value, the data is transmitted using IEEE 754-4byte format .e.g 100 is transmitted by 42-C8-00-00 (in HEX)
Please advise me how to convert  HEX to IEEE 754 using VBcode..
Sorry, my english is too bad..
Thanks..

[Edited by iman0207 on 28-07-2010 at 12:45 AM GMT]

____________________________
regards

27-07-2010 at 05:37 PM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 637
icon 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
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
iman0207
Level: Protégé

Registered: 06-07-2010
Posts: 6
icon Re: 3964 protocol

That's very useful  for me..
Thanks for your help  

____________________________
regards

02-08-2010 at 05:19 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : 3964 protocol Solved Topic
Previous Topic (Profibus-DP)Next Topic (Network Monitor / Listview problem) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2010 Andrea Tincaniborder