LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Craeting a serial program with Python (https://www.linuxquestions.org/questions/programming-9/craeting-a-serial-program-with-python-694084/)

jabfinger 12-31-2008 11:40 AM

Craeting a serial program with Python
 
Hello,

Its been a while since I have posted and today I am posting about
some problems I am having with a program I am attempting to Create
with Python. As you can see it will be running on a windows platform
(2000). What it is supposed to do is capture data over a serial line from
a WYSE 50 terminal when the WYSE Shift + Print keys are pressed,
put that data into a file, and finally open the file with notepad to make
edits and print the data. The WYSE terminal is set up to send data over
the AUX port at 9600 baud. If using Procom Plus to perform this function,
it (Pro Com) is set 9600,8,none,1 and works flawlessly. I have been testing my program using a garmin gps with a serial out and all seems fine using the gps device, but when connected to the WYSE the data is all Jibberishy with smiley faces. Some data does appear but not as it should. I am using pyserial, in the program so I got a copy of Chris Liechti's miniterm.py program, tried it, and it works fine for viewing the serial data from the WYSE.

Maybe you guys could look over my code and see where I have gone astray. I have been all over the web reading and have not been able to come up with a solution. I am a beginer at progrmaming so please be gentel.

Code:

Code:

#!/usr/bin/python

#_____________________import necessary modules__________________________

import sys, os, serial, msvcrt

print "Welcom to the GPS Weekly Program. \n\n\n\n"
print "Data collection begins upon entering your name in this terminal. \n"
print "After you have entered your name you may use the Shift + print \n"
print "keys on the WYSE terminal as normal to collect data. \n\n "
print "After you have collected the weekly data press the \"d\" key, \n"
print "pressing any other key during data collection will exit the program\n"
print "imediatly and the collection program will have to be restarted.\n\n\n"
name = raw_input('Enter your name>>>')
os.system('cls')


gpslog = open('GPS-9_Log.txt', 'w')
gpslog.write('GPS-9 Weekly Performed by ' + name + '\n \n')
gpslog.close()

def write():
    s = serial.Serial(port=0, parity=serial.PARITY_NONE, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE, timeout=3, baudrate=9600)
#_________________________Write data to GPS-9.txt______________________
    s.flushInput()
    s.flushOutput()
    gpsdata = s.readline()
    gpslog = open('GPS-9_Log.txt', 'a')
    gpslog.write(gpsdata)
    print gpsdata       
    gpslog.close()
    s.close()
       
   
#__________________Loop write() until "d" is pressed____________________
while not msvcrt.kbhit():
    write()

#_________Stop the loop clear the screen and pop open the text editor__
if msvcrt.getch() == 'd':
    os.system('cls')
    print "Data collection is now complete.\n\n\n"
    input = raw_input('Press Enter to continue>>>')
os.system('start ' + 'GPS-9_Log.txt')


Thanks guys.


All times are GMT -5. The time now is 07:48 AM.