与arduino串行通信 [英] serial communication with arduino

查看:95
本文介绍了与arduino串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯现在使用代码使模板匹配软磨损我希望使用串行通信进入arduino板。但它没有成功。这是我的code.my arduino板通过com10与pc连接(我在设计经理中看到它)。该端口未被另一个软件打开。但是在开始调试后总是打印这个消息。 错误港口开了!此处 https://docs.google.com/document/d/1taoPE3bb4i1D-B8xG2LgT6Liy2csnWO2jxGn7E5zLf4/编辑?usp =分享 [ ^ ]

请帮帮我





um make template matching soft wear using code now i want get x y cordinate in to the arduino board using serial communication. but its not success. this is my code.my arduino board connect with pc via com10(i see it in the devise manager) . that port not open by another softwaer. but always print this msg after begin debug. "error Port opened!" image here https://docs.google.com/document/d/1taoPE3bb4i1D-B8xG2LgT6Liy2csnWO2jxGn7E5zLf4/edit?usp=sharing[^]
pls help me


#include <iostream>
#include "opencv2/opencv.hpp"
#include 
#include 
#include 
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <sstream>


using namespace cv;
using namespace std;

Point point1, point2; /* vertical points of the bounding box */
int drag = 0;
Rect rect; /* bounding box */
Mat img, roiImg; /* roiImg - the part of the image in the bounding box */
int select_flag = 0;
bool go_fast = false;

Mat mytemplate;

void track(cv::Mat &img, const cv::Mat &templ, const cv::Rect &r )
{
    static int n = 0;

    if (select_flag)
    {
        templ.copyTo(mytemplate);
        select_flag = false;
        go_fast = true;
    }


    cv::Mat result;
    /// Do the Matching and Normalize
    matchTemplate( img, mytemplate, result, CV_TM_SQDIFF_NORMED );
    normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

    /// Localizing the best match with minMaxLoc
    double minVal; double maxVal; Point minLoc; Point maxLoc;
    Point matchLoc;

    minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
    matchLoc = minLoc;

    rectangle( img, matchLoc, Point( matchLoc.x + mytemplate.cols , matchLoc.y + mytemplate.rows ), CV_RGB(255, 0, 255), 3 );

    std::cout << matchLoc << "\n";
}

///MouseCallback function

void mouseHandler(int event, int x, int y, int flags, void *param)
{
    if (event == CV_EVENT_LBUTTONDOWN && !drag)
    {
        /* left button clicked. ROI selection begins */
        point1 = Point(x, y);
        drag = 1;
    }

    if (event == CV_EVENT_MOUSEMOVE && drag)
    {
        /* mouse dragged. ROI being selected */
        Mat img1 = img.clone();
        point2 = Point(x, y);
        rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
        imshow("image", img1);
    }

    if (event == CV_EVENT_LBUTTONUP && drag)
    {
        point2 = Point(x, y);
        rect = Rect(point1.x, point1.y, x - point1.x, y - point1.y);
        drag = 0;
        roiImg = img(rect);
    }

    if (event == CV_EVENT_LBUTTONUP)
    {
        /* ROI selected */
        select_flag = 1;
        drag = 0;
    }

}


///Main function

int main()
{


 char test[] = "Hello";
   HANDLE hDevice = CreateFile(L"COM10",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,0);
 if (hDevice !=INVALID_HANDLE_VALUE)
    {
        printf("Port opened! \n");
        DCB lpTest;
        GetCommState(hDevice,&lpTest);
        lpTest.BaudRate = CBR_9600;
        lpTest.ByteSize = 8;
        lpTest.Parity = NOPARITY;
        lpTest.StopBits = ONESTOPBIT;
        SetCommState(hDevice,&lpTest);

        DWORD btsIO;

        WriteFile(hDevice,test,strlen(test),&btsIO,NULL);

        CloseHandle(hDevice);
    }
 else{
	    printf("error Port opened! \n");

 }






    int k;
   
        VideoCapture cap(0);
        if (!cap.isOpened())
        return 1;
  
  //  VideoCapture cap;
    //cap.open("~/Downloads/opencv-2.4.4/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind.avi");
  //  cap.open("./Megamind.avi");
    if (!cap.isOpened())
    {
        printf("Unable to open video file\n");
        return -1;
    }

   
        // Set video to 320x240
        //cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
        //cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
      

    cap >> img;
    imshow("image", img);

    while (1)
    {
        cap >> img;
        if (img.empty())
            break;

        if (rect.width == 0 && rect.height == 0)
            cvSetMouseCallback("image", mouseHandler, NULL);
        else
            track(img, roiImg, rect);

        if (select_flag == 1)
            imshow("Template", roiImg);

        imshow("image", img);
        k = waitKey(go_fast ? 30 : 10000);
        if (k == 27)
            break;

    }

	CloseHandle(hDevice);

    return 0;
}

推荐答案

对于数字大于9的COM端口,必须将端口字符串指定为\\ .\COMn。然后是COM10的相应C字符串:

For COM ports with numbers greater than nine, the port string must be specified as " \\.\COMn". The corresponding C string for COM10 is then:
"\\\\.\\COM10"



另请参阅 https://support2.microsoft.com/ default.aspx?scid = kb; EN-US; q115831 [ ^ ]。


这篇关于与arduino串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆