CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

usb hid and rs232 [solved]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

usb hid and rs232 [solved]
PostPosted: Mon Sep 09, 2013 10:27 am     Reply with quote

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: 19539

View user's profile Send private message

PostPosted: Mon Sep 09, 2013 11:21 am     Reply with quote

Killer lines:

delay_ms.....

Use ex_sisr.c, to receive the serial.

Best Wishes
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 1:40 am     Reply with quote

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: 19539

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 2:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 1:34 pm     Reply with quote

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: 19539

View user's profile Send private message

PostPosted: Tue Sep 10, 2013 2:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 13, 2013 1:42 am     Reply with quote

@Ttelmah

Thank you for your support. I have solved the problem! It works fine :-)

Volker
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Sep 13, 2013 3:43 am     Reply with quote

Well done.

(and 'well done', in coming back to say). Smile

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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