system
June 22, 2011, 2:17pm
1
Hi All,
I am new to Arduino and to C but am learning fast.
I have a question, however, about initialising a struct.
The following code works:
const short rom_net_cfg = 128; //Start Address for Network Configuration Storage
struct NET_t
{
byte MyMac[6];
byte MyIP[4];
byte ServerIP[4];
byte ServerSN[4];
short MyPort;
short ServerPort;
} LAN;
but I then need to initialise it.
is there a shorter way than:
LAN.MyMac[0] = 0x02;
LAN.MyMac[1] = 0x00;
LAN.MyMac[2] = 0x01;
LAN.MyMac[3] = 0x00;
LAN.MyMac[4] = 0x00;
LAN.MyMac[5] = 0x00;
LAN.MyIP[0] = 10;
LAN.MyIP[1] = 0;
LAN.MyIP[2] = 0;
LAN.MyIP[3] = 191;
etc.
The Reason it is a struct is that several of them will be passed around the network and also stored and retrieved from EEPROM using EEPROM_readAnything(rom_net_cfg, LAN);
Thanks for any insight.
Cheers
Chris
system
June 22, 2011, 2:21pm
2
struct NET_t
{
byte MyMac[];
Doesn't the compiler complain about that?
try something like this.
struct NET_t
{
byte MyMac[];
byte MyIP[];
byte ServerIP[];
byte ServerSN[];
short MyPort; // should be an uint16_t myPort !!! range = 0..65535
short ServerPort; // idem
} LAN =
{
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01 },
{ 192,168,1,200 },
{ 192,168,1,2 },
{ 192,168,1,200 },
{ 192,168,1,200 },
50,
8080
}
system
June 22, 2011, 2:31pm
4
Or with a chance of compiling:
struct NET_t
{
byte MyMac[6];
byte MyIP[4];
byte ServerIP[4];
byte ServerSN[4];
short MyPort; // should be an uint16_t myPort !!! range = 0..65535
short ServerPort; // idem
} LAN =
{
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x01 },
{ 192,168,1,200 },
{ 192,168,1,2 },
{ 192,168,1,200 },
50,
8080
};
system
June 22, 2011, 2:40pm
5
@AWOL - no it didn't But I noticed and corrected it.
@robtillaart - Thanks for the Quick replays.
@AWOL again, you replied whilst I was testing.
Thanks a ton your code worked perfectly
And I have learn 3 things:
How to initialise a structure,
Don't be scared to ask,
and that you guys are a great bunch
Thanks you both,
Cheers
Chris