profisniff 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. """
  3. #
  4. # PROFIBUS - Telegram sniffer
  5. #
  6. # Copyright (c) 2016 Michael Buesch <m@bues.ch>
  7. #
  8. # Licensed under the terms of the GNU General Public License version 2,
  9. # or (at your option) any later version.
  10. #
  11. """
  12. from pyprofibus.phy_serial import *
  13. from pyprofibus.fdl import *
  14. import sys
  15. import getopt
  16. def usage():
  17. print("PROFIBUS bus sniffer")
  18. print("")
  19. print("Usage: profisniff [OPTIONS] DEVICE")
  20. print("")
  21. print("DEVICE is the PHY device /dev/ttySx")
  22. print("")
  23. print("Options:")
  24. print(" -h|--help Show this help.")
  25. def main():
  26. try:
  27. (opts, args) = getopt.getopt(sys.argv[1:],
  28. "h",
  29. [ "help", ])
  30. except getopt.GetoptError as e:
  31. sys.stderr.write(str(e) + "\n")
  32. usage()
  33. return 1
  34. for (o, v) in opts:
  35. if o in ("-h", "--help"):
  36. usage()
  37. return 0
  38. if len(args) != 1:
  39. usage()
  40. return 1
  41. dev = args[0]
  42. try:
  43. phy = CpPhySerial(dev)
  44. xceiv = FdlTransceiver(phy)
  45. xceiv.setRXFilter(None)
  46. except ProfibusError as e:
  47. sys.stderr.write("ERROR: %s\n" % str(e))
  48. return 1
  49. try:
  50. while True:
  51. try:
  52. ok, telegram = xceiv.poll(-1)
  53. if not ok or not telegram:
  54. continue
  55. print(telegram)
  56. except ProfibusError as e:
  57. sys.stderr.write("ERROR: %s\n" % str(e))
  58. except KeyboardInterrupt:
  59. return 0
  60. return 1
  61. if __name__ == "__main__":
  62. sys.exit(main())