与泼妇的arduino串行通信

时间:2014-11-27 13:44:43

标签: arduino

我正在尝试使用arduino通过串行通信从vixen软件中获取传入数据。并将数据移动到其他12个arduino如果我将频道号码从vixen保持到43但我需要控制480个频道。所以第一个arduino控制43个通道,下一个是44-87,依此类推。

这是我的主代码: `void setup() {   Serial.begin(9600);

  Serial3.begin(9600); 
}

void loop()
{ 
   // read from port 0, send to port 1:
   if (Serial.available()) {
   int inByte = Serial.read();
   Serial3.write(inByte); 

} } this is the slave code:     //输出

int cue1 = 3;
int cue2 = 4;
int cue3 = 5;
int cue4 = 6;
int cue5 = 7;
int cue6 = 8;
int cue7 = 9;
int cue8 = 10;
int cue9 = 11;
int cue10 = 12;
int cue11 = 13;
int cue12 = 22;
int cue13 = 23;
int cue14 = 24;
int cue15 = 25;
int cue16 = 26;
int cue17 = 27;
int cue18 = 28;
int cue19 = 29;
int cue20 = 30;
int cue21 = 31;
int cue22 = 32;
int cue23 = 33;
int cue24 = 34;
int cue25 = 35;
int cue26 = 36;
int cue27 = 37;
int cue28 = 38;
int cue29 = 39;
int cue30 = 40;
int cue31 = 41;
int cue32 = 42;
int cue33 = 43;
int cue34 = 44;
int cue35 = 45;
int cue36 = 46;
int cue37 = 47;
int cue38 = 48;
int cue39 = 49;
int cue40 = 50;
int cue41 = 51;
int cue42 = 52;
int cue43 = 53;


 int i = 0;
 int incomingByte[43];


  void setup() // run once, when the sketch starts

  {
     Serial3.begin(9600);
     pinMode(cue1, OUTPUT); // sets the digital pin as output
     pinMode(cue2, OUTPUT); // sets the digital pin as output
     pinMode(cue3, OUTPUT); // sets the digital pin as output
     pinMode(cue4, OUTPUT); // sets the digital pin as output
     pinMode(cue5, OUTPUT); // sets the digital pin as output
     pinMode(cue5, OUTPUT); // sets the digital pin as output
     pinMode(cue6, OUTPUT); // sets the digital pin as output
     pinMode(cue7, OUTPUT); // sets the digital pin as output
     pinMode(cue8, OUTPUT); // sets the digital pin as output
     pinMode(cue9, OUTPUT); // sets the digital pin as output
     pinMode(cue10, OUTPUT); // sets the digital pin as output
     pinMode(cue11, OUTPUT); // sets the digital pin as output
     pinMode(cue12, OUTPUT); // sets the digital pin as output
     pinMode(cue13, OUTPUT); // sets the digital pin as output
     pinMode(cue14, OUTPUT); // sets the digital pin as output
     pinMode(cue15, OUTPUT); // sets the digital pin as output
     pinMode(cue16, OUTPUT); // sets the digital pin as output
     pinMode(cue17, OUTPUT); // sets the digital pin as output
     pinMode(cue18, OUTPUT); // sets the digital pin as output
     pinMode(cue19, OUTPUT); // sets the digital pin as output
     pinMode(cue20, OUTPUT); // sets the digital pin as output
     pinMode(cue21, OUTPUT); // sets the digital pin as output
     pinMode(cue22, OUTPUT); // sets the digital pin as output
     pinMode(cue23, OUTPUT); // sets the digital pin as output
     pinMode(cue24, OUTPUT); // sets the digital pin as output
     pinMode(cue25, OUTPUT); // sets the digital pin as output
     pinMode(cue26, OUTPUT); // sets the digital pin as output
     pinMode(cue27, OUTPUT); // sets the digital pin as output
     pinMode(cue28, OUTPUT); // sets the digital pin as output
     pinMode(cue29, OUTPUT); // sets the digital pin as output
     pinMode(cue30, OUTPUT); // sets the digital pin as output
     pinMode(cue31, OUTPUT); // sets the digital pin as output
     pinMode(cue32, OUTPUT); // sets the digital pin as output
     pinMode(cue33, OUTPUT); // sets the digital pin as output
     pinMode(cue34, OUTPUT); // sets the digital pin as output
     pinMode(cue35, OUTPUT); // sets the digital pin as output
     pinMode(cue36, OUTPUT); // sets the digital pin as output
     pinMode(cue37, OUTPUT); // sets the digital pin as output
     pinMode(cue38, OUTPUT); // sets the digital pin as output
     pinMode(cue39, OUTPUT); // sets the digital pin as output
     pinMode(cue40, OUTPUT); // sets the digital pin as output
     pinMode(cue41, OUTPUT); // sets the digital pin as output
     pinMode(cue42, OUTPUT); // sets the digital pin as output
     pinMode(cue43, OUTPUT); // sets the digital pin as output

     }

      void loop()
    {
       if (Serial3.available() >=43)
    {
      for (int i=0; i<43;i++)
     {
       incomingByte[i] = Serial3.read();
        }                                      
     digitalWrite(cue1, incomingByte[0]);  
     digitalWrite(cue2, incomingByte[1]);  
     digitalWrite(cue3, incomingByte[2]);  
     digitalWrite(cue4, incomingByte[3]);  
     digitalWrite(cue5, incomingByte[4]);  
     digitalWrite(cue6, incomingByte[5]);  
     digitalWrite(cue7, incomingByte[6]);  
     digitalWrite(cue8, incomingByte[7]);  
     digitalWrite(cue9, incomingByte[8]);  
     digitalWrite(cue10, incomingByte[9]);  
     digitalWrite(cue11, incomingByte[10]); 
     digitalWrite(cue12, incomingByte[11]); 
     digitalWrite(cue13, incomingByte[12]); 
     digitalWrite(cue14, incomingByte[13]); 
     digitalWrite(cue15, incomingByte[14]); 
     digitalWrite(cue16, incomingByte[15]);  
     digitalWrite(cue17, incomingByte[16]);  
     digitalWrite(cue18, incomingByte[17]);  
     digitalWrite(cue19, incomingByte[18]);  
     digitalWrite(cue20, incomingByte[19]);  
     digitalWrite(cue21, incomingByte[20]);  
     digitalWrite(cue22, incomingByte[21]);  
     digitalWrite(cue23, incomingByte[22]);  
     digitalWrite(cue24, incomingByte[23]);  
     digitalWrite(cue25, incomingByte[24]);  
     digitalWrite(cue26, incomingByte[25]); 
     digitalWrite(cue27, incomingByte[26]); 
     digitalWrite(cue28, incomingByte[27]); 
     digitalWrite(cue29, incomingByte[28]); 
     digitalWrite(cue30, incomingByte[29]); 
     digitalWrite(cue31, incomingByte[30]);  
     digitalWrite(cue32, incomingByte[31]);  
     digitalWrite(cue33, incomingByte[32]);  
     digitalWrite(cue34, incomingByte[33]);  
     digitalWrite(cue35, incomingByte[34]);  
     digitalWrite(cue36, incomingByte[35]);  
     digitalWrite(cue37, incomingByte[36]);  
     digitalWrite(cue38, incomingByte[37]);  
     digitalWrite(cue39, incomingByte[38]);  
     digitalWrite(cue40, incomingByte[39]);  
     digitalWrite(cue41, incomingByte[40]); 
     digitalWrite(cue42, incomingByte[41]); 
     digitalWrite(cue43, incomingByte[42]); 

       }
      }`

1 个答案:

答案 0 :(得分:0)

首先,你实际上没有提出问题。这个论坛的形式不好。

看到你已经基本上复制了典型的Vixen代码并在Mega上添加了它,我可以看到几个需要寻址的区域。如何扩展到多个单位,框架和缓冲区大小。

Arduino从Vixen接收的典型例子似乎越来越多。可怕的是,它不构成提示流的开始或结束。相反,它需要流和保持同步,这取决于长度。这将一直有效,直到丢失一个字节。然后一切都将不同步,直到每个组件重置并重新启动。你不想用多个Arduino做的事情。

以下代码通过配置Vixen的通用串行端口设置来增强此功能,以启用发送单个字母“E”的“发送文本页脚”。

Vixen General Port Set Up 2.0

利用在此实现中输出提示仅为布尔值。开启或关闭。 Vixen将为序列中的每个cue发送0x00或0xFF。因此,启用上面的页脚,Vixen将使用字节“E”或0x45(十六进制)终止每个序列。通常,对于强度,提示的范围是0x00到0xFF。如果是这种情况,则需要更复杂的页脚,因为0x45可能是合法的值。在这种情况下,'E'可以很好地工作。

#define FIRST_IN_CHAIN 1 // set this to 1 for the first in Chain
#define SIZE_OF_ARRAY(X) (sizeof(X)/sizeof(X[0])) //total length divided by length of one element

// As specified in Vixen's General Serial Port Setup "Send a Text Footer"
#define VIXEN_END_OF_TEXT 'E'

#if (FIRST_IN_CHAIN == 1)
  #define SERIALIN Serial
#else
  #define SERIALIN Serial2
#endif

// General GPIO used to output Cues
uint8_t pins[] = {3,4,5,6,7,8,9,10,11,12,13,\
  // not used SerialX ports
  // 14,15, \ // TXD3/RXD3
  // 16,17, \ // TXD2/RXD2
  // 18,19, \ // TXD1/RXD1
  // 20,21, \ // SDA/SCL
  22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53};

void setup() {
  SERIALIN.begin(9600);
  Serial3.begin(9600);

  // initialize cue pins OFF and Output
  for (uint8_t pin = 0; pin < SIZE_OF_ARRAY(pins); pin++) {
    digitalWrite(pins[pin], LOW);
    pinMode(pins[pin], OUTPUT);
  }
}

void loop()
{
  // get first 43 cues for this Arduino
  for (uint8_t pin = 0; pin < SIZE_OF_ARRAY(pins); pin++) {
    while(!SERIALIN.available()); // wait for some data.
    char c = SERIALIN.read();
    if(c == VIXEN_END_OF_TEXT) {
      return;
    }
    else if(c > 0) {
      digitalWrite(pins[pin], HIGH);
    } 
    else {
      digitalWrite(pins[pin], LOW);
    }
  }

  // read from port 0, send to port 1:
  while(1) {
    while(!SERIALIN.available()) ; // wait for next data.
   // echo byte on to the next stage
    char c = SERIALIN.read();
    Serial3.write(c); 
    if(c == VIXEN_END_OF_TEXT) {
      // when Carriage Return is received Exit while and wait for new frame 
      break;
    }
  }
}

现在这并不完美,因为我没有Vixen这样的设置,所以还没有得到证实。但是,它确实编译并显示它应该工作正常。

配置为“E”的页脚.Vixen发出表示每个Cue的字节流,用“E”终止该帧。因此,上面的草图会侦听前43个提示并将它们应用到本地引脚。如果脚本不按顺序失败并且无意中检测到“E”的页脚,则返回loop()以启动a-new。如果成功接收到本地应用的所有提示,则接着将剩余的提示串口3(TXD3)回送到下一个Arduino的Serial2(RXD2),直到收到表示序列结束的页脚“E”。注意它从头开始。请注意,本地应用的cue不会被回应,这意味着下一个Arduino将再次对它接收的下一个43或前43个提示进行操作并回显其余部分,直到链被耗尽。

enter image description here


很少有兴趣点

请注意

的使用
    while(!SERIALIN.available()); // wait for some data.

哪个有效“等待没有数据可读”或等到有什么东西。这些是阻塞(意味着代码被卡住),直到收到某些东西。而预计Vixen将定期发送提示流。即使错过了一个'E',下一帧也会包含它,这样代码就会自动重新同步。

另外值得注意的是,提示定义的引脚更灵活。如果您想使用47个提示,请取消评论:

  // 18,19, \ // TXD1/RXD1
  // 20,21, \ // SDA/SCL

代码会自动调整大小。如果用于其他目的,请注释掉其他引脚。

值得注意的是,9600波特每秒约为960字节。这意味着序列只能每秒更新两次或大约2 Hz。菊花链会增加明显的传播延迟和Arduino之间的差异。 Vixen 2的最大速率可配置为57600波特,可产生约12 Hz的频率。因此,我建议将Vixen和Arduino配置为至少57600波特。尽量减少这些影响。 Vixen 3实际上可以更快。我建议115200波特或24赫兹。

总结 第一个单元使用FIRST_IN_CHAIN定义为1.这使得主ArduinoMega从连接到运行Vixen的PC的串行端口接收数据。其余的Mega编译为FIRST_IN_CHAIN定义为0.其中前一个单元的Serial3连接到下一个单元Serial2。可以开车进入Serial。保留串口进行调试等更简单的地方。 Vixen配置为“E”脚。

此解决方案中缓冲区大小不是问题,因为每个字节都是单独处理的。不等待它们全部在Arduino的Serial.read()缓冲区中构建。哪个会超过62。