#gps_recorder_serial_splitter.py
#Copyright (C) 2006  James R Glasgow
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.# serial_splitter.py
#

import serial, sys
from time import time, localtime, strftime
output_data = ""
#ser = serial.Serial(18,4800,timeout=1)
def create_serial_connection(com_num, baud):
    #print 'opening port - %s' % com_num
    #print 'serial.Serial(%s, %s, timeout=1)' % (com_num, baud)
    try:
        connection = serial.Serial(com_num,baud,timeout=1)
        serial_connections.append(connection)
    except:
        print ''
        print ''
        print "Unexpected error:", sys.exc_info()[0]
        print 'Cannot open port %s' % com_num
        print 'continuing...' 
    

if len(sys.argv) >= 5:
    print 
    serial_connections = []
    baud = int(sys.argv[1])
    com_num = sys.argv[2]
    try:
        com_num = int(com_num)
    except ValueError:
        pass
    
    print 'You are using serial_splitter.py to quit press ctrl+x'
    try:
        serial_source = serial.Serial(com_num,baud, timeout=1)
    except:
        print 'cannot connect to source port: %s' % com_num
        print 'quitting program'
        quit
    for com_num in sys.argv[3:]:
        #print com_num
        try:
            com_num = int(com_num)
        except ValueError:
            pass    
        create_serial_connection(com_num, baud)
    #print serial_connections
    if len(serial_connections)< 2:
        print 'you do not have enough valid destination ports making this script useless.'
    else:
        time_string = strftime('%Y.%m.%d.%H.%M.%S',localtime())
        log_file_name = 'c:/gps_data/' + time_string + '_gps_data.csv'
        log_file = file(log_file_name,'a+')
        try:
            while True:
                
                line = serial_source.readline()
                print line
                
                log_file.write(line)
                
                for serial_port in serial_connections:
                    serial_port.write(line)
                    
        except KeyboardInterrupt:
            
            
            
            log_file.close()
            print ''
            print ''
            print ''
            print ''
            print ''
            print ''
            print 'thank you for using serial splitter by james R Glasgow.'
            print 'all data from this session has been written to \n %s' % log_file_name
    
else:
    print 'Usage: \n%s (baud) (com src(int)) (com dest(int)) (com dest(int)) ... Multiple destinations can be used.' % sys.argv[0]

