Skip to content

Commit

Permalink
added UDPtoCANDUMP logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Daily committed Dec 18, 2021
1 parent 71f8e49 commit 005670f
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 4 deletions.
24 changes: 20 additions & 4 deletions CAN_to_UDP/CAN_to_UDP.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ EthernetUDP Udp;
#define WIZNET_CHIP_SELECT 10


#define GREEN_LED_PIN 5
#define RED_LED_PIN 16
bool GREEN_LED_state;
bool RED_LED_state;


uint32_t UDP_TX_Count;
uint32_t CAN0_RX_Count;
Expand All @@ -44,7 +49,7 @@ elapsedMicros microsecondsPerSecond;
#define UDP_TX_PACKET_MAX_SIZE 1472 //increase UDP size
#define CAN_FRAME_SIZE 19
#define CAN_BUFFER_THRESHOLD UDP_TX_PACKET_MAX_SIZE-CAN_FRAME_SIZE
#define UDP_TX_PERIOD_MS 40
#define UDP_TX_PERIOD_MS 100

byte CANBuffer[UDP_TX_PACKET_MAX_SIZE];
byte packetBuffer[UDP_TX_PACKET_MAX_SIZE];
Expand All @@ -57,20 +62,23 @@ uint16_t CANBufferPosition = 0;
uint8_t mac[6];

// Use this as a placeholder. We'll change it when we get the local IP.
IPAddress broadcast_address(192,168,1,255);
unsigned int localPort = 8899; // local port to listen for UDP packets
IPAddress broadcast_address(192,168,1, 49);
unsigned int localPort = 10559; // local port to listen for UDP packets

#define NTP_PACKET_SIZE 48 // NTP time stamp is in the first 48 bytes
const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP serverof the message


void setup(void) {
pinMode(GREEN_LED_PIN, OUTPUT); digitalWrite(GREEN_LED_PIN,HIGH);
pinMode(RED_LED_PIN, OUTPUT); digitalWrite(RED_LED_PIN,HIGH);
pinMode(13, OUTPUT); digitalWrite(13, HIGH);
pinMode(14, OUTPUT); digitalWrite(14, LOW); /* optional tranceiver enable pin */
pinMode(35, OUTPUT); digitalWrite(35, LOW); /* optional tranceiver enable pin */
setSyncProvider(getTeensy3Time);
setSyncInterval(1);



// start Ethernet and UDP
Ethernet.init(WIZNET_CHIP_SELECT); // Based on the Schematic for the CAN-2 Ethernet board.
teensyMAC(mac); // Assign a unique MAC address. This uses the processors MAC (not the Wiznet)
Expand All @@ -97,7 +105,9 @@ void setup(void) {
Serial.print("The Local IP address is: ");
Serial.println(Ethernet.localIP());
broadcast_address = Ethernet.localIP();
broadcast_address[2] = 255;
broadcast_address[3] = 255;

Serial.print("The Broadcast IP address is: ");
Serial.println(broadcast_address);
Serial.print("The subnet mask is: ");
Expand Down Expand Up @@ -190,6 +200,9 @@ void canSniff(const CAN_message_t &msg, uint8_t channel, uint32_t CAN_RX_Count)
CANBufferPosition += 1;
memcpy(&CANBuffer[CANBufferPosition],&msg.buf,8);
CANBufferPosition += 8;

RED_LED_state = !RED_LED_state;
digitalWrite(RED_LED_PIN,RED_LED_state);
}

/*
Expand Down Expand Up @@ -241,6 +254,9 @@ void loop() {
time_t timeStamp = now();
memcpy(&CANBuffer[CANBufferPosition], &timeStamp, 4);
CANBufferPosition += 4;

GREEN_LED_state = !GREEN_LED_state;
digitalWrite(GREEN_LED_PIN,GREEN_LED_state);
}

int packetSize = Udp.parsePacket();
Expand Down
130 changes: 130 additions & 0 deletions UDP_to_candump/Capture CAN from UDP.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "0f12f0b2",
"metadata": {},
"outputs": [],
"source": [
"import socket\n",
"import logging\n",
"import struct"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "729f2f52",
"metadata": {},
"outputs": [],
"source": [
"UDP_IP = \"\" #CAN to Ethernet device (program this with Arduino)\n",
"UDP_PORT = 9101 # This is specified in the CAN2Ethernet Arduino Sketch\n",
"rxSocket = socket.socket(socket.AF_INET, #Internet\n",
" socket.SOCK_DGRAM) #UDP \n",
"rxSocket.bind((UDP_IP,UDP_PORT))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "836fb6ec",
"metadata": {},
"outputs": [],
"source": [
"#logging.basicConfig(filename='can_data.txt', filemode='w', level=logging.DEBUG)\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"for h in logger.handlers:\n",
" logger.removeHandler(h)\n",
"logger.setLevel(logging.INFO)\n",
"fh = logging.handlers.RotatingFileHandler('can_data.txt',mode='w',maxBytes=100000000,backupCount=999)\n",
"fh.setLevel(logging.INFO)\n",
"formatter = logging.Formatter('%(message)s')\n",
"fh.setFormatter(formatter)\n",
"logger.addHandler(fh)\n",
"logger.handlers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e76fecd",
"metadata": {},
"outputs": [],
"source": [
"\n",
"for i in range(500):\n",
" data,addr = rxSocket.recvfrom(1500) \n",
" message_format = data[0]\n",
" udp_tx_count = struct.unpack(\"<L\",data[1:5])[0]\n",
" can_rx_count = struct.unpack(\"<L\",data[5:9])[0]\n",
" device_msg_time_sec = struct.unpack(\"<L\",data[9:13])[0]\n",
" print(message_format, udp_tx_count, can_rx_count, device_msg_time_sec)\n",
" for msg_start in range(13,len(data),19):\n",
" \n",
" i = msg_start\n",
" channel = data[i]\n",
" if channel !=0:\n",
" break\n",
" i+=1\n",
" micros = struct.unpack(\"<L\",data[i:i+3]+b'\\x00')[0]\n",
" i+=3\n",
" can_controller_time = struct.unpack(\"<H\",data[i:i+2])[0]\n",
" i+=2\n",
" can_id = struct.unpack(\"<L\",data[i:i+4])[0]\n",
" i+=4\n",
" can_len = data[i]\n",
" i+=1\n",
" data_string=\"\".join([\"{:02X}\".format(b) for b in data[i:i+can_len]])\n",
" \n",
" if micros>=1000000:\n",
" device_msg_time_sec+=1\n",
" micros-=1000000\n",
" \n",
" #print('\\t',channel,micros,can_controller_time,\"{:08X}\".format(can_id),can_len,data_string)\n",
" logger.info(\"({}.{:06d}) can{} {:08X}#{}\".format(device_msg_time_sec,micros,channel,can_id,data_string))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d13c5531",
"metadata": {},
"outputs": [],
"source": [
"\"use"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c07b7d9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
50 changes: 50 additions & 0 deletions UDP_to_candump/logCAN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import socket
import logging
import logging.handlers
import struct
UDP_IP = "" #CAN to Ethernet device (program this with Arduino)
UDP_PORT = 9101 # This is specified in the CAN2Ethernet Arduino Sketch
rxSocket = socket.socket(socket.AF_INET, #Internet
socket.SOCK_DGRAM) #UDP
rxSocket.bind((UDP_IP,UDP_PORT))

logger = logging.getLogger(__name__)
for h in logger.handlers:
logger.removeHandler(h)
logger.setLevel(logging.INFO)
fh = logging.handlers.RotatingFileHandler('can_data.txt',mode='w',maxBytes=100000000,backupCount=999)
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)


while True:
data,addr = rxSocket.recvfrom(1500)
message_format = data[0]
udp_tx_count = struct.unpack("<L",data[1:5])[0]
can_rx_count = struct.unpack("<L",data[5:9])[0]
device_msg_time_sec = struct.unpack("<L",data[9:13])[0]
#print(message_format, udp_tx_count, can_rx_count, device_msg_time_sec)
for msg_start in range(13,len(data),19):
i = msg_start
channel = data[i]
if channel !=0:
break
i+=1
micros = struct.unpack("<L",data[i:i+3]+b'\x00')[0]
i+=3
can_controller_time = struct.unpack("<H",data[i:i+2])[0]
i+=2
can_id = struct.unpack("<L",data[i:i+4])[0]
i+=4
can_len = data[i]
i+=1
data_string="".join(["{:02X}".format(b) for b in data[i:i+can_len]])

if micros>=1000000:
device_msg_time_sec+=1
micros-=1000000

#print('\t',channel,micros,can_controller_time,"{:08X}".format(can_id),can_len,data_string)
logger.info("({}.{:06d}) can{} {:08X}#{}".format(device_msg_time_sec,micros,channel,can_id,data_string))

0 comments on commit 005670f

Please sign in to comment.