View previous topic :: View next topic |
Author |
Message |
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
about int1 |
Posted: Sun Mar 17, 2019 4:43 am |
|
|
Hi, I have seen a variable int1 in Modbus library in CCS C, and I wonder if uint8_t two_characters=0; increment two times like that:
two_characters++; is 1
two_characters++; is 0
two_characters++; is 1 am I right? These are the code.
Code: |
void incomming_modbus_serial() {
char c;
static int1 two_characters=0;
static unsigned int8 datah,datal,data;
c = fgetc(MODBUS_SERIAL) & 0x7F;
if (!modbus_serial_new)
{
if(modbus_serial_state == MODBUS_START)
{
if(c==':')
modbus_serial_state++;
}
else if(modbus_serial_state == MODBUS_GETADDY)
{
if(!two_characters)
{
if(c>=0x41)
datah=((c-0x37)<<4);
else
datah=((c-0x30)<<4);
modbus_serial_lrc=0;
}
else
{
if(c>=0x41)
datal=c-0x37;
else
datal=c-0x30;
data=(datah | datal);
modbus_rx.address=data;
modbus_calc_crc(data);
modbus_serial_state++;
}
two_characters++;
}
else if(modbus_serial_state == MODBUS_GETFUNC)
{
if(!two_characters)
{
if(c>=0x41)
datah=((c-0x37)<<4);
else
datah=((c-0x30)<<4);
}
else
{
if(c>=0x41)
datal=c-0x37;
else
datal=c-0x30;
data=(datah | datal);
modbus_rx.func=data;
modbus_calc_crc(data);
modbus_serial_state++;
modbus_rx.len=0;
modbus_rx.error=0;
}
two_characters++;
}
else if(modbus_serial_state == MODBUS_GETDATA)
{
if(c=='\r')
{
modbus_serial_state++;
modbus_rx.len--;
modbus_serial_lrc-=data;
}
else if(!two_characters)
{
if(c>=0x41)
datah=((c-0x37)<<4);
else
datah=((c-0x30)<<4);
two_characters++;
}
else
{
if(c>=0x41)
datal=c-0x37;
else
datal=c-0x30;
data=(datah | datal);
if (modbus_rx.len>=MODBUS_SERIAL_RX_BUFFER_SIZE)
modbus_rx.len=MODBUS_SERIAL_RX_BUFFER_SIZE-1;
modbus_rx.data[modbus_rx.len]=data;
modbus_rx.len++;
modbus_calc_crc(data);
two_characters++;
}
}
else if(modbus_serial_state==MODBUS_STOP)
{
if(c=='\n')
{
modbus_serial_lrc=((0xFF-modbus_serial_lrc)+1);
if(modbus_serial_lrc==data)
modbus_serial_new=TRUE;
}
modbus_serial_state=MODBUS_START;
two_characters=0;
}
}
#if (MODBUS_TYPE == MODBUS_TYPE_MASTER)
modbus_serial_wait=MODBUS_SERIAL_TIMEOUT;
#endif
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9253 Location: Greensville,Ontario
|
|
Posted: Sun Mar 17, 2019 5:35 am |
|
|
Yes, by looking at the code (quickly), they use it to determine which incoming data byte is the MSB and the LSB of a 'pair' of data. I'm assuming the MODBUS data is in pairs.
Rather clever really, as a simple quick test determines if high byte or low byte. Actually any '1 of 2' choices.
You could also use it as an 'odd or even' test
Jay |
|
|
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
|
Posted: Sun Mar 17, 2019 6:01 am |
|
|
Okay temtronic,
changing I wonder here was this
two_characters++; is 1
two_characters++; is 0
and you checked it is right. Thank you |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9253 Location: Greensville,Ontario
|
|
Posted: Sun Mar 17, 2019 6:50 am |
|
|
Of course the easy check to this ,is to have the program display the 'two_characters' value, say to a PC or LCD module....
If you have a one spare I/O pin, asttache a TTL<>USB module and use this to send 'debug data' to a PC running a 'terminal program'. This is also a great way to look at ANY variable and any point of the program. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Sun Mar 17, 2019 6:58 am |
|
|
The only thing 'puzzling', is the phrasing of your question...
You say "uint8_t two_characters=0;". Er. unt8_8... No the variable is int1,
and importantly _static_. So the value is initialised to '0' on the first pass
(this is what it is explicitly initialised 'to'), and then increments 0,1,0,1 etc.. |
|
|
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
|
Posted: Mon Mar 18, 2019 5:11 am |
|
|
Yes I sould check it using LCD but I didnt have it and I needed learn it very fast, and thank you all help. |
|
|
|