Receiving Raw Ethernet VSCP events with Arduino

This sample print incoming VSCP Raw Ethernet frames on the serial line of the Arduino.

The VSCP Level II Raw Ethernet Driver makes it possible to receive and send VSCP events packed in raw Ethernet frames. This means that there is no need for a TCP/IP stack and the program code occupied by this subsystem code can instead be used for other purposes.

Warning Frames are broadcasted over the Ethernet bus and if you start to send a lot of frames you will probably slow down you network. This method is intended for a maximum of a few hundred or so events per second. If you send more use TCP/IP instead.

/*
 Print incoming VSCP Raw Ethernet frames on the serial terminal
*/

#include <SPI.h>         // needed for Ethernet library communication with the W5100 (Arduino ver>0018)
#include <Ethernet.h>
#include <utility/w5100.h>

SOCKET s; // our socket that will be opened in RAW mode
byte rbuf[500+14]; // receive buffer
//byte sbuf[500+14]; // send buffer
int rbuflen; // length of data to receive
int sbuflen; // length of data to send

void setup()
{
  Serial.begin(115200);      // open the serial port at 9600 bps:

  // initialize the w5100 chip and open a RAW socket
  W5100.init();
  W5100.writeSnMR(s, SnMR::MACRAW);
  W5100.execCmdSn(s, Sock_OPEN);
}

void loop()
{
  // check if we have received something
  rbuflen = W5100.getRXReceivedSize(s);

  if ( ( rbuflen > 0 ) && ( rbuflen < sizeof( rbuf ) ) ) {

    // receive packet
    W5100.recv_data_processing(s, rbuf, rbuflen);
    W5100.execCmdSn(s, Sock_RECV);

    // offset 2 ... because the first two bytes in 'rbuf' contain the packet
    // length

    // Is it a VSCP frame?
    if ( ( rbuf[14] != 0x25 ) || ( rbuf[15] != 0x7e ) ) return;

    Serial.println("\n---------------------------------------------------------");
    Serial.println("VSCP Raw Ethernet Frame");
    Serial.println("---------------------------------------------------------");

    Serial.print("Destination MAC = ");
    Serial.print(rbuf[2], HEX);
    Serial.print(":");
    Serial.print(rbuf[3], HEX);
    Serial.print(":");
    Serial.print(rbuf[4], HEX);
    Serial.print(":");
    Serial.print(rbuf[5], HEX);
    Serial.print(":");
    Serial.print(rbuf[6], HEX);
    Serial.print(":");
    Serial.println(rbuf[7], HEX);

    Serial.print("Source MAC = ");
    Serial.print(rbuf[8], HEX);
    Serial.print(":");
    Serial.print(rbuf[9], HEX);
    Serial.print(":");
    Serial.print(rbuf[10], HEX);
    Serial.print(":");
    Serial.print(rbuf[11], HEX);
    Serial.print(":");
    Serial.print(rbuf[12], HEX);
    Serial.print(":");
    Serial.println(rbuf[13], HEX);

    Serial.print("Frame id = ");
    Serial.print(rbuf[14], HEX);
    Serial.println(rbuf[15], HEX);

    Serial.print("Frame version = ");
    Serial.println(rbuf[16], HEX);

    Serial.print("VSCP Head = ");
    Serial.print(rbuf[17], HEX);
    Serial.print(rbuf[18], HEX);
    Serial.print(rbuf[19], HEX);
    Serial.println(rbuf[20], HEX);

    Serial.print("VSCP Sub. Address = ");
    Serial.print(rbuf[21], HEX);
    Serial.print(":");
    Serial.println(rbuf[22], HEX);

    Serial.print("VSCP Timestamp = ");
    Serial.print(rbuf[23], HEX);
    Serial.print(rbuf[24], HEX);
    Serial.print(rbuf[25], HEX);
    Serial.println(rbuf[26], HEX);

    Serial.print("VSCP OBID = ");
    Serial.print(rbuf[27], HEX);
    Serial.print(rbuf[28], HEX);
    Serial.print(rbuf[29], HEX);
    Serial.println(rbuf[30], HEX);

    Serial.print("VSCP Class = ");
    Serial.print(rbuf[31], HEX);
    Serial.println(rbuf[32], HEX);

    Serial.print("VSCP Type = ");
    Serial.print(rbuf[33], HEX);
    Serial.println(rbuf[34], HEX);

    Serial.print("VSCP Data Size= ");
    Serial.print(rbuf[35], HEX);
    Serial.println(rbuf[36], HEX);

    long datasize = rbuf[35]*256 + rbuf[36];

    if ( 0 ==  datasize ) {
      Serial.println("No data");
    }
    else {
      for ( int i=0; i<datasize; i++ ) {
        Serial.print(rbuf[37+i], HEX);
        Serial.print(" ");
      }
      Serial.println("\n---------------------------------------------------------");
    }

  }

}
 
coding/receiving_raw_ethernet_vscp_events_with_arduino.txt · Last modified: 2015/05/13 22:30 by akhe
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki