Home > code, Uncategorized > using pyserial

using pyserial

import sys
import glob
import serial
import time


def serial_ports():
    if sys.platform.startswith('win'):
        ports = ['COM%s' % (i + 1) for i in range(256)]
    elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
        # this excludes your current terminal "/dev/tty"
        ports = glob.glob('/dev/tty[A-Za-z]*')
    elif sys.platform.startswith('darwin'):
        ports = glob.glob('/dev/tty.*')
    else:
        raise EnvironmentError('Unsupported platform')

    result = []
    for port in ports:
        try:
            s = serial.Serial(port)
            s.close()
            result.append(port)
        except (OSError, serial.SerialException):
            pass
    return result


def process(p):
    ser = serial.Serial(port=p,
                        baudrate=9600,
                        parity=serial.PARITY_EVEN,
                        stopbits=serial.STOPBITS_TWO,
                        bytesize=serial.EIGHTBITS)

    for i in range(100):
        ser.write('A')
        time.sleep(0.1);

        out = ''
        while ser.inWaiting() > 0:
            out += ser.read(1)

        if out != '':
            print ">>" + out

    ser.close()


if __name__ == '__main__':
    port = (serial_ports())[0]
    process(port)
Advertisements
Categories: code, Uncategorized
  1. April 8, 2016 at 20:27

    Guys guys, I welcome the comments, but there’s a whole blog to explore out there besides this one post I wrote over a month ago (which a disgruntled S Click https://twitter.com/moooker1

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: