samedi 18 février 2017

Industruino + Ethernet + Labview

Linx ne permet pas de connexion en ethernet. Seule l'USB est possible.

A terme, nous utiliserons l'ethernet pour communiquer. Pour cela, le plus adapté pour nous sera d'utiliser le protocole UDP très rapide et simple. Il est cependant moins fiable que le protocole TCP/IP mais la fiabilité n'est pas le plus important dans notre cas.

Un hongrois a fait une partie du boulot, en créant une interface Labview pour Arduino Ethernet. Travail simple et propre à tester avec une Arduino et son shield ethernet puis avec l'industruino.
Le site en question

Le code Arduino à téléverser :
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>   

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 177);

unsigned int localPort = 8888;   

char packetBuffer[6]; 
char  ReplyBuffer[5];      

String back;

int pin;
int value;

EthernetUDP Udp;

void setup() 
{
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() 
{
  int packetSize = Udp.parsePacket();
  
  if(packetSize)
  {
    
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    
    int cs = packetBuffer[0];
    
    switch (cs) {
     case 'a':
     {
      value = analogRead(packetBuffer[1] - '0');
      itoa(value, ReplyBuffer, 10);     
     }
      break;
     case 'p':
     {
       pin = packetBuffer[1]-'0';       
       value = packetBuffer[2]*100 + packetBuffer[3]*10 + packetBuffer[4] - 111*'0';

       analogWrite(pin, value);
      
       back = "OK00";
       back.toCharArray(ReplyBuffer, 5);
     }
      break;
     case 'w':
     {
       pin = packetBuffer[1]-'0';
       value = packetBuffer[2]-'0';  
              
       pinMode(pin, OUTPUT);
       digitalWrite(pin, value);
       
       back = "OK00";
       back.toCharArray(ReplyBuffer, 5);     
     }
      break;
     case 'r':
     {
       pin = packetBuffer[1]-'0';
       
       pinMode(pin, INPUT);
       
       itoa(digitalRead(pin), ReplyBuffer, 10);           
     }
      break;
     default: 
     {
       back = "ERR0";
       back.toCharArray(ReplyBuffer, 5);       
     }
  }    
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
}

Il faut ensuite installer une toolbox dans Labview :
Placer arduino_ethernet.mnu dans Program Files\National Instruments\Labview 201x\menus\Categories
Dezipper et placer le dossier arduino_ethernet dans Program Files\National Instruments\Labview 201x\vi.lib

Ouvrir labview, une sous palette arduino ethernet apparait.


Aucun commentaire:

Enregistrer un commentaire