RoboWanabe
Member
I i was wondering if some one could explain whats going on in this pice of code: For e.g. :
1) what if some one has sent data on the line but the function serial_get_char() has already been passed? I presume that a flag gets set and waits till it is called again?
2) If some one could just explain how the data comes in where it is stored and how it is analysed? it looks like it only takes one character so how does it build up a word? with some kind of buffer?
3) just some general serial operational knowledge would be great thank you
im struggling getting my head round it
1) what if some one has sent data on the line but the function serial_get_char() has already been passed? I presume that a flag gets set and waits till it is called again?
2) If some one could just explain how the data comes in where it is stored and how it is analysed? it looks like it only takes one character so how does it build up a word? with some kind of buffer?
3) just some general serial operational knowledge would be great thank you
Code:
//*************************************************************************//
void mesh_commands_serve(void)
{
static unsigned char flag_delimiter = TRUE;
char c = 0;
char *sPtr;
char textbuffer[40];
signed long timeout_check;
// fetch a byte from serial receive stream
c = mesh_serial_get_char();
// if delimiter has been received, and c is not a delimiter
if ( (flag_delimiter != FALSE) && (c != '\r') )
{
// and if c is a valid message character
if (c >= 0x20)
{
// then flag message not delimited
flag_delimiter = FALSE;
// clear the message buffer
mesh_message_clear();
// and write c to message buffer
mesh_message_append(c);
}
}
else
{
// otherwise check for delimiter
if (c == '\r')
{
#ifdef MESH_DEBUG
console_put_string("[");
console_put_string(mesh_message_buffer);
console_put_string("]\r\n");
#endif
// flag message as delimited
flag_delimiter = TRUE;
// process message string
sPtr = strchr(mesh_message_buffer, ':');
if (sPtr != 0)
{
sPtr++;
mesh_message_parse(sPtr);
}
}
else
{
// otherwise check if c is a valid message character
if (c >= 0x20)
{
// then append c to message buffer
mesh_message_append(c);
}
}
}
// transmit status beacon
if (state_mesh_radio == RADIO_STATE_ONLINE)
{
if ((time - mesh_beacon_time) >= 0)
{
// Format is !R=nuuuussss
// where
// !R= is the message identifier
// n is the index (LHA number, 1 to 4)
// uuuu is the 16-bit node address (in hex)
// ssss is the reported status (in hex)
// p is the current on/off state
// i is the current intensity (1 to 5)
//
// Example !R=3C4D700081312
// means LHA 3 has node address 0xC4D7, reports a glideslope fault,
// is currently set to on at intensity 2.
/* sprintf(textbuffer, "!R=%01X%04X%04X%01X%01X",
mesh_settings.link_address,
mesh_settings.nodeaddress,
system_status,
(system_light_on & 0x01),
((ram_data.runtime.opmode + 1) & 0x07)); */
#ifdef MESH_DEBUG
// sprintf(textbuffer+6, "%02X%04X", debug_status_id++, system_status);
console_put_string(textbuffer);
console_put_string("\r\n");
#endif
// link_broadcast(textbuffer);
mesh_transmit(MESH_COORDINATOR, textbuffer);
mesh_beacon_time = time + MESH_BEACON_PERIOD;
}
}
// check for external coordinator presence
atomic_begin();
timeout_check = (time - mesh_coordinator_message_timeout);
atomic_end();
if ( (ram_data.config.option_radio != FALSE)
&& (timeout_check >= 0)
)
{
atomic_begin();
mesh_coordinator_message_timeout = time + COORDINATOR_MESSAGE_TIMEOUT;
atomic_end();
#ifdef MESH_DEBUG
sprintf(textbuffer,"time=%li",time - mesh_coordinator_message_timeout);
console_put_string(CRLF);
console_put_string(textbuffer);
console_put_string("NETWORK GONE!\r\n");
#else
mesh_radio_send("AT+WLEAVE");
mesh_state_set(mesh_state_offline);
#endif
}
}
//*************************************************************************//
Last edited by a moderator: