View previous topic :: View next topic |
Author |
Message |
Prefekt
Joined: 21 Oct 2010 Posts: 85
|
usb hid and rs232 [solved] |
Posted: Mon Sep 09, 2013 10:27 am |
|
|
I have use the hid example and change anything in the code.
The hid device is working fine.
But I need to receive data via rs232 and send it to the pc via usb.
When i catch the rs232 signal and send it to the pc, the terminal program shows the correct data.
But in my program the data are wrong.
Code: |
while(true)
{
usb_task();
usb_debug_task();
/*---------------------------------------------------------------------*/
CheckHDDSwitches();
/*---------------------------------------------------------------------*/
if (usb_enumerated())
{
/*------------------------------------------------------------------*/
if (usb_kbhit(1))
{
usb_get_packet(1, in_data, USB_CONFIG_HID_RX_SIZE);
delay_ms(100);
if(in_data[2]==0)//Message for master controler
{
if(in_data[3]==0) {WriteHDDOutput(in_data[0], in_data[1]);} //Write HDD message
if(in_data[3]==1) {ReadHDDOutput();} //Read HDD message
if(in_data[3]==2) {ReadHDDSwitches();} //Read switches message
}
else //Message for Extension
{
printf("%03u:%03u:%03u:%03u\r", in_data[0], in_data[1], in_data[2], in_data[3]);
SendInDataAsAcknowledge();
}
}
delay_ms(250);
/*-----------------------------------------------------------------*/
if (kbhit())
{
c=getc();
cKeyBuffer[iIndexKeyBuffer] = c;
iIndexKeyBuffer++;
printf("RS: %03u:%03u:%03u:%03u\r", in_data_RS[0], in_data_RS[1], in_data_RS[2], in_data_RS[3]); //this works when i catch the rs232 signal.
if (c=='\n' || c=='\r')
{
//printf("KeyBuffer: %s\r", cKeyBuffer);
cKeyBuffer[iIndexKeyBuffer] = 0;
FillInData(cKEYBuffer);
//all data comes from rs232 will be send to pc over usb
delay_ms(250);
usb_put_packet(1, in_data_RS, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE); //this is not working. This program code was never reached
iIndexKeyBuffer = 0;
cKeyBuffer[iIndexKeyBuffer] = 0;
}
}
}
/*-----------------------------------------------------------------*/
//send_timer--;
//delay_ms(1);
}
} |
The same kbhit() and follow code, I have used any times and it works!
Any idea?
Volker
Last edited by Prefekt on Fri Sep 13, 2013 1:41 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Mon Sep 09, 2013 11:21 am |
|
|
Killer lines:
delay_ms.....
Use ex_sisr.c, to receive the serial.
Best Wishes |
|
|
Prefekt
Joined: 21 Oct 2010 Posts: 85
|
|
Posted: Tue Sep 10, 2013 1:40 am |
|
|
The delay_ms was a test.
If I had a printf behind the usb_get_packet, it works better. But it could be a contingency.
I will test the example.
Volker |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Sep 10, 2013 2:39 am |
|
|
You have others though.
Problem starts with '/n'. Most terminal programs won't send this on it's own. They send LF/CR. If you pause, the keyboard scanning stops. When you think that at 9600bps, a 1/4 second pause could be 250 characters, and the PIC buffers less than two, you basically have to get into the practise of never stopping accepting characters. Hence EX_SISR (which shows how to use the hardware/software to buffer characters).
Personally, if your 'HID' device is actually CDC, then I'd suggest using the usb_getc, and usb_putc drivers, rather than direct USB I/O. Reason is that these (like EX_SISR), _buffer_ the communications....
Best Wishes |
|
|
Prefekt
Joined: 21 Oct 2010 Posts: 85
|
|
Posted: Tue Sep 10, 2013 1:34 pm |
|
|
Thanks for the explanation.
The RS232 comes from a second pic.
With the cdc device drivers I have trouble with windows 2008 server and an intel board. The HID device works very well.
In the ex_sisr example is described to use the B0 and int_ext interrupt if there are no rda interrupt.
I use the pic18f4550 and there is no rda interrupt.
What should i connect to the B0 pin???
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Sep 10, 2013 2:43 pm |
|
|
The 4550, has an INT_RDA. Use it. Serial is on pins C6, and C7.
It'll never be reliable using a software UART, and B0. Every time the serial receive is called it risks interrupting the USB (you must not have long delays in interrupts with USB - the mSec needed to receive a character is such a delay). Worse if you try to send anything, this will be corrupted by the USB interrupts...
Best Wishes |
|
|
Prefekt
Joined: 21 Oct 2010 Posts: 85
|
|
Posted: Fri Sep 13, 2013 1:42 am |
|
|
@Ttelmah
Thank you for your support. I have solved the problem! It works fine :-)
Volker |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Fri Sep 13, 2013 3:43 am |
|
|
Well done.
(and 'well done', in coming back to say).
Best Wishes |
|
|
|