Browse code

initial commit 20191113

Gandolf authored on 11/13/2019 20:52:19
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,151 @@
1
+#!/usr/bin/python -u
2
+# The -u option turns off block buffering of python output. This assures
3
+# that output streams to stdout when output happens.
4
+#
5
+# Description:  This program passes commands from a command line prompt
6
+#               directly through to the FT991.  Also a few utility macro
7
+#               commands have been defined that can be typed on the 
8
+#               command line.
9
+#
10
+# This script has been tested with the following
11
+#
12
+#     Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
13
+#     [GCC 7.3.0] on linux2
14
+#2345678901234567890123456789012345678901234567890123456789012345678901234567890
15
+
16
+import sys, serial, time
17
+
18
+# Determine OS type and set device port accordingly.
19
+OS_type = sys.platform
20
+if 'WIN' in OS_type.upper():
21
+    _SERIAL_PORT = 'COM5'
22
+else:
23
+    _SERIAL_PORT = '/dev/ttyUSB0'
24
+
25
+# General constant defines
26
+_BAUD_RATE = 9600
27
+_INTERFACE_TIMEOUT = 0.1 # seconds
28
+_SERIAL_READ_TIMEOUT = 0.1 # seconds
29
+_SERIAL_READ_BUFFER_LENGTH = 1024 # characters
30
+
31
+# Define functions encapsulating serial communications to and from
32
+# a generic serial device object.
33
+
34
+def getAnswer(device, termchar):
35
+    """
36
+    Description: Reads output one character at a time from the device
37
+                 until a terminating character is received.  Returns a     
38
+                 string containing the characters read from the serial
39
+                 port.
40
+    Parameters:  device - a serial object connected to the device port
41
+                 termchar - character terminating the answer string
42
+    Returns:     string
43
+    """
44
+    answer = '' # initialize answer string to empty string
45
+    charCount = 0  # reset read character count to zero
46
+
47
+    while True:
48
+        startTime = time.time() # Start read character timer
49
+        c =''
50
+        while True:
51
+            # Check for a character available in the serial read buffer.
52
+            if device.in_waiting:
53
+                c = device.read()
54
+                break
55
+            # Timeout if a character does not become available.
56
+            if time.time() - startTime > _SERIAL_READ_TIMEOUT:
57
+                break # Read character timer has timed out.
58
+        # Return empty string if a character has not become available.
59
+        if c == '':
60
+            break;
61
+        answer += c # Form a string from the read characters.
62
+        charCount += 1 # Increment character count.
63
+        # If a semicolon has arrived then the FT991 has completed
64
+        # sending output to the serial port so stop reading characters.
65
+        # Also stop if max characters received. 
66
+        if c == termchar:
67
+            break
68
+        if charCount > _SERIAL_READ_BUFFER_LENGTH:
69
+            raise Exception('serial read buffer overflow')
70
+    device.flushInput() # Flush serial buffer to prevent overflows.
71
+    return answer           
72
+## end def
73
+
74
+def sendCommand(device, command):
75
+    """
76
+    Description: writes a string to the device.
77
+    Parameters:  device - a serial object connected to the device port
78
+                 command - string containing the FT991 command
79
+    Returns:     nothing
80
+    """
81
+    device.write(command) # Send command string to FT991
82
+    device.flushOutput() # Flush serial buffer to prevent overflows
83
+
84
+# Iterate through all menu items, getting each setting
85
+def readMenuSettings(device):
86
+    """
87
+    Description: prints out all menu settings.
88
+    Parameters:  device - a serial object connected to the FT991
89
+    Returns:     nothing
90
+    """
91
+    for inx in range(1,125):
92
+        sCommand = 'EX%0.3d;' % inx
93
+        sendCommand(device, sCommand)
94
+        sAnswer = getAnswer(device, ';')
95
+        print "%0.3d: %s" % (inx, sAnswer[5:])
96
+
97
+# Iterate through all memory channels, getting settings
98
+def readMemoryChannels(device):
99
+    """
100
+    Description: prints out all memory channels.
101
+    Parameters:  device - a serial object connected to the FT991
102
+    Returns:     nothing
103
+    """
104
+    for inx in range(1,118):
105
+        sCommand = 'MT%0.3d;' % inx
106
+        sendCommand(device, sCommand)
107
+        sAnswer = getAnswer(device, ';')
108
+        print "%0.3d: %s" % (inx, sAnswer)
109
+
110
+def main():
111
+    """
112
+    Description: main routine starts command line interpreter.
113
+    Parameters:  none
114
+    Returns:     nothing
115
+    """
116
+    
117
+    # Present an introductory splash when the program first starts up.
118
+    splash = \
119
+"""
120
+Enter an FT991 command, e.g, "IF;";
121
+or enter one of the following commands
122
+    exit - terminates this program
123
+    rmem - prints out memory channels
124
+    rmenu - prints out all menu settings
125
+"""
126
+    print splash
127
+
128
+    # Create a FT991 object for serial communication
129
+    ft991 = serial.Serial(_SERIAL_PORT, _BAUD_RATE, timeout=_INTERFACE_TIMEOUT)
130
+    time.sleep(.1) # give the connection a moment to settle
131
+
132
+    while(True):
133
+        sCommand = raw_input('>')
134
+        # Process command string
135
+        if sCommand == '': # no command - do nothing
136
+            continue
137
+        elif sCommand.upper() == 'EXIT': # end program
138
+            break
139
+        elif sCommand.upper() == 'RMEM': # display channel memory
140
+            readMemoryChannels(ft991)
141
+        elif sCommand.upper() == 'RMENU': # display menu settings
142
+            readMenuSettings(ft991)
143
+        else: # run a user command
144
+            sendCommand(ft991, sCommand)
145
+            sAnswer = getAnswer(ft991, ';');
146
+            if sAnswer != '':
147
+                print sAnswer
148
+## end main
149
+
150
+if __name__ == '__main__':
151
+    main()