TCP/IP <=> Serial Gateway

Discussion in 'General Programming Discussion' started by borism, Oct 25, 2014.

  1. borism

    borism New Member

    Joined:
    Oct 21, 2014
    Messages:
    4
    Likes Received:
    0
    I would like to comunicate with arduino via TCP simple command like (GO, STOP, LEFT, RIGHT, etc ... ). I would also like to receive from arduino messages from sensor values.

    I have server.py - gateway TCP => ARDUINO => TCP
    Code:
    import socket
    import serial
    import time
    
    def Main():
    	host = '127.0.0.1'
    	port = 5000
    	arduino = serial.Serial('/dev/ttymxc3',9600)
    
    	s = socket.socket()
    	s.bind((host,port))
    
    	s.listen(1)
    
    	c, addr = s.accept()
    	print 'CONN:  ' + str(addr)
    
    	while True:
    		data = c.recv(1024)
    		if not data:
    				break
    		print "From user " +str(data)
    		print "SEND ARDU: " +str(data)
    		arduino.writeline(str(data))
    	        time.sleep(5);
    		print "READLN ARDU: "
    		data = arduino.readline();
    		print "sending " + str(data)
    		c.send(data)
    	c.close()
    if __name__== '__main__':
    	Main()
    I have client.py to send commands and receive messages
    Code:
    import socket
    
    def Main():
    	host = '127.0.0.1'
    	port = 5000
    
    	s = socket.socket()
    	s.connect((host,port))
    	
    	message = raw_input("=>")
    	while message != "q":
    		s.send(message)
    		data = s.recv(1024)
    		print "Received : " + str(data)
    		message = raw_input("=>")
    	s.close()
    if __name__ == '__main__':
    	Main()
    I have serial.ino arduino serial request / response
    Code:
    String readString;
    
    void setup(){
    
     Serial.begin(9600);
     Serial.write("Robot is powering On");
    }
    
    
    void loop()
    {
     while (Serial.available()) {
        delay(3);  //delay to allow buffer to fill 
        if (Serial.available() >0) {
          char c = Serial.read();  //gets one byte from serial buffer
          readString += c; //makes the string readString
        } 
      }
    
    if (readString.length() >0) {
          Serial.println("Dobil:"+readString); //see what was received
    }
    }   
    
    I'm having trouble sending data from phyton to arduino (server frezes). What am i doing wrong ?

    Regards,
    Boris
     
  2. dnulnets

    dnulnets New Member

    Joined:
    Jun 9, 2013
    Messages:
    7
    Likes Received:
    0
    Hi,

    I'm sorry but I am no python guy and cannot answer your question. But I had the need for something similar, i.e. to be able to communicate with the arduino from a remote PC via TCP/IP. So maybe this is another approach that you could try.

    I used the ser2net daemon which allows you to make a serial port available over tcp/ip on the UDOO. Then I could connect to the arduino serial port ttymxc3 via telnet from a remote computer or via sockets. It works like a charm.

    sudo apt-get install ser2net

    add to /etc/ser2net.conf

    192.168.1.75,2000:raw:0:/dev/ttymxc3:115200 8DATABITS NONE 1STOPBIT banner

    replace with your own values.

    /etc/init.d/ser2net start

    and from your remote just telnet to the ip adress and port. Then it is like you were sitting directly on the arduino serial port.

    Maybe it doesn't answer your needs, but It is anotherway of doing it. Remeber to stop the daemon if you want to program the arduino from the IDE.

    Good luck!
     
  3. ekirei

    ekirei Administrator

    Joined:
    Jun 14, 2013
    Messages:
    78
    Likes Received:
    3

    I'm not so good in python too but the codes seem right.
    Did you upload your arduino sketch from the internal IDE of UDOO? 'cause i suggest to use simple byte or char instead of the "String" object of Arduino that sometimes causes strange communication issues if the sketch is compiled from the internal IDE.
     

Share This Page