| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| cenadius 
 
 
 Joined: 13 Mar 2010
 Posts: 11
 
 
 
			    
 
 | 
			
				| USB_put_packet() function problem?or serial receive problem? |  
				|  Posted: Thu Apr 01, 2010 9:28 pm |   |  
				| 
 |  
				| I am currently doing a project which need receive a data from a serial port, RS232, and send it to computer through USB, the rs232 is communication between 2 PIC, which on the other side it is a temperature sensor board which will transmit the temperature through rs232. 
  	  | Code: |  	  | #include <18F4550.h>
 #fuses HSPLL,USBDIV,PLL5,CPUDIV1,VREGEN,NOWDT,NOPROTECT,NOLVP,NODEBUG,BROWNOUT, PUT
 #use delay(clock=48000000)
 //#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
 #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
 
 /////////////////////////////////////////////////////////////////////////////
 //
 // CCS Library dynamic defines.  For dynamic configuration of the CCS Library
 // for your application several defines need to be made.  See the comments
 // at usb.h for more information
 //
 /////////////////////////////////////////////////////////////////////////////
 #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    33                 //size to allocate for the tx endpoint 1 buffer
 #define USB_EP1_RX_SIZE    33                //size to allocate for the rx endpoint 1 buffer
 
 #define USB_EP2_TX_ENABLE  USB_ENABLE_BULK
 #define USB_EP2_RX_ENABLE  USB_ENABLE_BULK
 #define USB_EP2_TX_SIZE    33
 #define USB_EP2_RX_SIZE    33
 // 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
 #include <input.c>
 #define BUF_SIZE  25
 char k[BUF_SIZE];
 #define  mode     datain[0]
 #define  data     datain[1]
 #define  param1   datain[1]
 #define  param2   datain[2]
 
 #define  sdcard   dataout[1]
 
 #define  result   dataout[1]
 
 #define connect   dataout[0]
 #define  swst     dataout[1]
 #define  modeo    dataout[0]
 #define  conv     dataout[1]
 /
 
 
 
 void main(void)
 {
 int8  datain[33];
 int8  dataout[2];
 int8  dataout1[2];
 char  rcvstr[16];
 int8  i=0;
 char f[10];
 int a=0;
 
 
 
 
 
 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); //
 
 
 
 
 switch(mode)
 {
 case 0:
 {
 usb_get_packet(1, datain, 64);
 for(i=0;i<16;i++)
 {
 rcvstr[i] = datain[i+1];
 }
 printf("%s\r",rcvstr);
 delay_ms(1000);
 }
 
 [b][color=red]case 1: \\receive from rs and tx to comp through usb
 {
 modeo=1;
 if(kbhit(1))
 {
 get_string(k, BUF_SIZE);
 }
 conv=k;
 usb_put_packet(1, dataout, 2, USB_DTS_TOGGLE);
 break;
 }[/color][/b]
 case 2:
 {
 connect=2;
 swst=1;
 usb_put_packet(1, dataout, 2, USB_DTS_TOGGLE);
 }
 
 }
 
 
 
 
 
 }
 
 
 }
 }
 }
 
 | 
 Above is my code, can someone please help me debug?
 Is there something wrong?
 |  |  
		|  |  
		| FvM 
 
 
 Joined: 27 Aug 2008
 Posts: 2337
 Location: Germany
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 2:36 am |   |  
				| 
 |  
				| You don't report a particular problem with your code. 
 I see several oddities, however:
 - usb_task() is not executed periodically to keep the connection
 - get_string() has to wait (possibly forever) for a <CR> on the serial input
 - you send 2 bytes with put_packet(), without taking care for what has been actually received
 |  |  
		|  |  
		| cenadius 
 
 
 Joined: 13 Mar 2010
 Posts: 11
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 2:46 am |   |  
				| 
 |  
				| Thanks for replying. 
 1.) What should I do in order to keep the usb connection? I am having a problem, when I click "sendtext" button in my GUI, it will terminate the usb connection. I thought it is my GUI timer problem.
 
 2.) What is <CR>? I think it is wait forever because it can't get any input.
 What should I do in order to prevent it wait forever?
 
 3.) put_packet() can only send 2 bytes, right?  And put_packet() can only send int data, right? What if I want to send a string? I need to convert it to ascii?
 |  |  
		|  |  
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 3:10 am |   |  
				| 
 |  
				| A number of comments: First, you _must_ buffer your RS232. As it stands, the get_string function, is only called, once you have met your particular conditions on the USB. If data has arrived in the meantime (more than two characters), it _will_ be lost. Look at ex_sisr, which shows how to have an interrupt driven receive routine. Then have your main loop running all the time, with a state machine. When the USB conditions are met, switch state to receiving RS232, and pull anything already received from the buffer, and keep pulling data, till you see the end of RS232 packet marker (CR with get_string - is this what you are sending?.).
 Second, you have the maximum count for the USB transfers set to 64bytes, but the buffer you are using is only 33. What will happen if the packet is bigger than 33 bytes...
 Third, you are switching cases based on the value from the USB. Are you really sending ASCII 0, 1 and 2, or are you sending the _text_ '0', '1' and '2'....
 
 If you are just wanting to send data over USB, like over RS232, then use the usb_cdc_getc, and usb_cdc_putc functions. Much simpler, and less likely to cause problems, than getting involved in handling the packets yourself. You can then send a string using:
 
 printf(usb_cdc_putc,"%s\n",string_to_send);
 
 Best Wishes
 |  |  
		|  |  
		| cenadius 
 
 
 Joined: 13 Mar 2010
 Posts: 11
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 3:24 am |   |  
				| 
 |  
				| thanks for reply. 
 1.) what function or how can i buffer rs232? any reference for me on how to receive a data from rs232 and send it to rs232?
 
 2.)i think my data won't larger than 33 bytes, at most are 16-20 bytes.
 
 3.)yup, i am sendind ascii 0,1 ,2...
 
 actually what i am doing is to receive data drom rs232 and then send it through usb to my computer GUI, and receive data(most of the time text) from my computer GUI and sending it to another board for LCD display through rs232.
 |  |  
		|  |  
		| FvM 
 
 
 Joined: 27 Aug 2008
 Posts: 2337
 Location: Germany
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 5:39 am |   |  
				| 
 |  
				|  	  | Quote: |  	  | What should I do in order to keep the usb connection? | 
 Apparently, you copied part of your code from existing USB examples, but you didn't copy it correctly. They always
 call usb_task() in the main loop, you call it only once.
 
 It's "carriage return" respectively the "Enter" key code. As you are using get_string(), what do you expect as
 a string delimiter?
 
  	  | Quote: |  	  | First, you _must_ buffer your RS232. | 
 Yes, at least with get_string(). But pausing continous service of usb_task() can give unwanted results anyway.
 At moderate serial speeds, the main loop is executed often enough to receive serial data without interrupt
 processing. But the data must be buffered though.
 |  |  
		|  |  
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 8:49 am |   |  
				| 
 |  
				| And, to answer the bit about an example of buffering the serial, ex_sisr, as I have already said.... 
 Best Wishes
 |  |  
		|  |  
		| cenadius 
 
 
 Joined: 13 Mar 2010
 Posts: 11
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 10:47 am |   |  
				| 
 |  
				| thanks for FvM and Ttelmah for again for answer  my question. 
 
 
 
 
  	  | Quote: |  	  | They always call usb_task() in the main loop, you call it only once. | 
 
 you mean for each case i need to call usb_task() again?
 
 
  	  | Quote: |  	  | what do you expect as a string delimiter? 
 
 | 
 
 my project is to continuos receive a temperature from another board temperatue sensor, hence i think i no need delimiter in order to keep the data transmission continuos.
 
 
  	  | Quote: |  	  | pausing continous service of usb_task() can give unwanted results anyway. | 
 
 how to pausing the usb_task()?
 
 
  	  | Quote: |  	  | And, to answer the bit about an example of buffering the serial, ex_sisr, as I have already said.... | 
 
 thanks i already read the ex_sisr, is that better for me to use interrupt for the rs232 receiving? or just use if(kbhit(1)){get_string(k,BUF_SIZE);}?
 |  |  
		|  |  
		| FvM 
 
 
 Joined: 27 Aug 2008
 Posts: 2337
 Location: Germany
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Apr 02, 2010 2:23 pm |   |  
				| 
 |  
				|  	  | Quote: |  	  | you mean for each case i need to call usb_task() again? | 
 Consult the USB code examples. They are working.
 
  	  | Quote: |  	  | how to pausing the usb_task()? | 
 I meant, you should not stop calling usb_task() for a longer time, e.g. by waiting for serial input.
 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |