Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Serial port 从arduino到processing的多个序列值?_Serial Port_Arduino_Communication_Processing_Serial Communication - Fatal编程技术网

Serial port 从arduino到processing的多个序列值?

Serial port 从arduino到processing的多个序列值?,serial-port,arduino,communication,processing,serial-communication,Serial Port,Arduino,Communication,Processing,Serial Communication,我有个小问题 我正在传递来自arduino附加的3轴加速计+3轴磁强计+罗盘航向的信息。这些被缩放到midi范围(0-127) 阿杜伊诺: 它以类似76a45b120c23d12e23f34g的格式以串行打印方式传递 Serial.print(shiftAx); Serial.print("a"); Serial.print(shiftAy); Serial.print("b"); Serial.print(shiftAz); Serial.print("c"); S

我有个小问题

我正在传递来自arduino附加的3轴加速计+3轴磁强计+罗盘航向的信息。这些被缩放到midi范围(0-127)

阿杜伊诺: 它以类似76a45b120c23d12e23f34g的格式以串行打印方式传递

  Serial.print(shiftAx);
  Serial.print("a");
  Serial.print(shiftAy);
  Serial.print("b");
  Serial.print(shiftAz);
  Serial.print("c");
  Serial.print(shiftMx);
  Serial.print("d");
  Serial.print(shiftMy);
  Serial.print("e");
  Serial.print(shiftMz);
  Serial.print("f");
  Serial.print(shiftHead);
  Serial.print("g");
我可以通过我的串行监视器看到这一点。(但我不确定是否在“g”处打印为println。)

处理2:

我缓冲到g 在我的void设置()中

我有这个功能

    void serialEvent (Serial port)
{

  data = port.readStringUntil('g');

  AxVal = data.substring(0, data.indexOf('a'));

  AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b"));  

  AzVal = data.substring(data.indexOf("b") + 1, data.indexOf("c"));

  MxVal = data.substring(data.indexOf("c") + 1, data.indexOf("d"));

  MyVal = data.substring(data.indexOf("d") + 1, data.indexOf("e"));

  MzVal = data.substring(data.indexOf("e") + 1, data.indexOf("f"));

  HeadVal = data.substring(data.indexOf("f") + 1, data.indexOf("g"));

}
问题 所以这不起作用。没有显示文本。(这只是一个简单的填充(),text())

我不明白为什么。这个问题是我的协议(如果我可以这么说的话)的问题,我是如何解包字符串的?或者其他一些问题

我很困惑

我可以让它只与两个值一起工作,就像在这里一样

注: arduino主回路开启延迟(100); arduino的波特率为9600波特

如果需要帮助,我会附上整个草图,我会事先表示感谢

代码:

加工

import themidibus.*; //Import the library
import javax.sound.midi.MidiMessage; //Import the MidiMessage classes http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/midi/MidiMessage.html
import javax.sound.midi.SysexMessage;
import javax.sound.midi.ShortMessage;
import processing.serial.*;

MidiBus myBus; // The MidiBus

Serial port;

String AxVal = " ", AyVal = " ", AzVal = " ";
String MxVal = " ", MyVal = " ", MzVal = " ";
String HeadVal = " ";
String AxString="Ax",AyString = "Ay",AzString = "Az";
String MxString="Mx",MyString = "My",MzString = "Mz";
String HeadString="Heading";
String data = " ";
PFont font;


int status_byte = 0xB0; //  send control change
  int channel_byte = 0; // On channel 0 
  int first_byte; // cc number;
  int second_byte; // value

void setup()
{
  size(1000,500);
  port = new Serial(this, "/dev/tty.usbmodem411", 9600);
  port.bufferUntil('g'); 
  font = loadFont("NanumBrush-48.vlw");
  textFont(font, 48);

    MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
  myBus = new MidiBus(this, 1, 0); // Create a new MidiBus object

}

void draw()
{
  background(0,0,0);

  //first_byte = 1;
  //second_byte = int(AxVal); // But with less velocity
  //myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(46, 209, 2);
  text(AxVal, 60, 75);
  text(AxString, 60, 125);

  //first_byte = 2;
  //second_byte = int(AyVal); // But with less velocity
  //myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(0, 160, 153);
  text(AyVal, 120, 75);
  text(AyString,120,125);



 // first_byte = 3;
  //second_byte = int(AzVal); // But with less velocity
  //myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(0, 160, 153);
  text(AzVal, 180, 75);
  text(AzString,180,125);

  /*
  first_byte = 4;
  second_byte = int(MxVal); // But with less velocity
  myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(0, 160, 153);
  text(MxVal, 240, 75);
  text(MxString,240,125);

  first_byte = 5;
  second_byte = int(MyVal); // But with less velocity
  myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(0, 160, 153);
  text(MyVal, 300, 75);
  text(MyString,300,125);

  first_byte = 6;
  second_byte = int(MzVal); // But with less velocity
  myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(0, 160, 153);
  text(MzVal, 360, 75);
  text(MzString,360,125);

  first_byte = 7;
  second_byte = int(HeadVal); // But with less velocity
  myBus.sendMessage(status_byte, channel_byte, first_byte, second_byte);
  fill(0, 160, 153);
  text(HeadVal, 420, 75);
  text(HeadString,420,125);

  */

}

void serialEvent (Serial port)
{

  data = port.readStringUntil('g');
  data = data.substring(0, data.length() - 1);

  AxVal = data.substring(0, data.indexOf('a'));


  AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b"));


  AzVal = data.substring(data.indexOf("b") + 1, data.length());

 /* 
  index = data.indexOf("c")+1;
  MxVal = data.substring(index, data.indexOf("d"));

  index = data.indexOf("d")+1;
  MyVal = data.substring(index, data.indexOf("e"));

  index = data.indexOf("e")+1;
  MzVal = data.substring(index, data.indexOf("f"));

  index = data.indexOf("f")+1;
  HeadVal = data.substring(index, data.indexOf("g"));
  */

}
/*
void serialEvent (Serial port)
{

  data = port.readStringUntil('g');

  AxVal = data.substring(0, data.indexOf('a'));

  AyVal = data.substring(data.indexOf("a") + 1, data.indexOf("b"));  

  AzVal = data.substring(data.indexOf("b") + 1, data.indexOf("c"));

  MxVal = data.substring(data.indexOf("c") + 1, data.indexOf("d"));

  MyVal = data.substring(data.indexOf("d") + 1, data.indexOf("e"));

  MzVal = data.substring(data.indexOf("e") + 1, data.indexOf("f"));

  HeadVal = data.substring(data.indexOf("f") + 1, data.indexOf("g"));

}
阿杜伊诺:

// Add lastvalue check

#include <Wire.h>
#include <LSM303DLH.h>

LSM303DLH glove;

 //set max min magnetometer
        int maxMx = +353, maxMy = +527, maxMz = 426;
    int minMx = -700, minMy = -477, minMz = -561;

        int maxA = 2019;
    int minAx = -1043, minAy = -2048, minAz = -2048;

        int shiftMx,shiftMy,shiftMz;
        int shiftAx,shiftAy,shiftAz;    
        float shiftHeadTemp;
        int shiftHead;  



void setup()
{
 Wire.begin();
    glove.enableDefault();
   Serial.begin(9600);
}

void loop()
{



 glove.read();


    shiftMx = ((glove.m.x - minMx) / (maxMx - minMx)) * 127;
    shiftMy = ((glove.m.y - minMy) / (maxMy - minMy)) * 127;
    shiftMz = ((glove.m.z - minMz) / (maxMz - minMz)) * 127;

    shiftAx = ((glove.a.x - minAx) / (maxA - minAx)) * 127;
    shiftAy = ((glove.a.y - minAy) / (maxA - minAy)) * 127;
    shiftAz = ((glove.a.z - minAz) / (maxA - minAz)) * 127;

    shiftHeadTemp = (glove.heading((LSM303DLH::vector){0,-1,0}));

    shiftHead =  (shiftHeadTemp/360)*127; 

    if (shiftMx < 0){shiftMx=0;}
    if (shiftMx >127){shiftMx=127;}
    if (shiftMy < 0){shiftMy=0;}
    if (shiftMy >127){shiftMy=127;}
    if (shiftMz < 0){shiftMz=0;}
    if (shiftMz >127){shiftMz=127;}

    if (shiftAx < 0){shiftAx=0;}
    if (shiftAx >127){shiftAx=127;}
    if (shiftAy < 0){shiftAy=0;}
    if (shiftAy >127){shiftAy=127;}
    if (shiftAz < 0){shiftAz=0;}
    if (shiftAz >127){shiftAz=127;}

    if (shiftHead < 0){shiftHead=0;}
    if (shiftHead >127){shiftHead=127;}




  Serial.print(shiftAx);
  Serial.print("a");
  Serial.print(shiftAy);
  Serial.print("b");
  Serial.print(shiftAz);
  Serial.print("c");
  Serial.print(shiftMx);
  Serial.print("d");
  Serial.print(shiftMy);
  Serial.print("e");
  Serial.print(shiftMz);
  Serial.print("f");
  Serial.print(shiftHead);
  Serial.println("g");



  delay(100);


}
//添加上次值检查
#包括
#包括
LSM303DLH手套;
//设置最大最小磁强计
int maxMx=+353,maxMy=+527,maxMz=426;
int minMx=-700,minMy=-477,minMz=-561;
int maxA=2019;
int minAx=-1043,minAy=-2048,minAz=-2048;
int shiftMx、shiftMy、shiftMz;
西夫塔克斯,西夫塔伊,西夫塔兹;
浮动移位时间;
int-shiftHead;
无效设置()
{
Wire.begin();
glove.enableDefault();
Serial.begin(9600);
}
void循环()
{
手套。阅读();
shiftMx=((glove.m.x-minMx)/(maxMx-minMx))*127;
shiftMy=((glove.m.y-minMy)/(maxMy-minMy))*127;
shiftMz=((globate.m.z-minMz)/(maxMz-minMz))*127;
shiftAx=((手套a.x-minAx)/(maxA-minAx))*127;
shiftAy=((手套a.y-minAy)/(maxA-minAy))*127;
shiftAz=((手套a.z-minAz)/(maxA-minAz))*127;
shiftheadatemp=(globle.heading((LSM303DLH::vector){0,-1,0}));
shiftHead=(shiftheadatemp/360)*127;
如果(shiftMx<0){shiftMx=0;}
如果(shiftMx>127){shiftMx=127;}
如果(shiftMy<0){shiftMy=0;}
如果(shiftMy>127){shiftMy=127;}
如果(shiftMz<0){shiftMz=0;}
如果(shiftMz>127){shiftMz=127;}
如果(shiftAx<0){shiftAx=0;}
如果(shiftAx>127){shiftAx=127;}
如果(shiftAy<0){shiftAy=0;}
如果(shiftAy>127){shiftAy=127;}
如果(shiftAz<0){shiftAz=0;}
如果(shiftAz>127){shiftAz=127;}
如果(shiftHead<0){shiftHead=0;}
如果(shiftHead>127){shiftHead=127;}
连续打印(税务);
连续打印(“a”);
串行打印(shiftAy);
连续打印(“b”);
串行打印(shiftAz);
连续打印(“c”);
串行打印(shiftMx);
连续打印(“d”);
串行打印(shiftMy);
连续打印(“e”);
串行打印(shiftMz);
连续打印(“f”);
串行打印(shiftHead);
序列号。打印号(“g”);
延迟(100);
}

我现在无法测试您的代码,但这里有一些想法

您可以从Arduino向processing发送JSON。以下是我在自己的一个项目中使用的示例,但您可以轻松地对其进行调整以满足您的需要:

void sendJson(){
    String json;

    json = "{\"accel\":{\"x\":";
    json = json + getXYZ(0);
    json = json + ",\"y\":";
    json = json + getXYZ(1);
    json = json + ",\"z\":";
    json = json + getXYZ(2);
    json = json + "},\"gyro\":{\"yaw\":";
    json = json + getYPR(0);
    json = json + ",\"pitch\":";
    json = json + getYPR(1);
    json = json + ",\"roll\":";
    json = json + getYPR(2);
    json = json + "}}";

    Serial.println(json);
}
JSON的优点在于,使用
getType()
函数处理JSON很容易“解析”。不再使用
readUntil
,一切都是这样的:(请注意,在
void setup()
中,您需要将
port.bufferUntil('g');
更改为
port.bufferUntil('\n');
,因为在Arduino中,
json
Serial.println();
一起发送)

您将不得不稍微更改代码,但从长远来看,如果您添加更多传感器,代码将更易于维护。您只需在arduino中更改
json
,并在处理过程中获取值即可

另一件事:可以使用约束函数将值约束在
0
127
之间

value = constrain(value, 0, 127);

希望有帮助

我在c#中遇到了同样的问题(第二个参数,在c#中是减法字符串的长度)。然而,我看到如果我不等待一点时间,传输的字符串并不总是以相同的形式:例如:somtime,您的字符串将是(值)a(值)b(值)c(值)d(值)e(值)f(值)g,但其他时间将是:(值)a(值)b(值)c(值)d(值),下一个字符串将是e(值)f(值)g(值)a(值)b(值)等等毕竟,所有的数据都会被传输,但它的结构不同,所以我在每次串行读取之前都会增加一点延迟(以确保前一个串行字符串被读取,直到and和每个timpe以相同的形式获得字符串),这对我来说很好。希望这对你也有帮助。

这正是我想要的。更好的方法。谢谢。编辑*我会投票,但没有代表,有代表的人,给这个人一些代表
void serialEvent (Serial port){
        String json = port.readString();

        shiftAx = getInt("shiftAx");
        shiftAy = getInt("shiftAy"); // and so on...
}
value = constrain(value, 0, 127);