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 support@ccsinfo.com

how to stop USB data transmission infinitely?

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



Joined: 13 Mar 2010
Posts: 11

View user's profile Send private message

how to stop USB data transmission infinitely?
PostPosted: Sun Mar 14, 2010 6:21 pm     Reply with quote

Hi, I am doing a project in which I need to transmit data from from computer through usb to RS232, and I test my project with Hyperterminal and a USB GUI.

When I transmit some word from USB GUI for example: abc, Hyperterminal there will receive abc permanently.....

Can someone pls help me figure out my problem? Below is my code:
Code:

#include <18F4550.h>
#fuses HSPLL,USBDIV,PLL5,CPUDIV1,VREGEN,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=48000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#define USB_HID_DEVICE     FALSE             
#define USB_EP1_TX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for IN bulk/interrupt transfers
#define USB_EP1_RX_ENABLE  USB_ENABLE_BULK   //turn on EP1(EndPoint1) for OUT bulk/interrupt transfers
#define USB_EP1_TX_SIZE    2                 //size to allocate for the tx endpoint 1 buffer
#define USB_EP1_RX_SIZE    33                //size to allocate for the rx endpoint 1 buffer

// Include the CCS USB Libraries.  See the comments at the top of these
// files for more information
//
/////////////////////////////////////////////////////////////////////////////
#include <pic18_usb.h>     //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <fypusb.h>     //USB configuration and descriptors
#include <usb.c>           //handles usb setup tokens and get descriptor reports

void main(void)

   int8  datain[33];
   int8  dataout[2];
   char  rcvstr[16];
   int8  i;

 usb_init();                      //init USB

   usb_task();                      //usb  interrupt
   usb_wait_for_enumeration();      //wait from host PC

while (TRUE)

   {
       
      if(usb_enumerated())          //usb config
      {
         if (usb_kbhit(1))          //
         {
            usb_get_packet(1, datain, 64); //
               for(i=0;i<16;i++)                 
                  {
                    rcvstr[i] = datain[i+1];}           
                    printf("%s",rcvstr);
                   
                  }
}
}
}

Has USB got function to clear the received buffer?
Thank you very much.
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Mon Mar 15, 2010 9:36 am     Reply with quote

Ignoring the USB, there are enormous problems with your printf....
You transfer a number of bytes from the USB buffer to your buffer. You then ask printf to print this buffer. Series of problems:
1) You have not put a null terminator in the data, so the printf, will print through memory, till it happens to hit a null.....
2) You print the buffer repeatedly.

The 'get_packet' function, returns how many characters it has transferred. You need to null terminate the buffer at this point.
Then you need to copy the buffer, and only print once _after_ the copy.

Realistically, if you are using kbhit, why not use the usb_getc function, which automatically returns one character?...

I think if you sort out the problems here, you may well find it all starts working.

Best Wishes
cenadius



Joined: 13 Mar 2010
Posts: 11

View user's profile Send private message

PostPosted: Sat Mar 27, 2010 10:20 pm     Reply with quote

Thanks, the problem was solved by clearing the usb buffer.
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