Serial data over UDP Sockets in Python
Question: I may be going about this the wrong way but that's why I'm asking the
question.

I have a source of serial data that is connected to a SOC then streams the
serial data up to a socket on my server over UDP. The baud rate of the raw
data is 57600, I'm trying to use Python to receive and parse the data. I
tested that I'm receiving the data successfully on the port via the script
below (found here: <https://wiki.python.org/moin/UdpCommunication>)

    
    
    import socket
    
    UDP_IP = "MY IP"
    UDP_PORT = My PORT
    
    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 "received message:", data
    

Since I'm not reading the data with the .serial lib in Python or setting the
baud rate to read at it comes all garbled, as would be expected. My end goal
is to be able to receive and parse the data for server side processing and
also have another client connect to the raw data stream piped back out from
the server (proxy) which is why I'm not processing the data directly from the
serial port on the device.

So my question is, how can I have Python treat the socket as a serial port
that I can set a baud rate on and #import serial and .read from? I can't seem
to find any examples online which makes me think I'm overlooking something
simple or am trying to do something stupid.

# sadface


Answer: You can't treat a socket as a serial line. A socket can only send and receive
data (data stream for TCP, packets for UDP). If you would need a facility to
control the serial line on the SOC you would need to build an appropriate
control protocol over the socket, i.e. either use another socket for control
like FTP does or use in-band control and distinguish between controlling and
data like HTTP does. And of course both sides of the connection have to
understand this protocol.

