如何使用串口从kinect发送信息到arduino

本文关键字:信息 arduino kinect 何使用 串口 | 更新日期: 2023-09-27 18:07:28

我摆弄了一下Kinect SDK和arduino,我在网上找了一种从Kinect传递信息到arduino的方法。我只是想传递一些非常简单的信息(即如果kinect识别手势,arduino这样做),我发现人们在谈论使用串行端口来做到这一点。我看了文档和人们的代码,但我是c#的新手,并不真正理解串口是如何工作的。我用的是Windows v1的Kinect,我正试图将信息传递到一个主板上。如果有人能解释如何设置串行端口,将不胜感激!

p。我一直在Visual Studio 2015社区为Kinect和Arduino IDE为Arduino工作。

如何使用串口从kinect发送信息到arduino

我知道这有点明显,但是从使用System.IO.Ports命名空间中的SerialPort组件开始。

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx MSDN页面的示例部分提供了获取串行端口所需的所有功能,并解析数据。你当然需要打开你的串行端口,在Arduino端设置正确的奇偶校验来接收你想要发送和处理的数据-但是有足够多的例子。

Mike提供的示例应该给出一个一般的布局,但至于在程序中设置串行端口,您最有可能遵循9600 8N1。

下面是9600 8N1初始串行端口设置的示例(这些是使用Boost库的c++,但它应该清楚您需要设置的内容):

port.set_option(asio::serial_port_base::baud_rate(9600));
port.set_option(asio::serial_port_base::character_size(8));
port.set_option(asio::serial_port_base::flow_control(asio::serial_port_base::flow_control::none));
port.set_option(asio::serial_port_base::parity(asio::serial_port_base::parity::none));
port.set_option(asio::serial_port_base::stop_bits(asio::serial_port_base::stop_bits::one));

当我在过去设置这个时,我必须确保的唯一一件事是Arduino上的波特率与上述设置相匹配,否则其余的似乎是默认设置。但我不能保证你的情况。

您需要写入Arduino连接的串行。Windows将串行视为文件,因此要写入串行,请使用这个类(我使用VS2013,但它也应该适用于VS2015):

报头(命名为SerialClass.h):

#ifndef SERIALCLASS_H_INCLUDED
#define SERIALCLASS_H_INCLUDED
#define ARDUINO_WAIT_TIME 2000
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
class Serial
{
private:
    //Serial comm handler
    HANDLE hSerial;
    //Connection status
    bool connected;
    //Get various information about the connection
    COMSTAT status;
    //Keep track of last error
    DWORD errors;
public:
    //Initialize Serial communication with the given COM port
    Serial(char *portName);
    //Close the connection
    ~Serial();
    //Read data in a buffer, if nbChar is greater than the
    //maximum number of bytes available, it will return only the
    //bytes available. The function return -1 when nothing could
    //be read, the number of bytes actually read.
    int ReadData(char *buffer, unsigned int nbChar);
    //Writes data from a buffer through the Serial connection
    //return true on success.
    bool WriteData(char *buffer, unsigned int nbChar);
    //Check if we are actually connected
    bool IsConnected();

};
#endif // SERIALCLASS_H_INCLUDED

CPP文件(名为SerialClass.cpp):

#include "SerialClass.h"
#include<iostream>
Serial::Serial(char *portName)
{
    //We're not yet connected
    this->connected = false;
    //Try to connect to the given port throuh CreateFile
    this->hSerial = CreateFile(portName,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    //Check if the connection was successfull
    if (this->hSerial == INVALID_HANDLE_VALUE)
    {
        //If not success full display an Error
        if (GetLastError() == ERROR_FILE_NOT_FOUND){
            //Print Error if neccessary
            printf("ERROR: Handle was not attached. Reason: %s not available.'n", portName);
        }
        else if (GetLastError() == ERROR_ACCESS_DENIED)
        {
            printf("It is this!!!!");
        }
        else
        {
            printf("ERROR!!!");
            DWORD error = GetLastError();
            std::cout << error;
        }
    }
    else
    {
        //If connected we try to set the comm parameters
        DCB dcbSerialParams = { 0 };
        //Try to get the current
        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            //If impossible, show an error
            printf("failed to get current serial parameters!");
        }
        else
        {
            //Define serial connection parameters for the arduino board
            dcbSerialParams.BaudRate = CBR_9600;
            dcbSerialParams.ByteSize = 8;
            dcbSerialParams.StopBits = ONESTOPBIT;
            dcbSerialParams.Parity = NOPARITY;
            //Setting the DTR to Control_Enable ensures that the Arduino is properly
            //reset upon establishing a connection
            dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
            //Set the parameters and check for their proper application
            if (!SetCommState(hSerial, &dcbSerialParams))
            {
                printf("ALERT: Could not set Serial Port parameters");
            }
            else
            {
                //If everything went fine we're connected
                this->connected = true;
                //Flush any remaining characters in the buffers 
                PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
                //We wait 2s as the arduino board will be reseting
                Sleep(ARDUINO_WAIT_TIME);
            }
        }
    }
}
Serial::~Serial()
{
    //Check if we are connected before trying to disconnect
    if (this->connected)
    {
        //We're no longer connected
        this->connected = false;
        //Close the serial handler
        CloseHandle(this->hSerial);
    }
}
int Serial::ReadData(char *buffer, unsigned int nbChar)
{
    //Number of bytes we'll have read
    DWORD bytesRead;
    //Number of bytes we'll really ask to read
    unsigned int toRead;
    //Use the ClearCommError function to get status info on the Serial port
    ClearCommError(this->hSerial, &this->errors, &this->status);
    //Check if there is something to read
    if (this->status.cbInQue>0)
    {
        //If there is we check if there is enough data to read the required number
        //of characters, if not we'll read only the available characters to prevent
        //locking of the application.
        if (this->status.cbInQue>nbChar)
        {
            toRead = nbChar;
        }
        else
        {
            toRead = this->status.cbInQue;
        }
        //Try to read the require number of chars, and return the number of read bytes on success
        if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
        {
            return bytesRead;
        }
    }
    //If nothing has been read, or that an error was detected return -1
    return -1;
}

bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
    DWORD bytesSend;
    //Try to write the buffer on the Serial port
    if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
    {
        //In case it don't work get comm error and return false
        ClearCommError(this->hSerial, &this->errors, &this->status);
        return false;
    }
    else
        return true;
}
bool Serial::IsConnected()
{
    //Simply return the connection status
    return this->connected;
}

用法示例(向串行写入文本):

Serial Arduino = Serial("COM3"); //My Arduino is at COM3 - change it for yours!
if (Arduino.IsConnected() == false)  //Checks if it connected
{
    std::cout << "Your Arduino isn't connected!" << std::endl;
    std::cin.ignore();
    return 1;
}
Arduino.WriteData("This is sent data"); //Sends "This is sent data" to the Serial which Arduino is connected to!

Arduino应该从Serial读取数据(使用Serial类)。

请注意,你的Arduino将不得不通过USB连接到笔记本电脑(不是Wifi)这工作…

来源:我做了一个项目就像这样!