arduino pmw 串口 字符串_求救啊 arduino 串口读写问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#define T 2

#define R 3

#define P 4

#define Y 5

#define A1 6

#define A2 7

#define A3 10

#define A4 11

#define cx 15

#define cy 16

#define RX 8

#define TX 9

Servo tservo;//throttle

Servo rservo;//roll

Servo pservo;//pitch

Servo yservo;//yaw

Servo a1servo;//aux1

Servo a2servo;//aux2

Servo a3servo;//aux3

Servo a4servo;//au4

Servo cxservo;//camerax

Servo cyservo;//cameray

SoftwareSerial sserial(8, 9);

char infostore[26] = {0};

void setup() {

// put your setup code here, to run once:

pinMode(T, OUTPUT); //T

pinMode(R, OUTPUT); //R

pinMode(P, OUTPUT); //P

pinMode(Y, OUTPUT); //Y

pinMode(A1, OUTPUT); //A1

pinMode(A2, OUTPUT); //A2

pinMode(A3, OUTPUT); //A3

pinMode(A4, OUTPUT); //A4

pinMode(cx, OUTPUT); //X

pinMode(cy, OUTPUT); //Y

tservo.attach(T);

rservo.attach(R);

pservo.attach(P);

yservo.attach(Y);

a1servo.attach(A1);

a2servo.attach(A2);

a3servo.attach(A3);

a4servo.attach(A4);

Serial.begin(9600);

sserial.begin(9600);

}

void loop() {

// put your main code here, to run repeatedly:

char readbuffer[64];

char checkbuffer;

while(Serial.available()!=true)

{

;

}

delay(5);

//sserial.println("ready");

checkbuffer = Serial.read();

for (; checkbuffer != '#';)

{

checkbuffer = Serial.read();

}

Serial.println("get");

Serial.readBytesUntil('#', readbuffer, 64);

readbuffer[63] = 0;

Serial.println(readbuffer);

if (readbuffer[24] == 'E')

{

Serial.println("ture");

for (int i = 0; i <= 25; i++)

{

infostore[i] = readbuffer[i];

}

memset( readbuffer, 0, sizeof(readbuffer) );

while (Serial.read() >= 0) {};

putout();

delay(2);

}

}

void putout()

{

char tpwm[4];

char rpwm[4];

char ppwm[4];

char ypwm[4];

char a1info;

char a2info;

char a3info;

char a4info;

char Xinfo;

char Yinfo;

int ti, ri, pi, yi;

int t, r, p, y;

int a1, a2, a3, a4;

tpwm[0] = infostore[1];

tpwm[1] = infostore[2];

tpwm[2] = infostore[3];

tpwm[3] = 0;

//Serial.println(tpwm);

rpwm[0] = infostore[5];

rpwm[1] = infostore[6];

rpwm[2] = infostore[7];

rpwm[3] = 0;

//Serial.println(rpwm);

ppwm[0] = infostore[9];

ppwm[1] = infostore[10];

ppwm[2] = infostore[11];

ppwm[3] = 0;

//Serial.println(ppwm);

ypwm[0] = infostore[13];

ypwm[1] = infostore[14];

ypwm[2] = infostore[15];

ypwm[3] = 0;

//Serial.println(ypwm);

a1info = infostore[17];

a2info = infostore[18];

a3info = infostore[19];

a4info = infostore[20];

ti = ctoi(tpwm);

ri = ctoi(rpwm);

pi = ctoi(ppwm);

yi = ctoi(ypwm);

t = ti * 10;

r = ri * 10;

p = pi * 10;

y = yi * 10;

// sserial.println(t);

// sserial.println(r);

// sserial.println(p);

// sserial.println(y);

// Serial.println(t);

// Serial.println(r);

// Serial.println(p);

// Serial.println(y);

a1 = 0;

a2 = 0;

a3 = 0;

a4 = 0;

tservo.writeMicroseconds(t);

rservo.writeMicroseconds(r);

pservo.writeMicroseconds(p);

yservo.writeMicroseconds(y);

a1servo.writeMicroseconds(a1);

a2servo.writeMicroseconds(a2);

a3servo.writeMicroseconds(a3);

a4servo.writeMicroseconds(a4);

delay(3);

}

int ctoi(char a[3])

{

if (a[0] != '0' || a[1] != '0' || a[2] != '0')

{

int b, c, d, e;

b = ((int)a[0]) - 48;

c = ((int)a[1]) - 48;

d = ((int)a[2]) - 48;

e = (b * 100) + (c * 10) + (d);

//Serial.println(e);

return e;

}

else {

return 0;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值