在基于Unix的软件上,需要向Arduino发送0到179之间的数字,并且Arduino将把该数字作为角度应用到舵机上。但我不知道在terminos结构中需要更改哪些参数以允许串行通信。
以下是C++代码:
以下是C++代码:
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <termios.h>
using namespace std;
int main()
{
unsigned int angle;
ofstream arduino;
struct termios ttable;
//cout<<"test-1";
arduino.open("/dev/tty.usbmodem3a21");
//cout<<"test-2";
if(!arduino)
{
cout<<"\n\nERR: could not open port\n\n";
}
else
{
if(tcgetattr(arduino,&ttable)<0)
{
cout<<"\n\nERR: could not get terminal options\n\n";
}
else
{
//there goes the terminal options setting for the output;
ttable.c_cflag = -hupcl //to prevent the reset of arduino
cfsetospeed(&ttable,9600);
if(tcsetattr(arduino,TCSANOW,&ttable)<0)
{
cout<<"\n\nERR: could not set new terminal options\n\n";
}
else
{
do
{
cout<<"\n\ninsert a number between 0 and 179";
cin>>angle;
arduino<<angle;
}while(angle<=179);
arduino.close();
}
}
}
}
这是Arduino的:
#include <Servo.h>
Servo servo;
const int pinServo = 2;
unsigned int angle;
void setup()
{
Serial.begin(9600);
servo.attach(pinServo);
servo.write(0);
}
void loop()
{
if(Serial.available()>0)
{
angle = Serial.read();
if(angle <= 179)
{
servo.write(angle);
}
}
}
请问您需要修改"ttable"的哪些内容?
termios
的c_cc
字段是一个cc_t
数组,该类型在<bits/termios.h>
中被定义为unsigned char
。因此,对于c_cc[VTIME]
的浮点值将会被静默转换(除非您向 gcc 传递-Wconversion
,这从来不是一个坏主意)。 - David G