用c++将字符串从PC发送到arduino
sending string from pc to arduino in c++
我正在编写一个c++代码与arduino-uno连接到串口进行通信。我想发送这样一个字符串到arduino: 'X20C20'
我知道如何像这样发送单个字符到arduino:
int fd;
char *buff;
int open_port(void)
{
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/kittens ");
}
else
fcntl(fd, F_SETFL, 0);
return (fd);
}
int main( int argc, char** argv )
{
open_port();
int wr;
char msg[]="h";
/* Write to the port */
wr = write(fd, msg, 1);
close(fd);
}
这个代码用来发送一个char而不是一个String,那么我该怎么做呢??
你为什么不正确使用write ?
write(fd, s, strlen(s));
你必须指定你想要在文件描述符上打印多少字节。
也许你可以阅读这本有趣的关于linux高级编程的书来了解更多信息:http://www.advancedlinuxprogramming.com/alp-folder/alp-apB-low-level-io.pdf
欢呼
我假设您有很好的理由不使用write(fd, msg, strlen(msg))作为参数长度。所以我定义了函数send_string:
void send_string(int fd, char* s)
{
while( *s++ )
write(fd, *s, 1);
}
在main中使用:
int main( int argc, char** argv )
{
open_port();
int wr;
char* msg ="Ciao Mondo!";
/* Write to the port */
send_string(fd, msg);
// or use lenght parameter
write(fd, msg, strlen(msg));
close(fd);
}
安吉洛
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 人脸跟踪arduino代码的优化
- vscode下的Arduino代码出现意外编译错误
- Arduino从Txt读取整数
- 如何通过Conan和CMake找到用于pkg配置的.pc文件
- Arduino C++在构造函数中用参数声明对象数组
- 通过USB在PC和Arduino之间进行串行通信
- Arduino Mega ENC28J60以太网模块直接连接到PC,以接收/发送UDP
- Arduino控制由PC控制
- Arduino到PC蓝牙通信
- PC和Arduino之间的串行通信
- Sizeof() 在 PC 和 Arduino 上的 C++ 之间的差异
- 通过串行方式在Arduino和PC之间传递变量的最佳方式
- Arduino和PC之间通过Usb串行端口(C++控制台应用程序)进行通信
- 用c++实现pc机与arduino之间的RS232串行通信
- 将Arduino串行输出写入PC上的文件或程序
- 使用Arduino信号作为PC的输入
- 用c++将字符串从PC发送到arduino