Where The Streets Have No Name

GPS 데이터 가져오기 본문

Developement/GIS

GPS 데이터 가져오기

highheat 2008. 4. 30. 01:12
출처 : http://leewoosung.egloos.com/986759

# -*- coding: EUC-KR -*-
import serial    #시리얼 포트를 열기위해서 : serial.Serial(..)
import time    #시간을 알아내기 위해서    : time.ctime(time.time())

#현재 시리얼 포트를 엽니다.
s_com4 = serial.Serial(port = 3, baudrate = 4800, timeout = 1)    

#데이터를 저장할 파일입니다.
dst_file = "c:\\gps_message.txt"    

try:    #파일에 추가로 열기를 시도해봅니다.
    save_file = open(dst_file, 'a')    
except:    #기존에 파일이 없었다면 쓰기로 엽니다. (새로 하나 만듭니다.)
    save_file = open(dst_file, 'w')

while (1):    #무한반복입니다.
    #현재 시간을 [요일 월 일 시:분:초 연]의 형식으로 알아냅니다.
    current_time = time.ctime(time.time())

    #시리얼포트로부터의 메시지를 하나씩 받아들입니다.
    strgps = s_com4.readline()    

    #현재 시간과 GPS 메시지를 합칩니다.
    save_str = current_time + ' ' + strgps

    #합쳐진 메시지를 파일에 저장합니다.
    save_file.write(save_str)