|
|
View previous topic :: View next topic |
Author |
Message |
david123 Guest
|
printf causing logic to change |
Posted: Thu Feb 08, 2007 2:27 pm |
|
|
ide version 3.245
pch version 3.245w
pic version 18F8722
in the processInput function im simply keeping track of the number of bytes i recieve, and putting them in thier corresponding index in an array. when my array wasnt filling up, to debug i started to print the value of the counter to make sure it was incrementing. all of a sudden the code would work and the array would fill up, but when i take out the print statement, the counter stops incrementing. i cant figure out why that happens!?
ill try to include all relevant code below:
Code: |
#include <18F8722.H>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPUT,NOBROWNOUT,NOLVP,NOWRT,NOPROTECT,NODEBUG,MCLR
#use delay(clock=6000000)
#use rs232(STREAM=serial,BAUD=115200,XMIT=PIN_C6,RCV=PIN_C7,PARITY=N,BITS=8, ERRORS,DISABLE_INTS)
|
Code: |
// One PacketStruct instance will be used to keep packet info in one place
struct PacketStruct
{
// valid flag set to true when a complete command is stored in buffer...
// 3 byte commands consist of cmd byte, followed by arg, followed by an '*'
short valid;
char buffer[PKT_CMD_MAX_LEN];
int8 numBytesRead;
};
void clearPacket()
{
packet.valid = 0;
packet.numBytesRead = 0;
}
void processInput()
{
char inChar;
// Check for waiting data...
while (kbhit())
{
if(packet.numBytesRead>=PKT_CMD_MAX_LEN){
return;
}
// Read incoming byte
inChar = getc();
// If the char is '*' it signifies the end of a packet...
if (inChar == '*')
{
// Packet is now valid...
// Leave remaining bytes to be processed with next packet
packet.buffer[packet.numBytesRead] = '\0';
packet.valid = 1;
//outputting whats in the buffer once i have a valid packet
puts(packet.buffer);
return;
}
packet.buffer[packet.numBytesRead] = inChar;
packet.numBytesRead++;
//if this printf is commented out, packet.numBytesRead stops incrementing
printf("%d ",packet.numBytesRead);
}
}
|
Code: |
void main()
{
int i=0;
// Initialize the hardware
initDeviceState();
for(i=0; i<10; i++){
output_low(LED);
delay_ms(100);
output_high(LED);
delay_ms(100);
}
//soutput_high(LED);
// Loop over and over and over and...
while(1)
{
// Restarts watchdog timer (prevents processor from resetting...)
RESTART_WDT();
// Attempt to read packet data...
clearPacket();
processInput();
// If we have a valid packet, process it... (see packet desc above)
if(packet.valid)
{
//printf(packet.buffer[1]);
switch (packet.buffer[PKT_CMD_BYTE])
{
case CMD_LED:
printf("cmdled");
controlLED(packet.buffer[PKT_FIRST_DATA_BYTE]);
break;
}
outputError();
}
}//end of while(1)
}//end of main()
|
|
|
|
Ttelmah Guest
|
|
Posted: Thu Feb 08, 2007 3:42 pm |
|
|
Haven't looked far, but can make a guess.
The problem is the 'while(kbhit())' loop. This will fail, after a character is received. Remember the processor is _fast_ compared to serial rates the array access, only takes a few uSec, so another character has not arrived. The routine therefore drops through, and exits. The while loop in the main, has code 'clearpacket', whichresets the counter, so on the next character, the counter has been cleared, and everything starts again. Adding the printf, causes a short delay, which is enough for the next character to have arrived, so the routine does not drop through, and the counter works.
You should really modify the 'clearpacket' routine, so it is only called, when a full packet has been seen, or you will need to wait for each character in turn, which rather makes hardware serial pointless...
Best Wishes |
|
|
david123 Guest
|
|
Posted: Fri Feb 09, 2007 1:40 pm |
|
|
thats a great suggestion, ill give it a try, thanks! |
|
|
david123 Guest
|
|
Posted: Mon Feb 12, 2007 9:37 am |
|
|
it worked, i added a delay, which i had tried ealier with no luck, but increased the time it waited. thanks! |
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 12, 2007 10:34 am |
|
|
Using a 'wait' is potentially dangerous.
The problem is that if the wait is less than a character, you drop out prematurely, and the counter gets reset. However if the wait is longer than a character, you slow the response, and may end up overflowing the hardware input buffer of the chip. You 'get away' with this at present, because your message is so short, but it is a problem 'waiting to bite', if you lengthen the packet...
Look instead at one of two solutions. Look at how 'timed_getc' has been implemented, and consider using this approach to waiting in the fetch. Or as I already posted, consider ming the reset only happen when a complete packet has been seen.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|