Page 1 of 1

TCP server

Posted: Wed Jan 30, 2013 8:25 pm
by pl259
i am trying to run this code to take data from an r232->usb cable on USB0 and send it to a TCP server host. I have downloaded and configured this script:

Code: Select all

 #!/usr/bin/python           
# Xaver's TCP server. Runs on the Raspberry Pi.

import socket               # Import socket module
s = socket.socket()         # Create a socket object
host = '75.144.181.xxx'     # Get local machine name
port = 109xx               # Reserve a port for your service.

import serial

DEVICE = '/dev/ttyUSB0' # the arduino serial interface (use dmesg when connecting)
BAUD = 38400
ser = serial.Serial(DEVICE, BAUD)

print 'Server started!'
print 'Waiting for clients...'

s.bind((host, port))        # Bind to the port
s.listen(5)                 # Now wait for client connection.
c, addr = s.accept()     # Establish connection with client.
print 'Got connection from', addr
while True:
   msg = c.recv(2) # get 2 bytes from the TCP connection
   ser.write(msg)  # write the 2 bytes to the serial interface
and get this error:

Code: Select all

 pi@raspberrypi ~/Desktop $ sudo python tcpserv
Server started!
Waiting for clients...
Traceback (most recent call last):
  File "tcpserv", line 18, in <module>
    s.bind((host, port))        # Bind to the port
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
i know the host has an available port on that ip. Thanks in advance,

Re: TCP server

Posted: Wed Jan 30, 2013 8:45 pm
by rurwin
That code takes data from the TCP connection and sends it to the serial -- the opposite of how you described it. The way you use the word "host" indicates you may be confused about the meaning of the IP address you store in the "host" variable. This is the local host, ie the computer running this code. It is not the remote host, ie the computer that is sending the data to the computer running this code. The "host" variable should be set to the same IP address that is displayed when you give the "ifconfig" command. Unless you are on a company or college network, it is probably 192.168.xx.xx. It is not the address of the machine you want to talk to, and it is not the public address on the other side of your router.

Re: TCP server

Posted: Wed Jan 30, 2013 8:54 pm
by pl259
Okay are there any ways to do what i want? I am trying to take data from usb port on my pi and send it to a server using tcp.

Re: TCP server

Posted: Wed Jan 30, 2013 9:11 pm
by -rst-
Definitely best to break the problem (at least) to two sub-problems:
1. read serial data from usb (to begin with, just dump the data to the console with print (or to a file))
2. serve data over tcp (to begin with, server some static stuff from hardcoded strings)

That code there should actually implement most of it anyway - just switch it to: read from serial and write to socket?

And you did put in some proper values for the host and port (instead of those xxx's), didn't you?? Like host = '127.0.0.1' (or the proper ip address of your RPi) and port = 10912 (for example, best to check the available/non-reserved ports...).

Re: TCP server

Posted: Wed Jan 30, 2013 9:49 pm
by rurwin
What you have there is the server code. The site you copied it from should have the corresponding client code. That is what you want.

But you do realise that you will need some code (this code maybe) on the server too?

Re: TCP server

Posted: Thu Jan 31, 2013 11:36 am
by -rst-
To me it is not quite that clear what the OP wants to do...

Is it:

1. An application that reads data from serial and allows TCP clients to connect for reading the data ...so the application implements a server, for which the current code would do with switching from 'read from tcl, write to ser' to 'read from ser - write to tcp'

or

2. An application that reads data from serial and connects to a TCP server (maybe something like COSM.com) to 'post' the data ...in which case the application would be sort of a client, but compared to a client that would use the server implemented with the existing code, it would need to be 'turned inside out' = the client reads the serial (not the server)... :roll:

Re: TCP server

Posted: Thu Jan 31, 2013 11:45 am
by rurwin
-rst-, the way to think about it is that the client makes the connection to the server. The server just waits for connections to be made. After the two are connected data can flow in all sorts of directions.

Re: TCP server

Posted: Thu Jan 31, 2013 12:06 pm
by -rst-
Yes, rurwin - I do know that. Good clarification though.

But as far as I understand the OP's requirement, he wants to read the serial 'locally' and most likely create a 'local' client that sends the data to a (possibly already existing) 'remote' server - not a server that reads the serial, so the existing code is not usable as is...

Re: TCP server

Posted: Thu Jan 31, 2013 12:19 pm
by -rst-
Just to clarify, to me it seems there are two possible scenarios here - not exactly sure which one the OP wants to implement.


1. some_device ->> Serial/USB ->> RasPi_application <<- TCP <<- some_other_application ...so the application running on RasPi would be a server - the client app connects to RasPi to 'ask for' the serial data

or

2. some_device -> Serial/USB -> RasPi_application -> TCP -> some_other_application ...so the application running on RasPi would be a client that connects to the other server to 'store' the data there ...hmm, except if the RasPi_application was implementing a server PUSH... argh, I've always had mixed feeling about 'conceptual design' :?

Re: TCP server

Posted: Thu Jan 31, 2013 2:51 pm
by pl259
option 2
. some_device -> Serial/USB -> RasPi_application -> TCP -> some_other_application ...so the application running on RasPi would be a client that connects to the other server to 'store' the data the
.

I currently use lantronix boxes which collect the data via serial and then push it to the server via a tcp server. This has to be the case because i send to these to remote locations where i do not know the ip before hand and most of the time the boxes are assigned DHCP. Lantronix calls it "autostart" TCP server.

Thanks for all the great responses. I have learned so much about linux in the past two days my head is going pop.