1- Library
2- =======
3-
4- On the host, there are two ways to communicate with the device, either via the
5- API library, or via the command line.
6-
7-
8- API Library
9- -----------
10-
11- The API library provides the ``Interface `` class. A class instance is made by
12- passing the path to a serial device to the constructor.
13-
14- .. code :: python
15-
16- >> > from simple_rpc import Interface
17- >> >
18- >> > interface = Interface(' /dev/ttyACM0' )
19-
20- Every exported method will show up as a class method of the ``interface `` class
21- instance. These methods can be used like any normal class methods.
22- Alternatively, the exported methods can be called by name using the
23- ``call_method() `` function.
24-
25- The constructor takes the following parameters.
26-
27- .. list-table :: Constructor parameters.
28- :header-rows: 1
29-
30- * - name
31- - optional
32- - description
33- * - ``device ``
34- - no
35- - Serial device name.
36- * - ``baudrate ``
37- - yes
38- - Baud rate.
39- * - ``wait ``
40- - yes
41- - Time in seconds before communication starts.
42- * - ``autoconnect ``
43- - yes
44- - Automatically connect.
45-
46- The following standard methods are available.
47-
48- .. list-table :: Class methods.
49- :header-rows: 1
50-
51- * - name
52- - description
53- * - ``open() ``
54- - Connect to serial device.
55- * - ``close() ``
56- - Disconnect from serial device.
57- * - ``is_open() ``
58- - Query serial device state.
59- * - ``call_method() ``
60- - Execute a method.
61-
62- If the connection should not be made instantly, the ``autoconnect `` parameter
63- can be used in combination with the ``open() `` function.
64-
65- .. code :: python
66-
67- >> > interface = Interface(' /dev/ttyACM0' , autoconnect = False )
68- >> > # Do something.
69- >> > interface.open()
70-
71- The connection state can be queried using the ``is_open() `` function and it can
72- be closed using the ``close() `` function.
73-
74- .. code :: python
75-
76- >> > if interface.is_open():
77- >> > interface.close()
78-
79- Additionally, the ``with `` statement is supported for easy opening and closing.
80-
81- .. code :: python
82-
83- >> > with Interface(' /dev/ttyACM0' ) as interface:
84- >> > interface.version()
85-
86- The class instance has a public member variable named ``methods `` which
87- contains the definitions of the exported methods.
88-
89- .. code :: python
90-
91- >> > interface.methods.keys()
92- dict_keys([' inc' , ' set_led' ])
93- >> > interface.methods[' inc' ]
94- {
95- ' return' : {
96- ' doc' : ' a + 1.' ,
97- ' fmt' : ' <h' ,
98- ' typename' : ' int' },
99- ' doc' : ' Increment a value.' ,
100- ' name' : ' inc' ,
101- ' index' : 2 ,
102- ' parameters' : [
103- {
104- ' doc' : ' Value.' ,
105- ' name' : ' a' ,
106- ' fmt' : ' <h' ,
107- ' typename' : ' int'
108- }
109- ]
110- }
111-
112- Example
113- ^^^^^^^
114-
115- In our example we have exported the ``inc `` method, which is now present as a
116- class method of the ``interface `` class instance.
117-
118- .. code :: python
119-
120- >> > interface.inc(1 )
121- 2
122-
123- Alternatively, the exported method can be called using the ``call_mathod() ``
124- function.
125-
126- .. code :: python
127-
128- >> > interface.call_method(' inc' , 1 )
129- 2
130-
131- To get more information about this class method, the built-in ``help() ``
132- function can be used.
133-
134- .. code :: python
135-
136- >> > help (interface.inc)
137- Help on method inc:
138-
139- inc(a) method of simple_rpc.simple_rpc.Interface instance
140- Increment a value.
141-
142- :arg int a: Value.
143-
144- :returns int : a + 1 .
145-
146-
147- Command line interface
148- ----------------------
1+ Usage
2+ =====
1493
1504The command line interface can be useful for method discovery and testing
1515purposes. It currently has two subcommands: ``list ``, which shows a list of
@@ -156,8 +10,9 @@ the ``-h`` option.
15610
15711 simple_rpc -h
15812
13+
15914 Example
160- ^^^^^^^
15+ -------
16116
16217In our example, the ``list `` subcommand will show a description of the ``inc ``
16318method and the ``set_led `` method.
0 commit comments