Arduino串行打印停止从操纵杆模块输出一定值

时间:2019-07-01 00:39:42

标签: c++ arduino

我正在开发一个通过操纵杆模块移动鼠标的应用程序。我使用C++是因为我的开发板不支持Arduino IDE中的Mouse.move

我遇到了一个问题,如果我将操纵杆完全推回到X值为1024的位置,则打印出这些值的命令控制台将完全停止打印,没有空白字符串刚刚暂停。但是,如果我从Arduino IDE看串行监视器,我会看到它正在连续打印1024, 513并按需要打印。

我发现,当我使用Y值执行此操作时,它会同时将1024打印到Arduino IDE和C++串行监视器。例如513, 1024

我对将C++与Arduino结合使用还很陌生,这可能很简单。

JoystickModule.ino(Arduino代码)

#define BAUD 9600

const int SW_pin = 2;
const int X_pin = 0;
const int Y_pin = 1;

void setup(){
  Serial.begin(BAUD);
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
}

void loop(){
  Serial.println((String)analogRead(X_pin) + "," + (String)analogRead(Y_pin));
  delay(100);
}

main.cpp

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <Windows.h>
#include "SerialPort.h"
#include <vector>

using namespace std;

//portname must contain these backslashes, and remember to replace the following com port

char *port_name = "\\\\.\\COM4";

//String for incoming data
char incomingData[MAX_DATA_LENGTH];

int main() {
    SerialPort arduino(port_name);
    if (arduino.isConnected()) cout << "Connection Established" << endl;
    else cout << "ERROR, check port name";

    while (arduino.isConnected()) {
        int read_result = arduino.readSerialPort(incomingData, MAX_DATA_LENGTH);
        //store into array for later use by splitting by "," e.g. "513, 508"
        //result[0] will store the X val and result[1] will store the Y val
        string result[2];
        int xVal = 0;
        int yVal = 0;
        string data = (string)incomingData;
        size_t pos = data.find(",");

        result[0] = data.substr(0, pos);
        result[1] = data.substr(pos + 1, 4);

        cout << data << endl;

        Sleep(100);
    }
    return 0;
}

来自this tutorial

的头文件和其他资源

SerialPort.h

#pragma once
#ifndef SERIALPORT_H
#define SERIALPORT_H

#define ARDUINO_WAIT_TIME 2000
#define MAX_DATA_LENGTH 255

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

class SerialPort
{
private:
    HANDLE handler;
    bool connected;
    COMSTAT status;
    DWORD errors;
public:
    SerialPort(char *portName);
    ~SerialPort();

    int readSerialPort(char *buffer, unsigned int buf_size);
    bool writeSerialPort(char *buffer, unsigned int buf_size);
    bool isConnected();
};

#endif // SERIALPORT_H

SerialPort.cpp

#include "stdafx.h"
#include "SerialPort.h"

SerialPort::SerialPort(char *portName)
{
    this->connected = false;

    this->handler = CreateFileA(static_cast<LPCSTR>(portName),
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    if (this->handler == INVALID_HANDLE_VALUE) {
        if (GetLastError() == ERROR_FILE_NOT_FOUND) {
            printf("ERROR: Handle was not attached. Reason: %s not available\n", portName);
        }
        else
        {
            printf("ERROR!!!");
        }
    }
    else {
        DCB dcbSerialParameters = { 0 };

        if (!GetCommState(this->handler, &dcbSerialParameters)) {
            printf("failed to get current serial parameters");
        }
        else {
            dcbSerialParameters.BaudRate = CBR_9600;
            dcbSerialParameters.ByteSize = 8;
            dcbSerialParameters.StopBits = ONESTOPBIT;
            dcbSerialParameters.Parity = NOPARITY;
            dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE;

            if (!SetCommState(handler, &dcbSerialParameters))
            {
                printf("ALERT: could not set Serial port parameters\n");
            }
            else {
                this->connected = true;
                PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
                Sleep(ARDUINO_WAIT_TIME);
            }
        }
    }
}

SerialPort::~SerialPort()
{
    if (this->connected) {
        this->connected = false;
        CloseHandle(this->handler);
    }
}

int SerialPort::readSerialPort(char *buffer, unsigned int buf_size)
{
    DWORD bytesRead;
    unsigned int toRead;

    ClearCommError(this->handler, &this->errors, &this->status);

    if (this->status.cbInQue > 0) {
        if (this->status.cbInQue > buf_size) {
            toRead = buf_size;
        }
        else toRead = this->status.cbInQue;
    }

    if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL)) return bytesRead;

    return 0;
}

bool SerialPort::writeSerialPort(char *buffer, unsigned int buf_size)
{
    DWORD bytesSend;

    if (!WriteFile(this->handler, (void*)buffer, buf_size, &bytesSend, 0)) {
        ClearCommError(this->handler, &this->errors, &this->status);
        return false;
    }
    else return true;
}

bool SerialPort::isConnected()
{
    return this->connected;
}

谢谢。

0 个答案:

没有答案