jay123035
Posts: 32
Joined: Wed Nov 21, 2012 5:24 am

GPSd TCP socket test loop error

Wed May 29, 2013 5:43 am

first off thanks

i am new to programming and am trying to learn from the deep end.

my goal (at present): to have a loop that checks if GPSd is running on socket 2947 is so it prints 2 lines then sleeps for 30sec (30min) and checks again. if the Socket is "closed" it runs:

Code: Select all

 sudo killall gpsd
(to be sure & safe) then

Code: Select all

 sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock
and checks again in 10sec.

what i have pieced together:

Code: Select all

import socket
import os
import time

#Simply change the host and port values
host = 'localhost'
port = 2947

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
 s.connect((host, port))
 s.shutdown(2)
 print "Success connecting to "
 print host + " on port: " + str(port)
# os.system("cgps -s")
 time.sleep (30)
else:
 print "Cannot connect to "
 os.system("sudo killall gpsd")
 os.system("sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock")
 print host + " on port: " + str(port)
 time.sleep (10)
 
what i get:

Code: Select all

webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/ckeckfor_GPSd $ sudo python socketlooptest.py                                                                  
Success connecting to                                                                                                                                                                    
localhost on port: 2947                                                                                                                                                                  
Traceback (most recent call last):                                                                                                                                                       
  File "socketlooptest.py", line 11, in <module>                                                                                                                                         
    s.connect((host, port))                                                                                                                                                              
  File "/usr/lib/python2.7/socket.py", line 224, in meth                                                                                                                                 
    return getattr(self._sock,name)(*args)                                                                                                                                               
socket.error: [Errno 106] Transport endpoint is already connected                                                                                                                        
webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/ckeckfor_GPSd $
i do not even know were to start with this. any help would be appreciated. thanks again.

User avatar
DaveDriesen
Posts: 113
Joined: Sun Mar 31, 2013 8:28 pm
Location: Top of the food chain
Contact: Website

Re: GPSd TCP socket test loop error

Wed May 29, 2013 6:54 am

Hi -- You have a "while 1" and then an "else"..

the code in your "else" statement will never be executed.

Is that as intended?

I may be missing something..

Dave Driesen
Linux dev and oldskool elite

jay123035
Posts: 32
Joined: Wed Nov 21, 2012 5:24 am

Re: GPSd TCP socket test loop error

Wed May 29, 2013 2:24 pm

no your not missing anything. i am just that novice.

can you point me to what i should replace so that the lower section of code is run if the socket is NOT open? Maybe try?


thanks

jay123035
Posts: 32
Joined: Wed Nov 21, 2012 5:24 am

Re: GPSd TCP socket test loop error

Wed May 29, 2013 6:59 pm

Code: Select all

import socket
import os
import time

#Simply change the host and port values
host = 'localhost'
port = 2947

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
 try:
  s.connect((host, port))
  s.shutdown(2)
  print "Success connecting to "
  print host + " on port: " + str(port)
 #os.system("cgps -s")
  time.sleep(30)
 except:
  print "Cannot connect to "
  os.system("sudo killall gpsd")
  os.system("sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock")
  print host + " on port: " + str(port)
  os.system("sudo killall gpsd")
  os.system("sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock")
  time.sleep(20)
so this new loop seams to work. at least the first time. but it can never connect a second+ time. i think i need to disconnect from the socket (because when i kill the loop and restart it the first try connects)

Code: Select all

webide@raspberrypi /usr/share/adafruit/webide/repositories/my-pi-projects/ckeckfor_GPSd $ sudo python socketlooptest.py  
Success connecting to                                                                                                    
localhost on port: 2947                                                                                                  
Cannot connect to                                                                                                        
localhost on port: 2947                                                                                                  
Cannot connect to                                                                                                        
localhost on port: 2947                                                                                                  
Cannot connect to                                                                                                        
localhost on port: 2947                                                                                                  
Cannot connect to                                                                                                        
localhost on port: 2947 
any ideas.

Return to “Python”