Sorry for the late reply. I have changed my code a bit but still cannot logically process why it is not working. The way I see it, it should work, but the way the arduino see's it it does not.
Here's the relevant C# code:
public class ArduinoCom
{
static SerialPort _serialPort;
String[] messages = new String[100];
int messagecount = 0;
static byte[] serialData;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);
byte offByte = 253;
byte[] pwmPins = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 44, 45, 46 };
public byte[] analogPins = new byte[46];
public bool[] digitalPins = new bool[54];
public byte[] servoStates = new byte[8];
//command 0
public void getPing()
{
byte[] buffer = new byte[] { 1, offByte };
Write(buffer);
byte[] retunValues = ReadSerial();
}
//command 1
public void setPinAnalog(byte pinID, byte newState)
{
if (pwmPins.Contains(pinID))
{
byte[] buffer = new byte[] { 2, pinID, newState, offByte };
Write(buffer);
analogPins[pinID - 1] = newState;
}
}
//command 2
public void setPinDigtal(byte pinID, bool newState)
{
byte[] buffer;
if (newState)
{
digitalPins[pinID] = true;
buffer = new byte[] { 3, pinID, 255, offByte };
}
else
{
digitalPins[pinID] = false;
buffer = new byte[] { 3, pinID, 0, offByte };
}
Write(buffer);
}
//command 3
public byte getPinAnalog(byte pinID)
{
byte[] buffer = new byte[] { 4, pinID, offByte };
Write(buffer);
byte[] returnArray = ReadSerial();
analogPins[pinID-1] = returnArray[1];
return returnArray[1];
}
//command 4
public byte getPinDigital(byte pinID)
{
byte[] buffer = new byte[] { 5, pinID, offByte };
Write(buffer);
byte[] returnArray = ReadSerial();
if (returnArray[1] == 255)
digitalPins[pinID] = true;
else
digitalPins[pinID] = false;
return returnArray[1];
}
//command 5
public void setServo(byte servoID, byte dir)
{
byte[] buffer = new byte[] { 6, servoID, dir, offByte };
Write(buffer);
}
public byte[] ReadSerial()
{
try
{
Read();
if (serialData.Length > 0)
handleErrors(serialData);
return serialData;
}
catch (NullReferenceException)
{
//debug.WriteLine("[ERROR] Data array not initialized!");
debug.WriteLine("[ERROR] FATAL ERROR! \nABORTING COMMUNICATION!");
return new byte[0];
}
}
public static void Read()
{
try
{
serialData = new byte[_serialPort.BytesToRead];
_serialPort.Read(serialData, 0, _serialPort.BytesToRead);
}
catch (NullReferenceException)
{
debug.WriteLine("[ERROR] Serial not initialized!");
}
catch (TimeoutException)
{
debug.WriteLine("Serial read timed out!");
}
}
public void Write(byte[] packet)
{
try
{
if (_serialPort != null)
_serialPort.Write(packet, 0, packet.Length);
}
catch (TimeoutException) { Console.WriteLine("Serial write timed out."); }
}
protected void handleErrors(byte[] data)
{
if (data.Length > 0)
{
if (data[0] == 254)
{
debug.WriteLine("Ping");
}
}
//error messages
if (data[0] == 255)
{
if (data[1] != null)
{
//unknown
if (data[1] == 0)
{
debug.WriteLine("Unknown error from Arduino!");
}
//unknown
if (data[1] == 1)
{
debug.WriteLine("Set Pin High.");
}
//ping
if (data[1] == 2)
{
debug.WriteLine("~Ping~");
}
}
else
{
debug.WriteLine("FATAL ERROR HAS OCCURED! Closing\nserial port!");
_serialPort.Close();
}
}
}
}
and the new arduino code:
#include <Servo.h>
byte myVersion = 1;
byte incomingBuffer[64]; //holds data to be written to the serial port
Servo servos[10];
byte pointer = 0;
byte analogs[12];
boolean digitals[54];
//writes the buffer dat[] to the serial
void writeBuffer(byte dat[], int bufferSize)
{
int i;
for(i = 0; i < bufferSize; i++)
{
Serial.write(dat[i]);
}
}
//sets pin pinID to dat
void setPin(boolean analog, byte pinID, byte dat)
{
//handle analog pins
if (analog == true)
{
analogWrite(pinID,dat);
analogs[pinID] = dat;
}
//handle digital pins
else
{
if (dat>=128)
{
digitalWrite(pinID,HIGH);
digitals[pinID] = true;
}
else
{
digitalWrite(pinID,LOW);
digitals[pinID] = false;
}
}
}
//assigns a servo to a pin
void assignServo(byte servoID, byte pinID)
{
servos[servoID].attach(pinID);
}
//returns the state of analog pin pinID
byte getAnalogPin(byte pinID)
{
return analogs[pinID];
}
//returns 255 if digital pin pinID is HIGH
byte getDigitalPin(byte pinID)
{
if (digitals[pinID] == true)
{
return 255;
}
else
{
return 0;
}
}
//sets a servo to the setting
void setServo(Servo servo, int pos)
{
servo.write(pos);
}
//gets a servo's rotation
byte getServo(Servo servo)
{
return servo.read();
}
void setup()
{
Serial.begin(9600);
}
void handlePacket(byte buffer[])
{
switch (buffer[0])
{
//writes a ping back
case byte(1):
{
Serial.write(254);
}
break;
//sets an analog pin
case byte(2):
{
analogWrite(buffer[1],buffer[2]);
Serial.write(255);
Serial.write(1);
}
break;
//sets a digital pin
case byte(3):
{
pinMode(buffer[1],OUTPUT);
if (buffer[2]>=128)
{
digitalWrite(buffer[1],HIGH);
}
else
{
digitalWrite(buffer[1], LOW);
}
Serial.write(255);
Serial.write(1);
}
break;
//request for an analog pin's value
case byte(4):
{
Serial.write(3);
Serial.write(getAnalogPin(buffer[1]));
}
break;
//request for an digital pin's value
case byte(5):
{
Serial.write(4);
Serial.write(getDigitalPin(buffer[1]));
}
break;
//sets a servo
case byte(6):
{
setServo(servos[buffer[1]],buffer[2]);
}
break;
case byte(7):
{
assignServo(buffer[1],buffer[2]);
}
default:
byte buff[2];
buff[0]=255;
buff[1]=0;
Serial.write(buff,2);
break;
}
}
void loop()
{
byte dat = Serial.read();
if (dat!=253)
{
incomingBuffer[pointer] = dat;
pointer++;
}
else
{
pointer = 0;
handlePacket(incomingBuffer);
for(byte i = 0; i<64;i++)
{
incomingBuffer[i]=0;
}
}
}
I get the catch statement when trying to ping, and pin 13 is always high.