擅长:python、mysql、java
<p>如果要在程序进入while循环后动态设置等待时间,我建议在串行RX中断中附加一个回调函数。你知道吗</p>
<pre class="lang-cpp prettyprint-override"><code>RawSerial pc(USBTX, USBRX);
void callback() {
c = float(pc.getc());
}
</code></pre>
<p><code>Serial</code>使用互斥,不能在mbed OS5上的ISR中使用。改用<code>RawSerial</code>。你知道吗</p>
<pre class="lang-cpp prettyprint-override"><code>int main() {
pc.attach(&callback, Serial::RxIrq);
while(1) {
// your code for LED flashing
// no need to call pc.getc() in here
one = 1;
wait(c);
one = 0;
wait(c);
}
}
</code></pre>
<p>这样,LED会继续闪烁,并且每当mbed收到值时,您就可以更新<code>c</code>。你知道吗</p>
<p>另外,看起来您正在发送ASCII字符。ASCII<code>1</code>是十进制的<code>49</code>。因此,<code>pc.get()</code>在发送<code>'1'</code>时返回<code>49</code>。我不认为那是你想要的。如果您总是发送一个数字(1~9),一个简单的解决方法是<code>pc.getc() - 48</code>。但您最好将<code>string</code>解析为<code>int</code>,并在python端进行错误处理。你知道吗</p>