Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- To You Eli:
- 1. serialutils.py became like this on Linux:
- (haven't figured out conditional coding methods in Python yet)
- """
- Some serial port utilities for Linux and PySerial
- Port to Linux by Manni
- """
- import subprocess
- def full_port_name(portname):
- # No name lookup needed on *nix, keep function only for portability
- return portname
- def enumerate_serial_ports():
- p = subprocess.Popen('ls /dev/ttyS*', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
- l=[]
- for line in p.stdout.readlines():
- l.append(line.rstrip('\n'))
- retval = p.wait()
- return l
- if __name__ == "__main__":
- import serial
- for p in enumerate_serial_ports():
- print p, full_port_name(p)
- -------
- 2. sender_sim.py needs a different port name:
- port = "/dev/ttySx"
- --------
- 3. com_monitor.py needs 2 minor modifications to get the timestamps right.
- These modifcations might be runnable on Windows as well, I guess, please let us know.
- add a line somewhere (perhaps last) in def __init__(....):
- self.start_time=time.time() # Manni, for Linux
- and in def run(self):
- change timestamp = time.clock()
- into
- timestamp =time.time()-self.start_time # Manni, for Linux
- To clarify, here is my suggestion for a com_monitor.py in full again.
- (Look for the lines with Manni comments)
- import Queue
- import threading
- import time
- import serial
- class ComMonitorThread(threading.Thread):
- """ A thread for monitoring a COM port. The COM port is
- opened when the thread is started.
- data_q:
- Queue for received data. Items in the queue are
- (data, timestamp) pairs, where data is a binary
- string representing the received data, and timestamp
- is the time elapsed from the thread's start (in
- seconds).
- error_q:
- Queue for error messages. In particular, if the
- serial port fails to open for some reason, an error
- is placed into this queue.
- port:
- The COM port to open. Must be recognized by the
- system.
- port_baud/stopbits/parity:
- Serial communication parameters
- port_timeout:
- The timeout used for reading the COM port. If this
- value is low, the thread will return data in finer
- grained chunks, with more accurate timestamps, but
- it will also consume more CPU.
- """
- def __init__( self,
- data_q, error_q,
- port_num,
- port_baud,
- port_stopbits=serial.STOPBITS_ONE,
- port_parity=serial.PARITY_NONE,
- port_timeout=0.01):
- threading.Thread.__init__(self)
- self.serial_port = None
- self.serial_arg = dict( port=port_num,
- baudrate=port_baud,
- stopbits=port_stopbits,
- parity=port_parity,
- timeout=port_timeout)
- self.data_q = data_q
- self.error_q = error_q
- self.alive = threading.Event()
- self.alive.set()
- self.start_time=time.time() # Manni, for Linux
- def run(self):
- try:
- if self.serial_port:
- self.serial_port.close()
- self.serial_port = serial.Serial(**self.serial_arg)
- except serial.SerialException, e:
- self.error_q.put(e.message)
- return
- # Restart the clock
- time.clock()
- while self.alive.isSet():
- # Reading 1 byte, followed by whatever is left in the
- # read buffer, as suggested by the developer of
- # PySerial.
- #
- data = self.serial_port.read(1)
- data += self.serial_port.read(self.serial_port.inWaiting())
- if len(data) > 0:
- # timestamp = time.clock() # Windows
- timestamp =time.time()-self.start_time # Manni, for Linux
- self.data_q.put((data, timestamp))
- # clean up
- if self.serial_port:
- self.serial_port.close()
- def join(self, timeout=None):
- self.alive.clear()
- threading.Thread.join(self, timeout)
- ------------------------------------------------------------------------------------
- Instruction to any Linux user:
- # get socat, software package for serial port emulation/redirection
- # use your preferred available package manager, on ubuntu/debian:
- sudo apt-get install socat
- To run:
- 1. setup the virtual serial ports
- (change myuser to your actual user name to get access to the ports)
- sudo socat -d -d PTY,link=/dev/ttySx,user=myuser PTY,link=/dev/ttySy,user=myuser
- This creates two bidirectional virtual ports and creates symbolic links while the socat-process runs
- (Symbolic links makes them easier to refer than /dev/pts/x where x is unknown in advance.)
- Keep this process running as long as you run sendersim and dataplotting.
- 2. edit the data-sending script, sender_sim.py, set the sender portname to:
- port = "/dev/ttySx"
- and start the data-sending script:
- python sendersim.py
- 3. start the plotting:
- python plotting_data_monitor.pyw
- select COM port /dev/ttySy
- and start monitoring
- Setup becomes: sendersim.py --> /dev/ttySx --> socat ---> /dev/ttySy dataplotting.py
- ------------------------------------------------------------------------------------
- # References:
- # Emulating serial port communication on Linux
- # http://balau82.wordpress.com/2010/06/13/emulating-8051-serial-port-communication-on-linux/
- About the timestamping issue:
- Python time.clock() on Unices is only for benchmarking of code,
- you get used processor time which is not wall time.
- On windows wall time seems to be used for benchmarking purposes
- http://docs.python.org/library/time.html :
- time.clock()
- On Unix, return the current processor time as a floating point number
- expressed in seconds. The precision, and in fact the very definition of the
- meaning of processor time , depends on that of the C function of the same
- name, but in any case, this is the function to use for benchmarking Python or
- timing algorithms.
- On Windows, this function returns wall-clock seconds elapsed since the
- first call to this function, as a floating point number, based on the Win32
- function QueryPerformanceCounter(). The resolution is typically better than one
- microsecond.
- time.time()
- Return the time as a floating point number expressed in seconds since the
- epoch, in UTC. Note that even though the time is always returned as a floating
- point number, not all systems provide time with a better precision than 1
- second. While this function normally returns non-decreasing values, it can
- return a lower value than a previous call if the system clock has been set back
- between the two calls.
- Best regards,
- Manni
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement