I am trying to send data from a C program to a python script through sockets in order to perform real-time visualization of the data using matplotlib. I have also created a GUI using wxPython. I have used socket module, SocketServer module and twisted. In each one I had different problems.
Using socket module I was getting more than one messages combined. I reduced the buffer size of the recv() function but I was then getting only one package and after that nothing.
Then I started using twisted. I was still getting the packages as a collection and not one by one. Moreover when a delay was inserted in C file my python script crashed.
Then i moved to SocketServer and I created a thread to run the server on. The messages were coming as I wanted to but I can no longer interact with the GUI.
All I want to do is send a string of 4 values to the Python script, strip it and the plot it, having an interactive UI and I can't find an example of server,matplotlib and wxPython collaboration.
This is the C code that I found and I am using:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#define PORT 9992
#define HOST "localhost"
#define DIRSIZE 8192
main(argc, argv)
int argc; char **argv;
{
char hostname[100];
char dir[DIRSIZE];
int sd;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent *hp;
char message[50];
int i = 0;
int count = 50;
strcpy(hostname,HOST);
if (argc>2)
{ strcpy(hostname,argv[2]); }
/* go find out about the desired host machine */
if ((hp = gethostbyname(hostname)) == 0) {
perror("gethostbyname");
exit(1);
}
/* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_port = htons(PORT);
/* grab an Internet domain socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
/* connect to PORT on HOST */
if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
perror("connect");
exit(1);
}
/* send a message to the server PORT on machine HOST */
while (i < 100){
sprintf(message, "%d %d %d %d \n", count, count + 50, count + 100, count + 130);
if (send(sd, message, strlen(message), 0) == -1) {
perror("send");
exit(1);
}
count = count + 50;
i++;
sleep(1);
}
shutdown (sd, 2);
}
And this is the Python code that i currently have (after searching all over the net):
class ThreadedEchoRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
cur_thread = threading.currentThread()
line = self.rfile.readline()
while True:
line = self.rfile.readline()
if not line: break
print "%s wrote: %s" % (self.client_address[0], line.rstrip())
self.wfile.write(line)
return
class ThreadedEchoServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
########################################################################################
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, size=(1024,768))
self.SetIcon(wx.Icon('sim.ico', wx.BITMAP_TYPE_ICO))
self.SetBackgroundColour('#ece9d8')
self.add_toolbar()
self.Centre()
#Flag variables
self.isLogging = False
self.threads = []
server = ThreadedEchoServer(('localhost',9997), ThreadedEchoRequestHandler)
t = threading.Thread(target=server.serve_forever)
t.start()
#Create data buffers
#Some GUI Design Code
#Create timer to read incoming data and scroll plot
#Create start/stop button
self.start_stop_button = wx.Button(self, label="Start", pos=(80,550), size=(150,150))
self.start_stop_button.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False))
self.start_stop_button.Bind(wx.EVT_BUTTON, self.onStartStopButton)
def add_toolbar(self):
# Toolbar code
def onStartStopButton(self, event):
if not self.isLogging:
self.isLogging = True
self.start_stop_button.SetLabel("Stop")
call(["/home/user/Misc/socketTest/socketTest"])
else:
self.isLogging = False
self.start_stop_button.SetLabel("Start")
def GetSample(self, msg):
### Manipulate Data from socket for matplotlib update
if __name__ == '__main__':
app =wx.App(False)
frame = MyFrame(None, 'Sim')
frame.Show(True)
app.MainLoop()
Sorry but I am new to Python. Thank you in advance.