用c++实现pc机与arduino之间的RS232串行通信
Serial communication between pc and arduino via RS232 using c++
我正试图通过RS232线与我的arduino duemilanove进行通信。我只是希望能够从桌面应用程序发送一个字节(或字符)到我的arduino。Arduino正在插入我电脑上的USB COM5。我将RS232插入COM1,然后在RS232的另一端分别连接到arduino引脚TX, RX和GND上的引脚2,3和5。
我在以下链接中找到了c++的串行通信类:
http://playground.arduino.cc/Interfacing/CPPWindows我已经从上面的例子中添加了。h和。cpp文件作为Serial.h和Serial.cpp(我认为这个例子使用了SerialClass.h和SerialClass.cpp,我只是改变了名字)。
在我的arduino上,我有以下代码运行:
// ARDUINO
char incomingByte = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, HEX);
}
}
我的c++程序如下:
// C++
#include <iostream>
#include <Windows.h>
#include "Serial.h"
using namespace std;
int main(void)
{
Serial port("COM1");
char* msg = "Hello Arduino!";
int msgLen = strlen(msg);
bool writeSuccess = port.WriteData(msg, msgLen);
cout << "nn";
system("PAUSE");
}
当我使用Arduino的串口查看器来查看打印的内容时,我得到了非常奇怪的值,与我发送的内容不匹配(据我所知)。
当我发送"Hello Arduino!"时,Arduino打印以下内容:
I received: FFFFFFAB
I received: 3A
I received: 3A
I received: A
I received: FFFFFFFA
I received: FFFFFFEB
I received: 6D
I received: 37
I received: 15
I received: 2D
I received: 23
I received: 21
I received: FFFFFFBD
I received: 0
这似乎不是"Hello Arduino!"的正确十六进制,但我不知道为什么它不正确。有人知道我哪里做错了吗?
Arduino使用TTL逻辑进行串行连接。它期望值在0和5V。RS232使用不同的电压-V到+V。你可能需要一个转换器
…不!上拉和下拉不是为了这个原因。
-
TTL = low: 0V, high: 5V
-
RS232 =低:+ 3:+ 15 v,高:3:-15 v
结果. .你需要一个电压转换器(和逆变器),就像David Skogan正确指出的那样。
例子:
- 使用离散组件(具有自动回声功能,即在PC上,您将看到您发送的数据):http://project.irone.org/simple-rs232-to-ttl-level-converter.html或http://circuit-diagram.hqew.net/Simple-TTL$2dRS232-Level-Converter-Using-Transistor_2757.html
- 带MAX232(或同等)和四个电容的公共电路
- 使用USB-RS232转换器,而不是使用USB-UART转换器,例如使用FT232或类似的东西。不需要任何接口
或. .只需使用Arduino上的USB端口,Arduino上已经有FT232接口。
个人评论:i'd avoid solution 1…相关文章:
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 是否可以使用winusb同时与多个相同的usb设备进行通信
- 混淆了如何使用IDL与Ethovision进行通信
- C++ Boost::asio串行通信与Arduino无法写入
- 我是否需要包含隐式使用/与 WindowsAPI 通信"Windows.h"?
- 通过TCP的PvP通信问题
- C++一个线程如何正确通信其任务已完成?
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- 删除功能不适用于串行通信后多个循环中的多个实例
- 网站和C++程序在同一台 Linux PC 上的通信
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 使用 pybind11 共享 MPI 通信器
- 进程间通信堆内存
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- C++ 两台不同机器之间通过wifi进行套接字通信
- 提升::Asio 异步聊天客户端停止与服务器通信
- 通过 RS232 和 USB 进行串行通信
- 用c++实现pc机与arduino之间的RS232串行通信
- 重新启动后读取 - 使用串行库的RS232串行通信 - C++
- 使用 MFC 的 RS232 通信"Writefile"永远挂起,但