【问题标题】:How to use the serial port to send information from kinect to arduino?如何使用串口将信息从 kinect 发送到 arduino?
【发布时间】:2015-08-12 15:50:34
【问题描述】:

我一直在摆弄 Kinect SDK 和 arduino,并在网上寻找一种将信息从 Kinect 传递到 arduino 的方法。我只是想传递一些非常简单的信息(即,如果 kinect 识别手势,arduino 会这样做),我发现人们在谈论使用串口来做到这一点。我查看了文档和人们的代码,但我对 C# 很陌生,并不真正了解串行端口的工作原理。我有适用于 Windows v1 的 Kinect,并且正在尝试将信息传递给 botboarduino。如果有人能解释如何设置串口,将不胜感激!

附言。我一直在 Visual Studio 2015 社区为 Kinect 工作,在 Arduino IDE 为 arduino 工作。

【问题讨论】:

  • 您无法将 Kinect 连接到 Arduino。 PC 需要介于两者之间。它运行一个接收 Kinect 通知并使用串行端口让 Arduino 知道的程序。
  • 嘿,谢谢汉斯。我事先为我的问题不清楚而道歉。我将两者都连接到我的 Windows 8 PC 上,并且一直在 Visual Studios 2015 IDE 中的 Kinect 和 arduino IDE 中的 arduino 上工作
  • 考虑使用开源库,例如github.com/SolidSoils/Arduino。或深入了解 Microsoft IOT (dev.windows.com/en-us/iot)。

标签: c# serialization arduino kinect kinect-sdk


【解决方案1】:

有点明显,我知道,但首先使用来自 System.IO.Ports 命名空间的 SerialPort 组件。

【讨论】:

  • 谢谢格雷厄姆。之后你会做什么?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    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 上的波特率与上述设置相匹配,否则其余的似乎是默认设置。不过,我不能保证你的情况。

    【讨论】:

      【解决方案4】:

      您需要写入 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)才能正常工作。 . .

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

      【讨论】:

        猜你喜欢
        • 2012-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多