Arduino:无法从struct数组中获取值

时间:2013-05-12 20:09:32

标签: arrays struct serial-port arduino

我有一个我定义的二维结构数组,我想在结构中获取一个值,该结构本身包含在数组中。

我试图在裸C中执行相同类型的代码,它似乎可以工作,而Arudino IDE却没有(代码不完全相同,但通常是相同的)。

看看下面的例子,我正在打印从我的struct over serial中取出的任意值,我在串行监视器上得到的唯一输入是'ÿ'的套装,从我在上面看到的internet表示串行缓冲区为空。

在下面的代码中,您的任何内容都有问题吗? (看结尾看相关代码)

#include <Wire.h>

typedef struct {
  char nom[17]; // wanted number +1 for terminaison
  byte cor1[2]; // correspondance
  byte cor2[2];
  byte cor3[2];
  byte cor4[2];
  byte i2c;
  byte led;
} Station;

Station lignes[14][15] = {{
  {"Champs Elysees", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11101111},
  {"Concorde", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B10111111},
  {"Tuileries", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11101111},
  {"Palais Royal", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B10111111},
  {"Louvre Rivoli", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11101111},
  {"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
  {"Hotel de Ville", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11101111},
  {"St-Paul", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
  {"Bastille", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Ledru-Rollin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Faidherbe", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011}
},
{
},
{
  {"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
  {"Havre Caumartin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111110},
  {"Opera", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11110111},
  {"Quatre Septembre", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11111101},
  {"Bourse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11011111},
  {"Sentier", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11110111},
  {"R. Sebastopol", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111101},
  {"Arts et Metiers", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B01111111},
  {"Temple", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B10111111},
  {"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111}
},
{
  {"S. Saint-Denis", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11011111},
  {"R. Sebastopol", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111101},
  {"Etienne Marcel", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111011},
  {"Les Halles", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B10111111},
  {"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
  {"Cite", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B01111111},
  {"St-Michel", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110},
  {"Odeon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
  {"St-Germain", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B10111111},
  {"Saint-Sulpice", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
  {"St-Placide", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
  {"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
  {"Vavin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
  {"Raspail", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Denfert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
  {"J. Bonsergent", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11111011},
  {"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
  {"Oberkampf", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B01111111},
  {"Richard Lenoir", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110},
  {"Breguet Sabin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
  {"Bastille", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Quai de la Rapee", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
  {"Gare Austerlitz", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
  {"Saint Marcel", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
  {"Campo Formio", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
},
{
  {"Pasteur", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
  {"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
  {"Edgar Quinet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
  {"Raspail", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Denfert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
  {"Le Peletier", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11110111},
  {"La Fayette", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111011},
  {"Opera", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11110111},
  {"Pyramides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B01111111},
  {"Palais Royal", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B10111111},
  {"Pont Neuf", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11011111},
  {"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
  {"Pont Marie", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
  {"Sully Morland", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110},
  {"Jussieu", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Place Monge", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"C. Daubenton", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Les Gobelins", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
},
{
  {"Invalides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11110111},
  {"Concorde", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B10111111},
  {"Madeleine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11011111},
  {"Opera", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11110111},
  {"Richelieu Drouot", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111101},
  {"Gds Boulevards", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11111011},
  {"Bonne Nouvelle", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111110},
  {"S. Saint-Denis", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11011111},
  {"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
  {"F. du Calvaire", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11101111},
  {"St-S. Froissart", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B01111111},
  {"Chemin Vert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
  {"Bastille", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Ledru Rollin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Faidherbe", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011}
},
{
  {"Saint Philippe", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11110111},
  {"Miromesnil", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111011},
  {"Saint-Augustin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111101},
  {"Havre Caumartin", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111110},
  {"La Fayette", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111011},
  {"Richelieu Drouot", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100110, B11111101},
  {"Gds Boulevards", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100010, B11111011},
  {"Bonne Nouvelle", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11111110},
  {"S. Saint-Denis", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11011111},
  {"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
  {"Oberkampf", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B01111111}
},
{
  {"Duroc", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Vanneau", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
  {"Sevres Babylone", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Mabillon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B01111111},
  {"Odeon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
  {"La Sorbonne", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111101},
  {"Maubert", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
  {"Cardinal Lemoine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B01111111},
  {"Jussieu", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Gare Austerlitz", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
  {"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
  {"Hotel de Ville", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11101111},
  {"Rambuteau", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B11011111},
  {"Arts et Metiers", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B01111111},
  {"Republique", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11110111},
  {"Goncourt", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11111101},
  {"Belleville", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100000, B11111110}
},
{
  {"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
  {"Madeleine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11011111},
  {"Concorde", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B10111111},
  {"Assemblee", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11111011},
  {"Solferino", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11111110},
  {"Rue du Bac", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11101111},
  {"Sevres Babylone", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111011},
  {"Rennes", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
  {"N-D. Des-Champs", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
  {"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
  {"Falguiere", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B10111111},
  {"Pasteur", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11011111},
  {"Volontaires", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111}
},
{
  {"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
  {"Miromesnil", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111011},
  {"Champs Elysees", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11101111},
  {"Invalides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11110111},
  {"Varenne", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11111101},
  {"St-Fr. Xavier", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100101, B11011111},
  {"Duroc", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111},
  {"Montparnasse", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11101111},
  {"Gaite", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11111110}
},
{
  {"Saint-Lazare", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11111110},
  {"Madeleine", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B11011111},
  {"Pyramides", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100001, B01111111},
  {"Chatelet", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0100100, B10111111},
  {"Gare de Lyon", {0, 0}, {0, 0}, {0, 0}, {0, 0}, B0111000, B11110111}
}};


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

void loop(){
//  for (int j=0; j<16; j++) {
  byte a = lignes[0][0].i2c;
  Serial.write(a);
/*  for (int i=0; i<14; i++) {
    Serial.print(lignes[0][i].i2c, BIN);
    Serial.print(lignes[0][i].led, BIN);
    Serial.println(" ");

/*    Wire.beginTransmission(lignes[0][i].i2c); // PCF8574P GGG
    Wire.write(lignes[0][i].led);
    Wire.endTransmission();
    delay(10);*/
//  }
// }
}

非常感谢你的帮助,我已经花了很多时间在这上面......

2 个答案:

答案 0 :(得分:2)

在您给出的示例中,您正在编写“lignes [0] [0] .i2c”,即B0100001。这是一个ASCII空间代码。尝试使用简单的

验证串行连接
Serial.Write("Hello World");

如果通过,则尝试打印实际字符串,例如

Serial.Write(&lignes[0][0].nom)

答案 1 :(得分:1)

显然,这是由内存溢出引起的(感谢Arduino IDE没有指出)。

Arduino IDE说36kB可用,但那是闪存,需要考虑SRAM。

使用PROGMEM可以解决这个问题。

比照http://arduino.cc/en/Tutorial/Memory http://www.arduino.cc/en/Reference/PROGMEM

另外要注意,你必须使用特殊功能来提取和使用与PROGMEM一起存储的值。