I'm trying to get two systems to communicate, I want my C# application to talk trash with a TCP server written in Python.
First I thought of serialization, and got a good look at google's protobuf. But don't you only need serialization if you have complex types and datastructures. I don't. I only want to send a enum (The default underlying type of the enumeration elements is int (Signed 32-bit integer).).
But the enum defined is rather large (C#):
[Flags]
public enum RobotCommands
{
reset = 0x0, // 0
turncenter = 0x1, // 1
turnright = 0x2, // 2
turnleft = 0x4, // 4
standstill = 0x8, // 8
moveforward = 0x10, // 16
movebackward = 0x20,// 32
utility1on = 0x40, // 64
utility1off = 0x80, // 128
utility2on = 0x100, // 256
utility2off = 0x200 // 512
}
So really, do I need serialization? What's the easiest way by Python to be able to read my enums that I send it?
I've tried just sending it as string, hoping to convert them back, but they seem to be string:
#!/usr/bin/env python
import socket
TCP_IP = '192.168.1.66'
TCP_PORT = 30000
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
while True:
data = conn.recv(BUFFER_SIZE).encode("hex")
print "received data:", data
if( (data & 0x8) == 0x8 ):
print("STANDSTILL");
if not data: break
conn.send(data)
conn.close()