-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PDS Port #38
base: master
Are you sure you want to change the base?
PDS Port #38
Changes from all commits
47215f8
5244937
ba96ee6
46d19d2
7a52010
bcfac9d
2f86881
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## UDP Send / Receive | ||
A simple example of sending and receiving a message using python | ||
annd UDP, to aid in the development of a slim wrapper for python UDP. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" config.py | ||
|
||
UDP Configuration values | ||
""" | ||
|
||
# UDP Config Values | ||
UDP_IP = "127.0.0.1" | ||
UDP_PORT = 5005 | ||
SEND_FREQUENCY = 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" udp_receive.py | ||
|
||
Receive and print a message via UDP | ||
""" | ||
|
||
import socket | ||
|
||
from config import * | ||
|
||
if __name__ == "__main__": | ||
sock = socket.socket(socket.AF_INET, # Internet | ||
socket.SOCK_DGRAM) # UDP | ||
sock.bind((UDP_IP, UDP_PORT)) | ||
|
||
while True: | ||
data, addr = sock.recvfrom(1024) # Buffer size is 1024 bytes | ||
print(f"Received message: {data}") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
""" udp_send.py | ||
|
||
Send a simple message over UDP. | ||
""" | ||
|
||
import socket | ||
import time | ||
|
||
from config import * | ||
|
||
if __name__ == "__main__": | ||
MESSAGE = b"Hello, World!" | ||
|
||
print(f"UDP target IP: {UDP_IP}") | ||
print(f"UDP target port: {UDP_PORT}") | ||
print(f"Message: {MESSAGE}\n") | ||
|
||
sock = socket.socket(socket.AF_INET, # Internet | ||
socket.SOCK_DGRAM) # UDP | ||
|
||
while True: | ||
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) | ||
print("Message sent...\n") | ||
time.sleep(1/SEND_FREQUENCY) | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" udp_connection.py | ||
|
||
Classes: | ||
UDPListener - Wrapper around a uni-directional UDP connection | ||
""" | ||
import logging | ||
import socket | ||
import select | ||
from datetime import timedelta | ||
|
||
|
||
class UDPListener: | ||
""" UDP Listener | ||
|
||
Uni-directional UDP listening on specified ip and port | ||
|
||
Fields: | ||
ip: string - connection address | ||
port: int - port to listen on | ||
max_buffer: int - max byte size of the UDP receive/send buffers | ||
timeout: timedelta - how long to poll for data on the socket before timing out | ||
""" | ||
|
||
def __init__(self, ip : str, port : int, max_buffer : int, timeout : timedelta): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this typehinting style?? |
||
self.sock = None | ||
self.ip = ip | ||
self.port = port | ||
self.max_buffer_size = max_buffer | ||
self.timeout = timeout | ||
|
||
@classmethod | ||
def transcribe(self, data : bytes): | ||
""" Log the received byte data """ | ||
logging.info(f"[DATA] {data}") | ||
|
||
def connect(self): | ||
""" Connect to socket """ | ||
try: | ||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.max_buffer_size) | ||
self.sock.bind((self.ip, self.port)) | ||
except Exception as e: | ||
self.disconnect() | ||
raise e | ||
|
||
def is_connected(self): | ||
""" Returns true if connected """ | ||
connected = self.sock is not None and self.sock.fileno() >= 0 | ||
return connected | ||
|
||
def disconnect(self): | ||
""" Disconnect from socket """ | ||
if self.sock is not None: | ||
self.sock.close() | ||
self.sock = None | ||
|
||
def recv(self): | ||
""" Receive UDP packet """ | ||
if not self.is_connected(): | ||
return None | ||
try: | ||
ready = select.select([self.sock], [], [], self.timeout.total_seconds()) | ||
if ready[0]: | ||
data = self.sock.recvfrom(self.max_buffer_size) | ||
if data is not None: | ||
return data[0] | ||
else: | ||
return None | ||
|
||
except Exception as e: | ||
self.disconnect() | ||
raise e | ||
|
||
def __str__(self): | ||
ret_str = f"""UDP Connection: | ||
\nIp: {self.ip} | ||
\nPort: {self.port} | ||
\nBuffer Size: {self.max_buffer_size} | ||
""" | ||
return ret_str | ||
Comment on lines
+75
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very nice |
||
|
||
if __name__ == "__main__": | ||
# Vehicle ECU | ||
VEHICLE_IP = "127.0.0.1" | ||
VEHICLE_UDP_PORT = 5005 | ||
|
||
con = UDPListener(VEHICLE_IP, VEHICLE_UDP_PORT, 1024, timedelta(seconds=2)) | ||
con.connect() | ||
|
||
counter = 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be getting used anywhere |
||
while True: | ||
data = con.recv() | ||
if data is not None: | ||
print(f"Data:{data.decode('utf-8')}") | ||
else: | ||
print("No data...") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" boring_packet.py | ||
|
||
Contains the BoringPacket class for packaging | ||
critical system info for The Boring Company. | ||
""" | ||
|
||
class BoringPacket(): | ||
""" Create and pack The Boring Struct | ||
""" | ||
raise NotImplementedError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new newline EOF |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" config.py | ||
|
||
Configuration values for: | ||
- InfluxDB | ||
- Vehicle ECU TCP/UDP Connections | ||
- Surface ECU TCP/UDP Connections | ||
""" | ||
|
||
# Influx | ||
INFLUX_HOST = "localhost" | ||
INFLUX_PORT = 8086 | ||
INFLUX_NAME = "paradigm_boring" | ||
INFLUX_USER = "paradigm" | ||
INFLUX_PW = "boring" | ||
|
||
# TBC Packet | ||
TEAM_NAME = "Paradigm Boring" | ||
|
||
# SocketIO | ||
SOCKET_SERVER = "http://localhost:5000" | ||
|
||
# Vehicle ECU | ||
VEHICLE_IP = "127.0.0.1" | ||
VEHICLE_UDP_PORT = 5005 | ||
|
||
# Surface ECU | ||
SURFACE_IP = "127.0.0.1" | ||
SURFACE_UDP_PORT = 5006 |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats going on here