如何通过串口向arduino发送整数?
我想通过串行连接将整数发送到 Arduino。 例如,当我发送“1”时,Arduino收到的数据是“49” 当我发送“a”时,Arduino收到的数据是“97”
Python、ord()
和 unichr()
。它们的行为是这样的:
unichr(97) = u"a"
ord(u"a")=97
是否有等效的 C 函数?
I want to send integers to Arduino via a serial connection.
For example, when I send "1" the data received by Arduino is "49"
and when I send "a" the data received by Arduino is "97"
There are two functions in Python, ord()
and unichr()
. They behaved like this:
unichr(97) = u"a"
ord(u"a")=97
Are there equivalent C functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用字母数字到整数函数。
您还可以找到头文件cstdlib、stdlib.h、C 标准通用实用程序库很有用。虽然它说的是 C++,但这个特定部分是标准 C 库。请注意,您可以将 C++ 与 Arduino 结合使用。
Use the alphanumeric to integer function.
You may also find header cstdlib, stdlib.h, C Standard General Utilities Library useful. Although it says C++, this particular section is the standard C library. Notice that you can use C++ with Arduino.
只要您将字符存储为其 ASCII 值,最简单的方法 - 如果您的目标是转换个位数 - 就是减去 0 的 ASCII 值:
'8'- '0'
会给你 unsigned char 值 8。你需要确保它是一个数字而不是某个字符,但这很容易通过检查结果是否低于或等于 9 来完成。同样,你得到通过添加值“0”得到单个数字 z 的 ASCII 值。
As long as you have your characters stored as their ASCII-value, the easiest way - if your goal is converting single digits - is to subtract the ASCII-value of 0:
'8'-'0'
will give you the unsigned char value 8. You need to make sure it's a digit and not some character, but that is easily done by just checking if the result is below or equal 9.Similarly, you get the ASCII-value of a single digit z by adding the value of '0'.