如何在从处理到arduino的串行通信中发送100位数字 [英] how to send 100 digit in serial communication from processing to arduino

查看:18
本文介绍了如何在从处理到arduino的串行通信中发送100位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将像素数组转换为字符串并将此字符串发送到 arduino.但我认为这个字符串没有正确转换,因为 Serial.write 发送(8 位或 8 个字符)我不知道.并且还想将 100 个字符的字符串发送到 Serial .请发送您的建议并帮助我解决这个问题.如有任何错误,请提前道歉.

i am converting pixel array to String and send this String to arduino. but i Think this String is not converted properly because Serial.write send (8 bit or 8 character) i don’t know. and also want to send 100 character of string into Serial . please send your suggestion and getting help me out of this problem. for any mistake Sorry in advance.

  for(int x =0 ; x < img.height; x++)
   {
     for(int y=0; y <img.width; y++)
      {
        int i = x+y*width;
        if(img.pixels[i] == color(0,0,0))
       {
          i=1;
       }
       else
       {
        i=0;
       }

      String s = str(i);
      print(s);
      Serial.write(s);
      delay(2);
 }
}           

并告诉我如何通过不使用 ("\n" 或 "\r" ) 来停止 100 个字符后的字符串

and also tell me how to stop string after 100 character by not using ("\n" or "\r" )

推荐答案

您似乎正在寻找以下代码:

It seems you are looking for the code below:

for (int x = 0; x < img.height; x++) { // iterate over height
  for (int y = 0; y < img.width; y++) { // iterate over width
    int i = x + y * width;
    if (img.pixels[i] == color(0, 0, 0)) { // determine if zero
      Serial.write('1'); // send non zero char
    } else {
      Serial.write('0'); // send zero char
    }
  }
  Serial.write("\n\r");
}

如果您想以 img.width 的大小为单位对输出进行聚类,您可以这样做:

If you want to cluster your output in units the size of img.width you could do this:

for (int x = 0; x < img.height; x++) { // iterate over height
  String s;
  for (int y = 0; y < img.width; y++) { // iterate over width
    int i = x + y * width;
    if (img.pixels[i] == color(0, 0, 0)) { // determine if zero
      s += '1'; // append a non zero char to string s
    } else {
      s += '0'; // append a zero char to string s
    }
  }
  Serial.println(s);
}

请记住:

Serial.write 输出原始二进制值(s).

Serial.write outputs raw binary value(s).

Serial.print 输出字符.

Serial.println 输出字符and 在输出中附加一个换行符.

Serial.println outputs character(s) and appends a newline character to output.


我对这个计算有严重的怀疑 int i = x+y*width; 因为你的数据结构可能是:


I have serious doubts about this calculation int i = x+y*width; as your data is probably structured as:

vertical data:      0      1      2
horizontal data:    [row 0][row 1][row 2]

代替:

horizontal data:    0         1         2
vertical data:      [column 0][column 1][column 2]

这篇关于如何在从处理到arduino的串行通信中发送100位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆