簡體   English   中英

修復Arduino和C ++ Mac OS X之間的通信

[英]Fixing Communications between Arduino and C++ Mac OS X

我在使用串行從C ++與Arduino通訊時遇到問題。 我在這里使用了Salil Kapur討論的方法: https : //salilkapur.wordpress.com/2013/03/08/communicating-with-arduino-using-c/ 我已經調整了他使用c ++直接寫入Arduino文件的策略,對於我的特定任務,我需要向Arduino發送一串char(用於arduino處理的命令)。 我已經從C ++程序的文件中讀取了字符,但是由於某種原因,當我將其發送過來時,我的Arduino串行監視器什么也沒收到。 我認為波特率可能是問題所在,但我並不樂觀。 我將為您提供我的代碼以提供特定幫助。 如果有人可以建議我如何使C ++將字符寫入串行監視器以進行讀取:

C++:
#include
#include //For sleep()
#include //For FILE
#include //For ifstream
#include //For assert()
#include //For string
using namespace std;

int main()
{
   //Setting up the output to the Arduino
   FILE *arduino;
   arduino = fopen(“/dev/tty.usbmodem1411″,”w”);  //Declare the file in write mode
   if (arduino == NULL) perror (“Error opening file”);

   //Setting up the file input stream
   ifstream inFile (“input.txt”);
   assert(inFile.is_open());

   char input = '\0'; //Starts out as NULL

   while (input!= EOF) {
      fprintf(arduino,”%c “, input); //Writing to the file
      inFile >> input; //Getting the file input to make sure it isn’t the EOF
      sleep(1);
   }
   fclose(arduino);
}

Arduino代碼:

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   char input;
   if(Serial.available()) //If anything is in the Serial 
   {
      input=Serial.read();

      Serial.println(input); //Print out any input
   }
}

來自input.txt的樣本輸入:

wadswdaasdw

該字符串將根據WASD提供方向(上,下,左,右)。

可以使用文件句柄將數據寫入串行設備,但是首先嘗試使用本機Unix串行通信API作為健全性檢查可能會有所幫助。

看看todbot.com上的這篇文章: Arduino系列:與Arduino交流的C代碼

todbot.com帖子描述了一個通用程序,用於通過其串行連接與Arduino通訊。 該源代碼有很多用於從命令行獲取設置的代碼,並且因為它很健壯而有些令人生畏,但是如果您想了解串行通信的方式,則串行通信的業務端相當簡單。

我編寫了有關該主題的系列文章,其中包含一些輕量級代碼(在第3部分和第4部分中),它們演示了如何幾乎完全按照您的意圖進行操作。

如何使用C從Linux中的Arduino讀取串行數據:第1部分

希望這些鏈接對您有所幫助! 串行通信很困難,因此,如果不按照自己的方式進行,就不要打敗自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM