java中until方法,java - 处理bufferUntil()方法仅适用于'\\ n' - 堆栈内存溢出

TL,DR:设置为'\\ n'时,bufferUntil()和readStringUntil()可以正常工作,但会给其他字符带来麻烦。

将数据发送到pc的代码如下;

Serial.print(rollF);

Serial.print("/");

Serial.println(pitchF);

加工的相关零件是;

myPort = new Serial(this, "COM3", 9600); // starts the serial communication

myPort.bufferUntil('\n');

void serialEvent (Serial myPort) {

// reads the data from the Serial Port up to the character '\n' and puts it into the String variable "data".

data = myPort.readStringUntil('\n');

// if you got any bytes other than the linefeed:

if (data != null) {

data = trim(data);

// split the string at "/"

String items[] = split(data, '/');

if (items.length > 1) {

//--- Roll,Pitch in degrees

roll = float(items[0]);

pitch = float(items[1]);

}

}

}

来自我的传入数据的图片(来自arduino串行监视器):

0.62/-0.52

0.63/-0.52

0.63/-0.52

0.64/-0.53

0.66/-0.53

0.67/-0.53

0.66/-0.54

到这里为止,一切都应该正常。 没什么特别的。 当我将bufferUntil()和readStringUntil()函数的参数更改为除'\\ n'以外的任何参数时, 都会发生问题。 当然,当我这样做时,我也会更改arduino代码中的相应部分。 例如,当用“ k”替换“ \\ n”时,从arduino串行监视器看到的传入数据看起来像,

45.63/22.3k21.51/77.32k12.63/88.90k

并继续这样。 但是处理无法获得每个缓冲区中的第二个值。 当我还在处理控制台上打印值来检查它时,我得到了第一个值(roll),但是第二个值(pitch)显示为NaN。 那是什么问题呢? 它仅在为'\\ n'时才起作用的原因是什么。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值