基本串行端口通信Visual Studio C++(在浏览现有示例中时遇到问题)
Basic Serial Port Communication Visual Studio C++ (having trouble wading through existing examples)
我刚刚开始使用C++,我无法弄清楚如何通过串行端口将一些信息发送到我的arduino(更容易编码)。 该信息是使用 opencv 从我的网络摄像头读取的 RGB 值。 这就是我到目前为止所拥有的(有人帮助我):
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
int main()
{
int delay = 30; // 30 ms between frames
int x = 20; // X coordinate of desired pixel
int y = 140; // Y coordinate of desired pixel
int key = 0;
cv::VideoCapture camera(0);
cv::Mat img;
while(key != 27)
{
camera >> img;
cv::imshow("Web cam image", img);
cv::Vec3b pixel = img.at<cv::Vec3b>(y,x);
// Write the values of pixel to the serial port here
key = cv::waitKey(delay);
}
return 0;
}
我在网上找到的所有内容似乎都非常复杂,我已经试图弄清楚几个小时了。 有没有不太复杂的方法
Windows 上的串行 I/O 可能非常复杂,但从根本上说它不需要 - 您只需打开端口,设置波特率等,然后写入数据。尝试这样的事情(显然根据需要调整COM端口和参数):
HANDLE hFile = CreateFile("\\.\COM1", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
DCB port{0};
port.DCBlength = sizeof(port);
if (GetCommState(hFile, &port)
&& BuildCommDCB("baud=19200 parity=N data=8 stop=1", &port)
&& SetCommState(hFile, &port))
{
DWORD written;
// write the data - I'm assuming &pixel[0] will work for a cv vector
// if not, you'll have to fix this bit yourself
WriteFile(hFile, &pixel[0], 3, &written, NULL);
}
CloseHandle(hFile);
}
相关文章:
- 编译包含字符串的代码时遇到问题
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从其他类访问类时遇到问题
- 打印问题.我在数组中打印值时遇到一些问题
- 使用静态成员声明类时遇到问题
- C++ 填充数组时遇到问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 我在使用C++文件时遇到了一些问题
- 在将字符串放入字符串向量时遇到问题?
- 余数除法和不允许除以零 (c++) 时遇到问题
- 我在C++中使用 getline() 函数时遇到问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 在我的四叉树实现中遇到问题
- 在 c++ 中循环遍历数组时遇到问题
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- 编译 Cilk Plus 程序时遇到问题