I am developing a utility program in C# to tune parameters of a Arduino Mega being used as a Motor controller for up to 3 motors. The data must be received by the motor controller eg. as [D2100] [=start byte, D=parameter to modify, 2=is a multiplier of 256 (0-3), 100=the remainder added to the result of the multiplication(0-255) which must remain < 1024, ]= end byte. The numeric values are passed as the Decimal ASCII value(48-51).
I have tried to send as a string via Serial.write which works when requesting data from controller as I can fill the current fields. I have sent the modified values it as a Byte by Byte from a byte Array, used convert.ToInt32 but can only seem to send as the ASCII decimal values which I have confirmed in the Arduino IDE serial window by serial.print the fields value. Having only done database programming in C back in the 1990's please excuse my ignorance I am only learning. Below is my latest version sending as a string but the butchered version of public bool ExCmd(string cmd) was used when doing the byte array and then commented out sections for the string. Been trying to resolve for two weeks, reading and coding but no resolve, any help would be greatly appreciated.
private void SlKp_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!initLoad)
{
string cmdChar = "";
int val = Convert.ToInt32(slKp.Value);
if (enaMotor == 1)
{
cmdChar = "D";
}
if (enaMotor == 2)
{
cmdChar = "E";
}
if (enaMotor == 3)
{
cmdChar = "F";
}
string val1 = Convert.ToString(val / 256);
string val2 = Convert.ToString(val % 256);
string cmd = '['+ cmdChar + val1 + val2 +']' ;
if (spHandler.ExCmd(cmd))
{
fieldChanged = true;
btSave.IsEnabled = true;
}
}
}
public bool ExCmd(string cmd)
{
try
{
if (_serialPort != null && _serialPort.IsOpen)
{
_serialPort.DiscardOutBuffer();
_serialPort.DiscardInBuffer();
_receiveNow.Reset();
/*int intCount = 0;
int intChar = cmd[intCount];
if (intChar == 0b1011011) //91 [ bracket is start byte
{
do
{*/
_serialPort.Write(cmd); //intCount,1);
/* if (cmd[intCount] != 0b1011101)
{
intCount++;
}
} while (cmd[intCount] != 0b1011101 && intCount < 4); //93 ] backet is end byte
if(cmd[intCount] == 0b1011101)
{*/
return true;
}
else
{
return false;
}
}/*
else
{
return false;
}
}
else
{
return false;
}
}*/
catch
{
return false;
}
}
Arduino Code
case 'D':
#ifdef MOTOR_1
Mtr1.lKp_x100=(RxBuffer[1]*256)+RxBuffer[2];
#endif
[D2100]
. But not sure about the remainder 100. Is this a decimal one hundred, and transmitted as three bytes '1' , '0' , '0' . The whole number has a value of 2*256+100 = 612? Please clarify. – datafiddlerx - '0'
(Identical but harder to read thanx-48
) – datafiddler