ฉันมีปัญหาในการสื่อสารจาก C ++ ถึง Arduino โดยใช้ Serial ฉันใช้วิธีการที่ Salil Kapur กล่าวถึงที่นี่: https://salilkapur.wordpress.com/2013/03/08/communicating-with-arduino-using-c/. ฉันได้ปรับกลยุทธ์การเขียนของเขาโดยตรงไปยังไฟล์ของ Arduino โดยใช้ c++ สำหรับงานเฉพาะของฉัน ฉันต้องส่งสตริงอักขระ (ซึ่งเป็นคำสั่งสำหรับ Arduino ในการประมวลผล) ไปยัง Arduino ฉันได้อ่านตัวอักษรจากไฟล์ในโปรแกรม C++ แล้ว แต่ด้วยเหตุผลบางอย่าง ฉันไม่ได้รับอะไรเลยจากจอภาพ Arduino Serial ของฉันเมื่อฉันส่งมันไป ฉันคิดว่าอัตรารับส่งข้อมูลอาจเป็นปัญหา แต่ฉันไม่เป็นบวก ฉันจะให้รหัสของฉันแก่คุณเพื่อขอความช่วยเหลือโดยเฉพาะ หากมีใครสามารถแนะนำฉันถึงวิธีรับ 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);
}
รหัสอาร์ดูโน่:
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:
w a d s w d a a s d w
สตริงอักขระนั้นจะมีทิศทาง (ขึ้น, ลง, ซ้าย, ขวา) ตาม WASD