Complete the code to open a serial port connection.
serial_port = Serial(port=[1], baudrate=9600)
To open a serial port, you specify the port name like "COM3".
Complete the code to create an Ethernet socket connection.
sock = socket.socket(socket.AF_INET, socket.SOCK_[1])For TCP (Ethernet) connections, use SOCK_STREAM for a reliable stream socket.
Fix the error in the code to send data over serial port.
serial_port.write([1])Serial write requires bytes, so prefix string with b to make bytes literal.
Fill both blanks to create a dictionary mapping communication types to their typical ports.
comm_ports = {"serial": [1], "ethernet": [2]Serial ports use names like "COM3"; Ethernet commonly uses port 80 for HTTP.
Fill all three blanks to create a function that sends a message over the chosen communication type.
def send_message(comm_type, message): if comm_type == "serial": port = Serial(port=[1], baudrate=9600) port.write(message.[2]()) elif comm_type == "ethernet": sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(([3], 80)) sock.sendall(message.encode())
Serial port needs a port name like "COM4". To send bytes, encode the message. Ethernet socket connects to an IP address.