forked from ovravna/pican
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pican-server.py
68 lines (48 loc) · 1.63 KB
/
pican-server.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
#!/usr/bin/env python
import socket
import struct
from threading import Thread
from time import time
import cangen
def send_tcp(can_socket, tcp_socket, start_time):
while True:
recv_msg = can_socket.recv(256)
t = int(round(time() * 1000)) - start_time
t_array = [n for n in struct.pack('Q', t)]
# print('msg:', [hex(m) for m in recv_msg])
l = 20
msg = [0] * l
msg[0] = 0x88
msg[1] = 0
msg[2] = 0x00 if bus0 else 0x10
msg[2] |= recv_msg[1]
msg[3] = recv_msg[0]
for i in range(8):
msg[4 + i] = recv_msg[i + 8]
msg[12 + i] = t_array[i]
# print('msg:', [hex(m) for m in msg])
# print(bytearray(msg))
# print(len(msg))
tcp_socket.send(bytearray(msg))
TCP_IP = '192.168.220.1'
TCP_PORT = 5005
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
can0_socket = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
can0_socket.bind(('can0',))
can1_socket = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
can1_socket.bind(('can1',))
tcp_socket0 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket0.bind((TCP_IP, TCP_PORT))
tcp_socket0.listen(1)
print('Listening on ', TCP_IP, ':', TCP_PORT, sep='')
conn, addr = tcp_socket0.accept()
print('Connection address:', addr)
bus0 = False
now = lambda: int(round(time() * 1000))
time_0 = now()
sender = Thread(target= cangen.sending)
sender.start()
reader0 = Thread(target=send_tcp, args=(can0_socket, conn, time_0))
reader0.start()
reader1 = Thread(target=send_tcp, args=(can1_socket, conn, time_0))
reader1.start()