如何从处理到arduino的串行通信中发送100位数字 [英] how to send 100 digit in serial communication from processing to arduino
问题描述
我正在将像素数组转换为String并将此String发送给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
输出原始二进制值.
Serial.write
outputs raw binary value(s).
Serial.print
输出字符.
Serial.print
outputs character(s).
Serial.println
输出字符和在输出后附加换行符.
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屋!