Camera Control

Hi, I worked out the issue, the SD card needed to be turned of by setting pin 4 high.

whos only job is to grab the COMMAND PACKET DATA from the UDP packet (sorry, I'm rubbish)

PacketSize is used like that on the Arduino playground ethernet example page, see here Ethernet - Arduino Reference. Being as :

ParsePacket (from the EthernetUdp.h file) // Returns the size of the packet in bytes, or 0 if no packets are available

In any case I've taken your advise as it alowed me to screen for only packets of correct size.

I have also amended the packetBuffer var to be of type byte and so that now works, I know I know, it's size is not 7, should I make it so?

Also, on your advice I changed the c# code to this to let it know the size of the data it's sending.

sock.SendTo(send_buffer, 7, SocketFlags.None, endPoint);

Anyway, below(bottom) is my amended code and it works. The problem I have now is my lack of understanding of either UDP in general or specifically the EthernetUDP arduino implementation of it. I know UDP is a post and forget protocol which for my purpose is fine as I'm just generating pelco commands from a joystick and throwing them at the camera controller, it doesn't matter if some are lost along the way. Problem is lag or speed. It is very laggy, move the joystick and maybe sometime next week the camers will respond.

I've tried to think about this, maybe I'm trowing too many commands per second at the UNO, maybe they are overflowing the recieve buffer on the UNO, I don't really want a recieve buffer, I want it real time.

My understanding goes like this:

Man A has lots of balls, Man B has a basket.

A throws balls into Bs basket and B removes them from the basket.

If A throws balls too quickly the basket will overflow before B has a chance to remove them.

What happens to the overflowed balls?

On that basis, should I be using UDP or is there a better way to do what I'm trying to?

I'd like to say I appretiate the help offered and am thankful for the time you guys have spend reading my woes. I am finding the solutions myself alot of the time but it really helps to be mind dumping here and getting feedback, thank you so much. Regards, Rick.

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

int MAX485 = 3;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //this mac
IPAddress ip(192, 168, 1, 111); //this IP
unsigned int localPort = 4567;      // local port to listen on

// buffers for receiving and sending data
byte packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(MAX485, OUTPUT);
  
  //turn off SD card
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
   
  Serial.begin(2400);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize == 7)
  {    
    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    
    //only for valid Pelco commands, ie sync byte 0xff
    if(packetBuffer[0] == 0xFF)
    {
      digitalWrite(MAX485, HIGH);
      delay(100);
      
      byte checkSum = ((byte)packetBuffer[1] + (byte)packetBuffer[2] + (byte)packetBuffer[3] + (byte)packetBuffer[4] + (byte)packetBuffer[5])%256;
      Serial.write((byte)packetBuffer[0]); // Synch Byte
      Serial.write((byte)packetBuffer[1]); //Camera Address
      Serial.write((byte)packetBuffer[2]);  
      Serial.write((byte)packetBuffer[3]);
      Serial.write((byte)packetBuffer[4]); //Pan Speed
      Serial.write((byte)packetBuffer[5]); //Tilt Speed
      Serial.write((byte)checkSum); //check sum is the sum of bytes (excluding the synchronization byte) modulo 256
   
      Udp.flush();// Finish reading the current packet 
     
      delay(100);
      digitalWrite(MAX485, LOW);
    
    }
    else
      Udp.flush();
  }
  else
  {
    Udp.flush();
  }
}