Please help with RS232 DF1 Drive

H

Thread Starter

Hidroilio Perez

<p>Can anybody please help me out with this?

<p>I am trying to create a df1 RS232 drive to communicate with AB PLC 5 using Visual basic 6. After weeks of searching the only thing that I found to get me started is this code from this forum.

<p>I have made some modification but I am not able to make it work. I know that the ConvertFileType function is missing. I contact the author of the code but did not receive any answer yet. I was reading the AB DF1 Protocol but I can not figure it out. Any help would be appreciated.

<p>Best regards,

<p>Hidroilio Perez<br>
email [email protected]
<pre>

Option Explicit
'\\\\\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 Output
'\\\\\Case 1 Input
'\\\\\Case 2 status
'\\\\\Case 3 binary
'\\\\\Case 4 timer
'\\\\\Case 5 counter
'\\\\\Case 6 control
'\\\\\Case 7 integer
'\\\\\Case 8 float
'\\\\\Case 9 - 999 user config
'\\\\\Case 10 Ascii
'\\\\\Case 11 BCD
'\\\\\ElementNumber=Start read at element 7 ie. N7:7 O:031/14

'\\\\\SubElementNumber=0

'\\\\\timer/counte
'\\\\\0 Control
'\\\\\1 preset
'\\\\\2 accumulated

'\\\\\BT
'\\\\\0 Control
'\\\\\1 RLEN
'\\\\\2 DLEN
'\\\\\3 data file #
'\\\\\4 element #
'\\\\\5 rack/grp/slot


'\\\\\ $ N 10 : 360
'\\\\\ | | | | |
'\\\\\Displayed Logical -----------| | | | |
'\\\\\\\\address symbol | | | |
'\\\\\File type identifier ----------| | | |
'\\\\\File Number ----------------------| | |
'\\\\\Delimiter --------------------------| |
'\\\\\Element Number ------------------------|

'\\\\\Chr(16) & Chr(3)= Close DF1 Message


Dim DestinationNode As Integer
Dim SourceNode As Integer
Dim TransactionNo As Integer
Dim Size As Integer
Dim FileNumber As Integer
Dim FileType As Integer
Dim ElementNumber As Integer
Dim SubElementNumber As Integer
Dim TimeOut As Integer

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

Private Sub Form_Load()

RS232PLC.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
RS232PLC.Settings = "19200,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
RS232PLC.InputLen = 0
' Open the port.
RS232PLC.PortOpen = True
DestinationNode = 0
SourceNode = 0
TransactionNo = 5
Size = 4
FileNumber = 0
FileType = 7
ElementNumber = 31
SubElementNumber = 14

End Sub

Private Sub Timer1_Timer()

Dim TmpTime As Date
Dim QueryBuffer As String

'2:Clear MsComms Buffer
RS232PLC.InputLen = 0
QueryBuffer = RS232PLC.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$(FileType + 132) & _
Chr$(ElementNumber) & Chr$(SubElementNumber) & _
Chr$(16) & Chr$(3)

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

'5: Send Message

RS232PLC.Output = QueryBuffer

'5:Wait for acknowledgment Or Time Out

TmpTime = Now

Do

DoEvents

If DateDiff("s", TmpTime, Now) >= 3 And _
RS232PLC.InBufferCount < 2 Then

Exit Do

End If

Loop

'6:Remove acknowledgment from buffer

RS232PLC.InputLen = 2
QueryBuffer = RS232PLC.Input

'7:Check for good acknowledgement

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

MsgBox "Bad Ack"
Exit Sub

End If

'8: Wait for response

TmpTime = Now

Do

DoEvents

If DateDiff("s", TmpTime, Now) >= 3 And _
RS232PLC.InBufferCount < 12 + (Size * 2) Then

Exit Do

End If

Loop


'9:If timeout then exit

If RS232PLC.InBufferCount < 12 + (Size * 2) Then

MsgBox "Timed Out"
Exit Sub

End If

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

'11:Get response
RS232PLC.InputLen = 0
QueryBuffer = RS232PLC.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

End Sub

Public Sub ConvertFileType(FileType As Integer)

End Sub
</pre>
 
Top