-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcanpipe.py
103 lines (78 loc) · 3.48 KB
/
pcanpipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pipe stream to wireshark ..can?? ;)
#
# "C:\Program Files\Wireshark\Wireshark.exe" -i\\.\pipe\wireshark -k
# wireshark_cmd=['C:\Program Files\Wireshark\Wireshark.exe', r'-i\\.\pipe\wireshark','-k']
#pip install pywin32
import win32pipe, win32file
import time
import subprocess
import struct
from PCANBasic import * ## PCAN-Basic library import
def compose_packet(_id, _len, _data, timestamp_seconds, timestamp_microseconds):
#b'\xc5\xea\x11Y\xceM\n\x00' #seconds + microseconds
incl_len = b'\x10\x00\x00\x00'
orig_len = b'\x10\x00\x00\x00'
#can_id = b'\x00\x00\x07\xdf'
can_id = struct.pack(">I", _id)
#can_len = b'\x08\x00\x00\x00'
can_len = struct.pack("<I", _len)
#can_data = b'\x02\x01\x11UUUUU'
can_data = _data
packet_data = can_id + can_len + can_data
packet = timestamp_seconds + timestamp_microseconds + incl_len + orig_len + packet_data
return packet
###*****************************************************************
if __name__ == '__main__':
print (" ")
print ("-------------------------------")
print ("- PCAN Wireshark pipe -")
print ("- by Antonio Della Rovere -")
print ("- cmd: \"C:\Program Files\Wireshark\Wireshark.exe\" -i\\.\pipe\wireshark -k -")
print ("-------------------------------")
pcan = PCANBasic()
baudrate = PCAN_BAUD_500K
hwtype = PCAN_TYPE_ISA
ioport = 0x2A0
interrupt = 11
# Connects a selected PCAN-Basic channel
#
result = pcan.Initialize(PCAN_USBBUS1,baudrate,hwtype,ioport,interrupt)
pcan.Reset(PCAN_USBBUS1)
CAN_Receive = TPCANMsg()
#create the named pipe \\.\pipe\wireshark
pipe = win32pipe.CreateNamedPipe(
r'\\.\pipe\wireshark',
win32pipe.PIPE_ACCESS_OUTBOUND,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
300,
None)
#connect to pipe
win32pipe.ConnectNamedPipe(pipe, None)
# libpcap header for CAN Bus data. See https://wiki.wireshark.org/Development/LibpcapFileFormat
#
header = b'\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\xe3\x00\x00\x00'
first = 1
#wait 2 second (not mandatory, but this let watching data coming trough the pipe)
time.sleep(2)
while(1):
Status, CAN_Receive, Timestamp = pcan.Read(PCAN_USBBUS1)
if CAN_Receive.ID > 0:
#timestamp = int(time.time())
#timestamp_seconds = struct.pack("<I", timestamp)
timestamp_seconds = struct.pack("<I", int(Timestamp.millis//1000))
#timestamp_microseconds = b'\x00\x00\x00\x00'
timestamp_microseconds = struct.pack("<I", int((Timestamp.millis%1000)*1000 + Timestamp.micros))
print (Timestamp.millis/1000, Timestamp.micros, '\n')
packet = compose_packet(CAN_Receive.ID, CAN_Receive.LEN, bytes(CAN_Receive.DATA), timestamp_seconds, timestamp_microseconds)
if first == 1:
first = 0
packet = header + packet
else:
packet = packet
win32file.WriteFile(pipe, packet)
#open and read an arbitrary pcap file (file must in same folder than script)
#cf = open(r'ipv4frags.pcap', 'rb')
#cf = open(r'pipetest.pcap', 'rb')
#data = cf.read()
#then pcap data appears into wireshark