|
|
View previous topic :: View next topic |
Author |
Message |
rjrodrig
Joined: 09 Sep 2009 Posts: 7
|
RS232 kbhit() functions |
Posted: Wed Sep 09, 2009 9:29 am |
|
|
I wrote a simple program to test out my communications between a Visual Basic 6.0 using MSCOMM (serial port) to communicate to the hardware I designed using a PIC18F6622 MCU.
I sent to the hardware via Visual Basic
form1.mscomm1.output = "b-0123456789" & chr(13)
This puts all the strings + a carriage return in the PC UART configured at 115200,8,N,1. I place my scope probe at my USB to RS232 Bridge, and I see the characters come in.
In the firmware, I wrote the code below
when I flash the code into memory, I enable my GUI, open the virtual serial comm port (COM4 in my case), and I transmit: "b-0123456789" + CR to perform a test. I want to echo back the characters to the GUI in visual basic.
I get all the characters in VB like this:
GOT: b-b-0123456789
RX_Counter=14
Virtual comm ports have latency. FTDI gives you drivers to use USB to RS232 converter chips to transfer data. I have used these in lots of hardware I have developed. If you go to device manager, and select the com port and right click on it, you can adjust the latencies to virtually 1ms delay or better. In any case, I know it is not that because the line of C code : RX_Counter = strlen(RX_BUFFER);
counts exactly 14 characters in its buffer. So the GUI is picking up what the hardware transmitted.
I believe there is some issue with the kbhit() or fgets() function.
Has anyone know what I am doing wrong? I have contacted support numerous times, and I think they are working on it! I need to deliver my hardware ASAP! Please if you have found a solution please let me know
1. what does KBHIT() returns if no char come in? My understanding is zero is this true?
2. should I have written: while(!kbhit()) instead. Please advise. I can post the entire code so you can see what I have.
Code: |
//The PIC18F6622 has 2 UARTS. I am trying to use HW UART#1
//Do I need to say UART1 in the #use rs232 definition?
#use rs232(baud=115200, xmit=PIN_C6,rcv=PIN_C7,bits=8,stream =RS232,errors) //do I need to include UART1?
#define RX_BUFFER_SIZE 20
int8 RX_Counter = 0;
void main (void)
{
init_mcu(); //initialize all the registers etc..
while (1)
{
while(kbhit(RS232)) //if you see a start bit in the RX hW jump in here
{ //get all chars into the buffer
fgets(RX_BUFFER,RS232);
RX_Counter = strlen(RX_BUFFER);
printf("GOT: %s\r\nRX_Counter: %d\r\n",RX_BUFFER,RX_Counter);
}
}
} |
|
|
|
rjrodrig
Joined: 09 Sep 2009 Posts: 7
|
Addendum |
Posted: Wed Sep 09, 2009 9:32 am |
|
|
I forgot to say that my PIC18F6622 external hardware is 10MHz. I use the x4 PLL HS to get it up to 40MHz. Therefore, meeting 115200 baud should not be an issue. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 09, 2009 12:27 pm |
|
|
You don't need to test kbhit in that loop. The fgets() function calls kbhit
before it reads each character. Try this loop code:
Code: |
while (1)
{
fgets(RX_BUFFER,RS232);
RX_Counter = strlen(RX_BUFFER);
printf("GOT: %s\r\nRX_Counter: %d\r\n",RX_BUFFER,RX_Counter);
}
|
Also, are you sending more than one message to the PIC for this test ?
If so, what is the time interval between messages ? |
|
|
|
|
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
|