RSS Feed

RSS

Comments RSS

Serial Communications using Python

#k2test1.py  by Bill Allen – WA5PB
#This is an experiment in communicating with an Amateur Radio transceiver
#via Python through RS232 serial communications.  Similar code could be used
#to communicate with any device supporting serial communications.

#This was written using Python 2.6.5, but I like using the new
#Python 3.x print function so we pull it in for use here.
from __future__ import print_function

#We will be using the PySerial library to provide RS232 serial communications.
#PySerial is a third party library module that can be found here:
#http://pyserial.sourceforge.net/
import serial

#Configure the serial port to communicate with the Elecraft K2 xcvr.
#I am coding this on a Linux system, so the device string /dev/ttyS0
#works for the first serial port.  Your system my use a different serial
#port device.  For a Windows system, try COM1 for the first serial port.
ser = serial.Serial(‘/dev/ttyS0’, 4800, timeout=1)

#Sanity check for the K2 AutoInfo mode, turn it off.
ser.write(“AI0;”)

#Now set the K2 AutoInfo mode to mode 1 and get some initial data.
ser.write(“AI1;”)

#Read up to 150 bytes of returned data from the K2 and print it out.
K2_data = ser.read(150)
print(K2_data)

#Now set the K2 AutoInfo mode to return the most data while it is being polled.
#Ctrl-C will be used to end the program.  End the program gracefully by trapping
#the Ctrl-C, turning off the K2 AutoInfo mode and closing the serial port prior
#to exiting.
ser.write(“AI3;”)
while True:
    try:
        K2_data = ser.read(150)
        if K2_data != “”:
            print(K2_data)
    except KeyboardInterrupt:
        print(“\nOK, exiting…”)
        print(“Reseting K2 AutoInfo mode OFF”)
        ser.write(“AI0;”)
        print(“Closing Serial Port”)
        ser.close()
        raise SystemExit(0)

electronics experimentation

There are lots of ways to experiment with electronics.  One way is to use a programmable testbed system.  There are several available at various costs.  One that I am looking at that seems interesting is the Arduino UNO, which sells for about $30.   I was shown this one at work by our electronics engineer.   I may get one for myself to play with.  It looks like a very inexpensive way to experiment with programmable electronics.  Such system can be used for data output and display, data collection, motor control, ham radio applications, and much more.

The main website.
http://www.arduino.cc/

A hands on turtorial.
http://www.ladyada.net/learn/arduino/

Another programmable testbed system that is quite popular is the Parallax Basic Stamp.
http://www.parallax.com/tabid/295/Default.aspx

The Arduino UNO

computer sound cards, so many uses!

The PC sound card may just be one of the most useful electronic devices that nearly everyone owns.   Not only can you listen to music with it, but you can do nearly any Ham Radio digital mode.   However, did you know you can also use your PC+sound card combination as a spectrum analyzer or even as an Oscilloscope?   Here are links to software that will allow you do this.  Both are easy to install and free.

Spectrogram 5.17 – for Windows.   I did all the xtal alignment work on my Elecraft K2 radio with this program.

Baudline – for Linux, Mac OS X, and Solaris SPARC systems.   This one has LOTS of features.  Simple to install!  Download, unzip, and run the “baudline” program from the unziped folder.

73,
Bill – WA5PB