串口通讯。无法从Visual Studio向Arduino发送消息

时间:2017-08-03 06:40:41

标签: c++ visual-studio-2017 arduino-uno

我对Arduino编码很新,所以我需要一些帮助。我希望我的视觉工作室向我的Arduino uno发送消息,以便电机在收到消息时移动。我尝试过使用字符,ascii代码和数字,但不发送消息。任何人都可以帮我弄清楚我的问题是什么?谢谢。

这是我的代码:(使用头文件更新)

    #include <opencv2/core.hpp>
    #include <opencv2/highgui.hpp>
    #include <opencv2/videoio.hpp>
    #include <opencv2/objdetect.hpp>
    #include <opencv2/imgproc.hpp>
    #include <iostream>
    #include <stdio.h>
    #include <tchar.h>
    #include <thread>
    #include <chrono>
    #include <tserial.h>

    using namespace cv;
    using namespace std;

    void detectAndDisplay(Mat img);

    /** Global variables */  //Declaring 
    //-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
    String face_cascade_name = "haarcascade_frontalface_alt.xml";
    String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
    CascadeClassifier face_cascade;
    CascadeClassifier eyes_cascade;
    string window_name = "Capture - Face detection ";
    // Serial to Arduino global declarations
    int arduino_command;
    Tserial *arduino_com;
    short MSBLSB = 0;
    unsigned char MSB = 0;
    unsigned char LSB = 0;
    // Serial to Arduino global declarations

    //main program
    int main(int argc, char** argv)
    {

        int nb = 0;
        //my own code don't need to care 

        // capture from web camera init
        VideoCapture cap(0);
        cap.open(0);

        Mat img;
        Mat pic;
        Mat frame_gray;

        // serial to Arduino setup 
        arduino_com = new Tserial();
        if (arduino_com != 0) {

            status = arduino_com->connect("COM13", 9600, spNONE);
            printf("Connecting...");
        }
        // serial to Arduino setup 
        // Initialize the inbuilt Harr Cascade frontal face detection
        // Below mention the path of where your haarcascade_frontalface_alt2.xml file is located

        CascadeClassifier face_cascade;
        face_cascade.load("C:/OpenCV/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
        // i tried changing this line to match my folder in C Drive


        for (;;)
        {

            // Image from camera to Mat

            cap >> img;
            cap >> pic;


            // Just resize input image if you want
            resize(img, img, Size(640, 480));
            resize(pic, pic, Size(640, 480));

            // Container of faces
            vector<Rect> faces;

            cvtColor(img, frame_gray, CV_BGR2GRAY);
            equalizeHist(frame_gray, frame_gray);

            // Detect faces
            face_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(140, 140));



            //Show the results
            // Draw circles on the detected faces

            for (int i = 0; i < faces.size(); i++)
            {
                Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);   //center position of face
                ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); 

                LSB = faces[i].x & 0xff;
                // read next significant byte 
                MSB = (faces[i].x >> 8) & 0xff;
                arduino_com->sendChar(MSB);
                arduino_com->sendChar(LSB);             //send a message to arduino

                // Send Y axis
                LSB = faces[i].y & 0xff;
                MSB = (faces[i].y >> 8) & 0xff;
                arduino_com->sendChar(MSB);
                arduino_com->sendChar(LSB);
                /*if (arduino_com = )
                {
                    printf("sent");                     // check whether message is being sent
                }*/
                // serial com port send  
                //arduino_com->sendChar(0x4F);
                //arduino_com->sendChar('P');
                //arduino_com->sendChar('E');
                //arduino_com->sendChar('N');
                //arduino_com->sendChar(0x0A);


                Mat faceROI = frame_gray(faces[i]);
                std::vector<Rect> eyes;
            }
            //if (int i ) {                                      // add if statement, if can detect face, take picture.
            //std::this_thread::sleep_for(5s);                    // time delay by 5 seconds 
            //either this
            //imwrite("123.jpg", img);                          // captures only 1 picture with same name and changes the img when taken again
            //or this
            //imwrite(format("MyImage%d.png", nb++), img);        // captures img and give random name
        //}


            imshow("live", pic);
            imshow("capture", img);
            // To draw rectangles around detected faces
            /* for (unsigned i = 0; i<faces.size(); i++)
            rectangle(img,faces[i], Scalar(255, 0, 0), 2, 1);*/
            int key2 = waitKey(20);

        }
        if (int i = 1) {
            // Serial to Arduino - shutdown
            arduino_com->disconnect();
            delete arduino_com;
            arduino_com = 0;
            printf("Arduino disconnected!!");
            // Serial to Arduino - shutdown
        }
        return 0;
    }

Arduino代码:

#define  servomaxx   180   // max degree servo horizontal (x) can turn
#define  servomaxy   180   // max degree servo vertical (y) can turn
#define  screenmaxx   320   // max screen horizontal (x)resolution
#define  screenmaxy   240    // max screen vertical (y) resolution
#define  servocenterx   90  // center po#define  of x servo
#define  servocentery   90  // center po#define  of y servo
#define  servopinx   8   // digital pin for servo x
#define  servopiny   9  // digital servo for pin y  
#define  baudrate 9600  // com port speed. Must match your C++ setting        MAKE SURE ITS 9600 C++ CODE MAKE SURE ITS THE SAME
#define distancex 1  // x servo rotation steps
#define distancey 1  // y servo rotation steps

String inputString = ""; 
String strOpen="O";
boolean stringComplete = false; 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(baudrate);        // connect to the serial port
  Serial.println("Starting Cam-servo Face tracker");

  pinMode(servopinx, OUTPUT);   // declare the LED's pin as output
  pinMode(servopiny, OUTPUT);   // declare the LED's pin as output

  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop() {

    while (Serial.available()) {
     digitalWrite(8, HIGH);
     digitalWrite(9, HIGH);            //i just want it to get a message for it to move
  }

}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    //if (inChar == '\n') {
      stringComplete = true;
    //}
  }
}

0 个答案:

没有答案