GNS.py

Aus Wiki
Wechseln zu: Navigation, Suche

A Python "driver" for the GNS FM9, a RDS/TMC-Receiver, attachable via serial RS-232 port (/dev/ttyUSB0 below).

Published as Free Software, licensed as GNU GPL v2.

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
 
import serial
from time import sleep
import threading
import sys
 
Privat = [	'privat',
		'privat/öffentlich-rechtlich'	]
 
Reichweite = [	'lokal',
		'international',
		'national',
		'supranational',
		'regional'	]
 
Bundesland = [	'Baden-Württemberg',
		'Bayern',
		'Berlin',
		'Brandenburg',
		'Bremen und Bremerhaven',
		'Hamburg',
		'Hessen',
		'Mecklenburg-Vorpommern',
		'Niedersachsen',
		'Nordrhein-Westfalen',
		'Rheinland-Pfalz',
		'Saarland',
		'Sachsen',
		'Sachsen-Anhalt',
		'Schleswig-Holstein',
		'Thürigen'
		]
 
def Hex(i):
	h = hex(i)[2:]
	while len(h) < 2:
		h = '0'+h
	return h
 
class PI_code:
	def __init__(self, bytes):
		self.code = Hex(ord(bytes[0]))+Hex(ord(bytes[1]))
		if self.code[0] == '1':
			self.private = Privat[0]		# erste Ziffer
		else:
			self.private = Privat[1]
		r = int(self.code[1], 16)
		if r < 4:
			self.range = Reichweite[r]		# zweite Ziffer
		else:
			self.range = Reichweite[4]
		self.country = Bundesland[int(self.code[2], 16)] # dritte Ziffer
 
		print self.code
		print self.private
		print self.range
		print self.country
 
class RDS_message:
	def __init__(self, bytes):
		self.PI_code = PI_code(bytes[0]+bytes[1])
 
def hex2string(hexstring):
	result = ''
	hexstring = hexstring.replace(' ','')
	for i in range(len(hexstring)/2):
		result += chr(int(hexstring[i*2]+hexstring[i*2+1], 16))
	return result
 
class GNS_RDS_TMC_receiver:
	def __init__(self, port):
		self.port = serial.Serial(port, 38400, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
		self.receiver_thread = threading.Thread(target=self.reader)
		self.receiver_thread.setDaemon(True)
		self.receiver_thread.start()
		self.reset()
 
	def poweroff(self):
		self.port.setRTS(0)
 
	def poweron(self):
		self.port.setRTS(1)
 
	def reset(self):
		self.poweroff()
		self.buffer = ''
		sleep(0.01)
		self.poweron()
 
	def reader(self):
		while True:
			byte = self.port.read(1)
			sys.stdout.write(byte)
			if ord(byte) == 0x3F:
				if len(self.buffer) > 3:
					if self.buffer[0:3] == hex2string('00 00 56'):
						print "RDS enabled"
					elif self.buffer[0:3] == hex2string('00 00 73') or self.buffer[0:3] == hex2string('00 00 66'): # manual tuning or scan
						freq = ord(self.buffer[3])*0.1+87.5
						print "tuned to %.1f MHz" % freq
						if self.buffer[4:6] == hex2string('01 55'):
							print 'RDS signal found'
						else:
							print 'no RDS signal'
					elif self.buffer == 'GNS GmbH':
						print 'GNS GmbH'
					elif self.buffer[0:6] == hex2string('00 00 00 45 72 72'):
						print 'Device says: Error'
					elif len(self.buffer) == 8: # RDS message
						print 'RDS message'
						msg = RDS_message(self.buffer)
				self.buffer = ''
			else:
				self.buffer += byte
 
	def write(self, hexstring):
		self.port.write(hex2string(hexstring))
 
	def enable(self):
		self.write('FF 56 78 78 56')
 
	def disable(self):
		self.write('FF 53 78 78 53')
 
	def setMHz(self, freq):
		aa = Hex(int(round(freq-87.5, 1)/0.1))
		self.write('FF 73 '+aa+' 05 73')
 
	def scan(self):
		self.write('FF 59 00 01 59')
 
	def request_GNS_GmbH(self):
		self.write('FF 43 00 00 43')
 
if __name__ == '__main__':
	dongle = GNS_RDS_TMC_receiver('/dev/ttyUSB0')
	print "Enable ..."
	dongle.enable()
	sleep(0.5)
	dongle.request_GNS_GmbH()
	sleep(0.5)
#	print "Tuning to 88.8 MHz..."
#	dongle.setMHz(88.8)
#	sleep(5)
	print "Scanning ..."
	dongle.scan()
	sleep(60)
Meine Werkzeuge
Namensräume
Varianten
Aktionen
Navigation
Werkzeuge