problems using serial port

This is the place for queries that don't fit in any of the other categories.

problems using serial port

Postby eightbits » Thu Jul 11, 2013 2:50 am

Hello, I am experimenting with serial I/O on Ubuntu 12.04.
I am trying a very simple test code

Code: Select all
#!/usr/bin/python
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)

count = 0
msg='yyyy'                       #just test data string
print ser.portstr                #This  prints the correct data
while (count < 5):           
     print ser.write(msg)      #this outputs data from serial port as expected, but incorrect data.
     count = count + 1
 


""" This only transmits the number of char's in message ??? """

I know this is not a really good example for testing but it does seem to work as far as outputing char's .
It seems to only output the number of char's in the string. If I change the length of the string, the data
output will still output the number of char's.

The following is the output when the code is executing. I am using IDEL.
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
/dev/ttyUSB0
4
4
4
4
4
>>>
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am

Re: problems using serial port

Postby micseydel » Thu Jul 11, 2013 6:10 am

write(), like printf() in C and many low-level functions like them returns the number of bytes written. So, it's doing exactly as expected. You haven't indicated what you expected instead.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: problems using serial port

Postby tnknepp » Thu Jul 11, 2013 4:24 pm

I was tinkering with serial port transmission a while back too. Below are some lines I put together to log data coming from an ozone monitor and print each line to the screen. If I understand your goal correctly, you should be able to get what you want from this.

Code: Select all
# Open file to write to.
if os.path.isfile('O3_log.txt'):
    fid = open('O3_log.txt','a')
else:
    fid = open('O3_log.txt','w')
    fid.write('Ozone log using 2B Tech Instrument\n')
    fid.write('Ozone (ppb),Cell Temp. (C),Cell P (mbar), Date (dd/mm/yyyy HH:MM:SS UTC),'+
              'Time (UTC), Computer Time (mm/dd/yyyy HH:MM:SS UTC)\n')

print 'Collecting O3 data.'
ser = serial.Serial(cp-1,br,timeout=to)
while True:
    try:
        line = ser.readline()       
        fid.write(line[1:-1]+','+time.strftime('%m/%d/%Y %H:%M:%S',
                                                      time.gmtime(time.time()))+'\n')
        print (line[1:-1]+'\tComputer Time: '+time.strftime('%m/%d/%Y %H:%M:%S',
                                                            time.gmtime(time.time())))
       
    except KeyboardInterrupt: # Allows you to do a "cntrl c" to interrupt acquisition. 
        print 'Are you trying to quit? (y/n)'
        response = raw_input()
        if response == 'y' or response == 'yes':
            break
        else:
            continue

fid.close()
ser.close()
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 145
Joined: Mon Mar 11, 2013 7:41 pm

Re: problems using serial port

Postby eightbits » Thu Jul 11, 2013 9:43 pm

tnknepp Thanks for the information. I will try your code and respond in a few days.
I was surprised that it is that uncomplicated to access the serial (USB/RS-232) ports from a program.
I downloaded pyserial, is that the proper class file?

And thanks to micseydel for your post as well. That explains the len(gth) of the string as the data received. I was expecting the string to display. I was running the program in the terminal mode so I am thinking that
I need to route the data to a different serial port or device. Or perhaps just use a loop back?
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am

Re: problems using serial port

Postby tnknepp » Fri Jul 12, 2013 6:04 pm

Odd, I must not have copied all of my code. Sorry about that. This should go before my last post.

Code: Select all
import serial, os, time

# Specify baud rate.
br = 2400

# Specify timeout (s).
to = 15

# Specify com port number.
cp = 4


Yes, serial is the correct package.
You have to see what port is being used for communication, then specify it as above.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 145
Joined: Mon Mar 11, 2013 7:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests