vb6 code for communicating to micrologix

N

Thread Starter

nitin

i am working on a project which needs pc to plc communication through vb6.i had mscomm control in vb by using its oncom event but it did'nt worked out.i had checked the communication terminals using a multimeter which shows a costant voltage of 30v. does it require any handshaking to start the communication.i had also tried to get data by hyperterminal but no use.can anybody e-mail me the sample code necessary for this purpose.
 
K

Keith Bradley

Hi, I work heavly with the SLC500 range of AB processors, and I have experimented with communication between the PC and a 5/03 processor via MSComm. Now it has been some time since I looked at my code, I have it here in front of me now. 5/03 and Micrologix both use DF1 and any documentation I found leads me to recall that the code I was working on would work with Micrologix. Where I got : I was able to read all file types, and I am sure (but didn't get that far) I could write to them also. At my work we use a third party ocx to communicate, I was trying to get the third party out of the equation.

Ok, my code is in a complete mess so I will break it into simple english for you.

1:Do the business with your Application, MSComm and a command button, behind the command button this is what I did.

2:Clear MsComms Buffer
mscPLC.InputLen = 0
QueryBuffer = mscPLC.Input

3:Create Query (Build the string)

QueryBuffer = Chr(16) & Chr(2) & Chr DestinationNode) & Chr(SourceNode) & _
Chr(15) & Chr(0) & Chr(TransActionNo) & Chr(0) & Chr(162) & _
Chr(Size * 2) & Chr(FileNumber) & Chr(ConvertFileType(FileType+132)) & _
Chr(ElementNumber) & Chr(SubElementNumber) & Chr(16) & Chr(3)

Ok a break down of whats above
*Chr(16) & Chr(2) = Start DF1 Message
*DestinationNode =1
*SourceNode=0
*TransactionNo=any number between 0-15 or 17-255
*Size=no of integers (Times 2 as an int is 2 bytes)
*FileNumber=The File number ie. N10 = 10
*Chr(FileType+132)= N filetype = 5

'Case 0 Status
'Case 1 Bit
'Case 2 Timer
'Case 3 Counter
'Case 4 Control
'Case 5 Integer
'Case 6 Float
'Case 7 Output (Logical By Slot)
'Case 8 Input (Logical By Slot)
'Case 9 String
'Case 10 Ascii
'Case 11 BCD

*ElementNumber=Start read at element 7 ie. N7:7
*SubElementNumber=0
*Chr(16) & Chr(3)= Close DF1 Message

4:Calculate Crc Check Sum and add to Query
QueryBuffer = QueryBuffer & MsgCRC(QueryBuffer)

Paste below:
Public Function MsgCRC(Message As String) As String
'Calculate CRC checksum for message then add
'to end of message. Check for 10h byte 'DLE'
'within message and change to 'DLE DLE'.


'Function to calculate message CRC

Dim xByte As Integer
Dim xResult As Long

xByte = 3

Do

xResult = xResult Xor Asc(Mid(Message, xByte, 1))

RotateResult xResult
'test for 'DLE' and double up to DLE DLE
If Asc(Mid(Message, xByte, 1)) = 16 Then
'add extra DLE
Message = Left$(Message, xByte) + Chr(16) + Right$(Message, Len(Message) - xByte)

xByte = xByte + 1 'don't include in CRC

End If
xByte = xByte + 1

Loop While (xByte <= Len(Message) - 2)

xResult = xResult Xor 3 'ETX byte
RotateResult xResult

'add CRC checksum to message
MsgCRC = Chr(xResult Mod 256) + Chr(Int(xResult / 256))

End Function

Public Sub RotateResult(res&)
Dim bitout%, shift%

'Rotate result 8 times and combine with const.
For shift% = 1 To 8
bitout% = res& Mod 2 'test if bit will be shifted out
res& = Int(res& / 2) 'shift right
If bitout% Then
res& = res& Xor &H1000A001 'xor with constant
res& = res& - &H10000000 'clear top word
End If
Next shift%

End Sub

5:Send Message

mscPLC.Output = QueryBuffer

5:Wait for acknowledgment Or Time Out

TmpTime = Timer

Do

DoEvents

Loop While TmpTime + 3 > Timer And mscPLC.InBufferCount < 2

6:Remove acknowledgment from buffer

mscPLC.InputLen = 2
QueryBuffer = mscPLC.Input

7:Check for good acknowledgement

If QueryBuffer <> Chr(16) + Chr(6) Then


msgbox "Bad Ack"
Exit Function

End If

8: Wait for response

TmpTime = Timer

Do

DoEvents

Loop While TmpTime + 1 > Timer And mscPLC.InBufferCount < 12 + (Size * 2)

9:If timeout then exit
If mscPLC.InBufferCount < 12 + (Size * 2) Then

msgbox "Timed Out"
Exit Function

End If

10:Send acknowledgment
mscPLC.Output = Chr(16) + Chr(6)

11:Get response
mscPLC.InputLen = 0
QueryBuffer = mscPLC.Input

12:Remove surplus 'DLE's
TmpTime = 3

Do

If Mid(QueryBuffer, TmpTime, 1) = Chr(16) Then

QueryBuffer = Left(QueryBuffer, TmpTime) + Right(QueryBuffer, Len(QueryBuffer) - 1 - TmpTime)

End If

TmpTime = TmpTime + 1

Loop While TmpTime < Len(QueryBuffer) - 4


OK, I sort of need to go here, but QueryBuffer should now contain your read info. Email me at [email protected] and let me know how you get on. I believe in sharing code, so if you want what I have, if it will help, just say, or send me yours and I can see where your heading. Hope above helped you in some way.

Keith Bradley
> i am working on a project which needs pc to plc communication through vb6.i had mscomm control in vb by using its oncom event but it did'nt worked out.i had checked the communication terminals using a multimeter which shows a costant voltage of 30v. does it require any handshaking to start the communication.i had also tried to get data by hyperterminal but no use.can anybody e-mail me the sample code necessary for this purpose.
>

 
Top