#!/usr/bin/env python3

import binascii
import csv
import sys
from panda import Panda

def can_logger():

  try:
    print("Trying to connect to Panda over USB...")
    p = Panda()

  except AssertionError:
    print("USB connection failed. Trying WiFi...")

    try:
      p = Panda("WIFI")
    except:
      print("WiFi connection timed out. Please make sure your Panda is connected and try again.")
      sys.exit(0)

  try:
    outputfile = open('output.csv', 'wb')
    csvwriter = csv.writer(outputfile)
    #Write Header
    csvwriter.writerow(['Bus', 'MessageID', 'Message', 'MessageLength'])
    print("Writing csv file output.csv. Press Ctrl-C to exit...\n")

    bus0_msg_cnt = 0
    bus1_msg_cnt = 0
    bus2_msg_cnt = 0

    while True:
      can_recv = p.can_recv()

      for address, _, dat, src  in can_recv:
        csvwriter.writerow([str(src), str(hex(address)), "0x" + binascii.hexlify(dat), len(dat)])

        if src == 0:
          bus0_msg_cnt += 1
        elif src == 1:
          bus1_msg_cnt += 1
        elif src == 2:
          bus2_msg_cnt += 1

        print("Message Counts... Bus 0: " + str(bus0_msg_cnt) + " Bus 1: " + str(bus1_msg_cnt) + " Bus 2: " + str(bus2_msg_cnt), end='\r')

  except KeyboardInterrupt:
    print("\nNow exiting. Final message Counts... Bus 0: " + str(bus0_msg_cnt) + " Bus 1: " + str(bus1_msg_cnt) + " Bus 2: " + str(bus2_msg_cnt))
    outputfile.close()

if __name__ == "__main__":
  can_logger()

@@ -21,24 +21,8 @@
 n3%0A%0A
-import binascii%0A
 impo
@@ -443,17 +443,16 @@
 csv', 'w
-b
 ')%0A    c
@@ -862,36 +862,24 @@
 )), 
+f
 %220x
-%22 + binascii.hexlify(dat)
+%7Bdat.hex()%7D%22
 , le
@@ -1054,16 +1054,17 @@
   print(
+f
 %22Message
@@ -1077,32 +1077,25 @@
 s... Bus 0: 
-%22 + str(
+%7B
 bus0_msg_cnt
@@ -1090,37 +1090,33 @@
 bus0_msg_cnt
-) + %22
+%7D
  Bus 1: 
 %22 + str(bus1
@@ -1099,32 +1099,25 @@
 cnt%7D Bus 1: 
-%22 + str(
+%7B
 bus1_msg_cnt
@@ -1112,37 +1112,33 @@
 bus1_msg_cnt
-) + %22
+%7D
  Bus 2: 
 %22 + str(bus2
@@ -1121,32 +1121,25 @@
 cnt%7D Bus 2: 
-%22 + str(
+%7B
 bus2_msg_cnt
@@ -1138,17 +1138,18 @@
 _msg_cnt
-)
+%7D%22
 , end='%5C
@@ -1191,16 +1191,17 @@
   print(
+f
 %22%5CnNow e
@@ -1239,24 +1239,17 @@
  Bus 0: 
-%22 + str(
+%7B
 bus0_msg
@@ -1256,21 +1256,17 @@
 _cnt
-) + %22
+%7D
  Bus 1: 
 %22 + 
@@ -1261,24 +1261,17 @@
  Bus 1: 
-%22 + str(
+%7B
 bus1_msg
@@ -1278,21 +1278,17 @@
 _cnt
-) + %22
+%7D
  Bus 2: 
 %22 + 
@@ -1287,16 +1287,9 @@
  2: 
-%22 + str(
+%7B
 bus2
@@ -1296,17 +1296,18 @@
 _msg_cnt
-)
+%7D%22
 )%0A    ou

Fix can_logger.py to run correctly on python3 (#392)