鲸鱼2013 2022-03-22 22:15 采纳率: 33.3%
浏览 149
已结题

arduino (发送)android studio(接收)通过串口通讯2个字节,向各位请教~

原全部程序:https://blog.csdn.net/xiaomuzi0802/article/details/50441213?spm=1001.2014.3001.5506

1、原程序:
arduino (发送)android studio(接收)通过串口通讯1个字节,可以实现1个控件。
以下关键程序:安卓app1个字节可以实现seebar的动态改变:
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(1);
UsbRequest request = new UsbRequest();
request.initialize(usbDeviceConnection, endpointIn);
while (true) {
request.queue(buffer, 1);
if (usbDeviceConnection.requestWait() == request) {
byte rxCmd = buffer.get(0);
if(rxCmd!=0){
bar.setProgress((int)rxCmd);
}
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
}
else {
break;
}

这是arduino发送1个字的源码: Serial.write(data);
安卓1个字节可以实现seebar的动态改变:

2、自己想法更改的程序:

想法:因为只能控制1个设备太少,想改成2个或多个设备;打算改成发送2个字节,前1个字节设置为设备ID与安卓具体view对应,后面1个字节为设备的具体动态参数,这样就可以通过aduino就可以表现多种设备的动态。
更改后的app程序:
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(2);//自己修改原数字为1
UsbRequest request = new UsbRequest();
request.initialize(usbDeviceConnection, endpointIn);
while (true) {
request.queue(buffer, 2);//自己修改原数字为1
if (usbDeviceConnection.requestWait() == request) {
byte[] rxCmd= new byte[2];
for (int i = 0; i<=2; i++){
rxCmd[i]= buffer.get(2);//想用数组单独读出存放数据,之前也想过rxCmd[0]= buffer.get(0);rxCmd[1]= buffer.get(1);都没有实现;
if(rxCmd[0]!=0){
bar.setProgress((int)rxCmd[0]);//1个bar
progressBar3.setProgress((int)rxCmd[1]);}//1个progressBar3,共2个控件;
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}
}
else {
break;
}

arduino程序可以实现要求:
可以发出2个字节的十六进制。

int prvValue;
#define outputA 6
#define outputB 7
int counter = 1;
int aState;
int aLastState;
byte dataToSent;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
aLastState = digitalRead(outputA);
prvValue = 0;
}
void loop() {
// byte dataToSent;
if(Serial.available()){
byte cmd = Serial.read();
if(cmd == 0x02){
digitalWrite(13, LOW);
}else if(cmd == 0x01){
digitalWrite(13, HIGH);
}
}
aState = digitalRead(outputA); // 这部分程序就是旋转编码器的,发送增量或减量的值,控制在120;Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
aLastState = aState; // Updates the previous state of the outputA with the current state
if (counter>=120){
counter=120;
}//限制最大值,防止超出bar的max值
if (counter<0){
counter=0;}
//限制最小值

  unsigned char data[2] ={counter,22};//这地方是关键,也就是发送2个字节
  Serial.write(data,2);//发送2个字节,目前已经实现:如下图几组数据

}
}

img

3、我想要达到的结果:APP端可以接收2个字节数据,分别放rxCmd[1]和rxCmd[2]。是不是ByteBuffer.allocate(1);request.queue(buffer, 2);rxCmd[i]= buffer.get(2);等改错了还是其它什么原因?

麻烦各位看到这里,刚开始学习,基本都是皮毛,请教!
打算改成发送2个字节,前1个字节设置为设备ID与安卓具体对应相应控件(这部分打算用if语句判断一下十六进制的rxCmd[0]),后面1个字节为设备的具体动态参数(这部分计划设置int rxCmd[1]),这样就可以通过aduino就可以表现多种设备的动态。
目前读出的数据好像都是rxCmd[0]好像把2个字节都赋值了一下,定在后1个字节,无法把2个字节分开。

  • 写回答

1条回答 默认 最新

  • 歇歇 2022-03-25 03:20
    关注

    你要建立多个
    UsbRequest request1 = new UsbRequest();
    UsbRequest request2 = new UsbRequest();
    发送端,两个字节
    截取第一个字节,取得设备号
    调用request1 或者request2 分别操作。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 修改了问题 3月24日
  • 修改了问题 3月24日
  • 展开全部

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改