我已经摆弄了Kinect SDK和arduino,我已经浏览了网页,找到了将信息从Kinect传递到arduino的方法。我只想传递一些非常简单的信息(例如,如果kinect识别出一个手势,arduino就这样做了)我发现有人在谈论使用串口来做这件事。我查看了文档和人员的代码,但我对C#很新,并不太了解串口的工作原理。我有Kinect for Windows v1,我正在尝试将信息传递给botboarduino。如果有人可以解释如何设置串口,非常感谢!
PS。我一直在用于Kinect的Visual Studio 2015社区和用于arduino的Arduino IDE中工作。
答案 0 :(得分:1)
有点明显,我知道,但首先使用SerialPort
命名空间中的System.IO.Ports
组件。
答案 1 :(得分:1)
此示例部分:https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx
MSDN页面提供了启动串口和解析数据所需的所有功能。您当然需要打开串口,在Arduino端设置正确的奇偶校验,以接收您想要发送和处理的所需数据 - 但是有足够的例子。
答案 2 :(得分:0)
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上的波特率与上述设置相匹配,否则其余部分似乎是默认设置。我不能保证你的情况。
答案 3 :(得分:0)
您需要写入连接到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)才能正常工作。 。 。
来源:我做了一个像这样的项目!