Scale-N

Scale-N
Arduino
Componenten
Rollend Materieel
Naslag
Onderdelen
Wissels
Basis Electronica
Symbols Electronica
Programming Arduino
AKS
DCC++
DR5000













































AKS | 1 | Communication computer with arduinos over USB


Arduino Nano source

struct Message {
uint16_t Uniek;
uint8_t BoardNo;
uint16_t Command;
uint16_t Argument;
uint16_t ArgumentExt;
uint8_t ArgumentSmall;
byte Values[4];
};
const uint16_t None = 0;
const uint16_t Nano_CH340 = 5;
const uint16_t Identifier = 8324;
bool IsActive = false;
const uint16_t ErrorUniek = 1;
const uint16_t ErrorCommandNotDefined = 14;
const uint8_t BoardNr = 1;
const uint16_t CmdGetDefinition = 1;
const uint16_t CmdTest = 2;
const uint16_t CmdError = 13;
void setup() {
Serial.begin(9600);
IsActive = false;
}
void loop() {
struct Message msg;
if (Serial.available() > 0) {
Serial.readBytes((uint8_t *)&msg, sizeof(msg));
if (msg.Uniek == Identifier) {
switch (msg.Command)
{
case CmdGetDefinition:
GetDefinition();
break;
case CmdTest:
SendTest();
break;
default:
SendError(ErrorCommandNotDefined, msg.Command);
break;
}
}
else
{
SendError(ErrorUniek, 0);
}
}
}
void SendTest()
{
struct Message msg;
memset(&msg, 255, sizeof( msg)) ;
msg.Uniek = Identifier;
msg.BoardNo = BoardNr;
msg.Command = CmdTest;
msg.Argument =1000;
msg.ArgumentExt = 1001;
msg.ArgumentSmall = 100;
msg.Values[0] = 10;
msg.Values[1] = 11;
msg.Values[2] = 12;
msg.Values[3] = 13;
Serial.write((const uint8_t *)&msg, sizeof(msg));
}
void SendError(int error, int parms)
{
struct Message msg;
memset(&msg, 255, sizeof( msg)) ;
msg.Uniek = Identifier;
msg.BoardNo = BoardNr;
msg.Command = CmdError;
msg.Argument = error;
msg.ArgumentExt = parms;
Serial.write((const uint8_t *)&msg, sizeof(msg));
}
void GetDefinition()
{
struct Message msg;
memset(&msg, 255, sizeof( msg)) ;
msg.Uniek = Identifier;
msg.BoardNo = BoardNr;
msg.Command = CmdGetDefinition;
msg.Argument = Nano_CH340;
Serial.write((const uint8_t *)&msg, sizeof(msg));
IsActive = true;
}

Download as text