質問編集履歴

1

プログラムコードを追加

2020/02/19 04:07

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -113,3 +113,75 @@
113
113
  main()
114
114
 
115
115
  ```
116
+
117
+
118
+
119
+ ##シリアル通信確認プログラム
120
+
121
+ ```python
122
+
123
+ import serial
124
+
125
+ import time
126
+
127
+ import threading
128
+
129
+ import Queue
130
+
131
+
132
+
133
+ class SerCom:
134
+
135
+ def __init__(self, tty, baud='9600'):
136
+
137
+ self.ser = serial.Serial(tty, baud, timeout=0.1)
138
+
139
+ self.queue = Queue.Queue()
140
+
141
+
142
+
143
+ self.event = threading.Event()
144
+
145
+ self.thread_r = threading.Thread(target=self.recv_)
146
+
147
+ self.thread_r.start()
148
+
149
+
150
+
151
+ def recv_(self):
152
+
153
+ while not self.event.is_set():
154
+
155
+ line = self.ser.readline()
156
+
157
+ if len(line) > 0:
158
+
159
+ print(line)
160
+
161
+ self.queue.put(line)
162
+
163
+
164
+
165
+ def send(self, data):
166
+
167
+ self.ser.write(data)
168
+
169
+
170
+
171
+ def stop(self):
172
+
173
+ self.event.set()
174
+
175
+ self.thread_r.join()
176
+
177
+
178
+
179
+ ser = SerCom('/dev/ttyS0', '9600')
180
+
181
+ #ser.send('test')
182
+
183
+ time.sleep(2)
184
+
185
+ ser.stop()
186
+
187
+ ```