请选择 进入手机版 | 继续访问电脑版
查看: 192|回复: 0

[.NET开发] 欧姆龙 PLC 串行口

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2015-11-23 17:52:06
http://www.codeproject.com/KB/system/OmronPLCHostLinkProtocol.aspx
Surpass Omron OCX OPC Component and use a user friendly .NET based class to interface Omron PLC Series
  1. Do
  2. Do
  3. str_in = str_in & Link.ReadExisting
  4. Loop While ((str_in.Length < length_predicted) And ((DateAndTime.Now.Ticks - tmr_indicator) < lng_length))
  5. ....
  6. 'Checking FCS code
  7. 'Checking PLC Response code (No Command Error)
  8. ...
  9. 'Assume we're waiting for frame(s)
  10. 'The matter that must be checked is the penultimate and the last
  11. 'character received from PLC.
  12. if mid$(str_in,str_int.length - 1,1)<>"*" then 'Means some frame(s) are ready to be received
  13. 'First Process Data
  14. 'Clear RS232 Input Buffer
  15. str_out = Link.readExisting
  16. 'str_out = ""
  17. link.Write(vbCr)'Indicates Host Computer is ready to receive data
  18. ...
  19. end if
  20. ...
  21. Loop While loop_condition
复制代码
  1. Dim loop_count As Integer = 0, count_predicted As Integer = 0,
  2. _As Long = 0, tmr_indicator As Long = 0,
  3. _As Integer = 0
  4. Dim str_in As String = "", str_in_final As String = ""
  5. str_in = Link.ReadExisting()
  6. str_in = ""
  7. Link.Write(str_out)
  8. Do
  9. str_in = ""
  10. count_predicted = IIf(count > 30, 30, count)
  11. If loop_count = 0 Then
  12. If count_predicted = count Then
  13. length_predicted = count_predicted * 4 + 11
  14. Else
  15. length_predicted = count_predicted * 4 + 10
  16. End If
  17. Else
  18. If count_predicted = count Then
  19. length_predicted = count_predicted * 4 + 4
  20. Else
  21. length_predicted = count_predicted * 4 + 3
  22. End If
  23. End If
  24. lng_length = (length_predicted * ByteTime * 1000) + delay_ms
  25. lng_length *= TimeSpan.TicksPerMillisecond
  26. tmr_indicator = DateAndTime.Now.Ticks
  27. Do
  28. str_in = str_in & Link.ReadExisting
  29. Loop While ((str_in.Length < length_predicted) And ((DateAndTime.Now.Ticks - tmr_indicator) < lng_length))
  30. If ((loop_count > 0) And (str_in.Length < length_predicted)) Then Throw New OmronException("Error in Data line, Data corrupted!")
  31. If loop_count = 0 Then 'Indicates first frame
  32. p_response = Hex2Int(Mid$(str_in, 6, 2))
  33. If Mid$(str_in, str_in.Length, 1) <> vbCr Then Throw New OmronException("Error in Data line, Data corrupted!")
  34. If Not (FCSCode_Get(str_in)) Then Throw New OmronException("Comminucation Error, Please check the line!")If p_response <> Omron_Response_Code.Command_Completed_Normally Then Throw New OmronException(Response(p_response)(0))
  35. If count_predicted = count Then 'One block contains the whole response
  36. str_in_final = str_in_final & Mid$(str_in, 8, str_in.Length - 11)
  37. Else 'It's the first block of a sequence of response blocks
  38. str_in_final = str_in_final & Mid$(str_in, 8, str_in.Length - 10)
  39. End If
  40. Else
  41. If count_predicted = count Then 'Indicates last frame
  42. str_in_final = str_in_final & Mid$(str_in, 1, str_in.Length - 4)
  43. Else 'Indicates intermediate frame
  44. str_in_final = str_in_final & Mid$(str_in, 1, str_in.Length - 3)
  45. End If
  46. End If
  47. str_in = Link.ReadExisting
  48. if count_predicted < count then Link.Write(vbCr)
  49. str_in = ""
  50. count = IIf(count - 30 > 0, count - 30, 0)
  51. loop_count += 1
  52. Loop While count > 0
复制代码


回复

使用道具 举报