profisniff 1.3 KB

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