Serin and usbout in the same code-problem?


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2012
    Posts
    13

    Question Serin and usbout in the same code-problem?

    Hi,

    I'm trying to read data incoming to the 18F2550 using Serin and then send the read data using USBOut to the PC as follows:

    Code:
    DEFINE    OSC 48
    Include "modedefs.bas"
    
    Buffer    VAR BYTE[1]
    Cnt       VAR BYTE
    counter   VAR WORD
    B0        VAR BYTE
    
    ADCON1 = 15               ' Set all I/Os to Digital      
    CMCON = 7                 ' Disable Comparators
    Cnt = 1
    
    input PORTA.0 
    SI var PORTA.0
    
    USBInit  
    
    Loop1:
        Serin SI,N9600,B0
        Buffer(0)=B0  
    
    Loop2: 
        USBService        
        USBOut 3, Buffer, Cnt, Loop2
    goto Loop1 ' Go and read serial input data again
    
    end
    This code does not work, 18F2550 is not even recognized as a USB CDC device. I'm sure of the hardware and the usage of CDC, and also that serial data exists in the relevant pin. The error seems to occur only when I use Serin and Usbout in the same code as above. How can I change the code to make it work properly?

    Best regards,

  2. #2
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Serin and usbout in the same code-problem?

    USB is one of the more complex things to tackle. A bit of searching and reading will get you closer. As you go through the USB section of this forum, you will find some excellent include files from Darrel to help, but it will not be something you can jump into without a lot of background reading, and significantly more code than you posted. In terms of complexity, USB is as far away from an RS232 serial port, as an airliner is from a paper plane.

    USB is also time critical. You will likely have to abandon SERIN for HSERIN so that you can use interrupts.

  3. #3
    Join Date
    May 2012
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Serin and usbout in the same code-problem?

    Thanks Charile,

    In fact I can send data thru USB using the USB CDC demo I found on this forum.

    On the other hand, isn't there any way to get serial data with Serin or Serin2 and send this data thru USB? This seems a very basic operation.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: Serin and usbout in the same code-problem?

    This may help you in setting up the USB.
    http://www.picbasic.co.uk/forum/cont...USB-SD-LOGGING
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Serin and usbout in the same code-problem?

    Quote Originally Posted by rubisharmax View Post
    Thanks Charile,

    In fact I can send data thru USB using the USB CDC demo I found on this forum.

    On the other hand, isn't there any way to get serial data with Serin or Serin2 and send this data thru USB? This seems a very basic operation.
    The issue is that the USB is very time critical and really needs to be interrupt driven. SERIN does not use interrupts, so if you are in the middle of receiving something with SERIN, and the USB demands attention, you drop the packet, or worse, part of the packet on the floor. HSERIN can use interrupts, and so you can manage the 2 independent flows, by requesting a pause, or bouncing back and forth between the two workflows.

    Still tricky to acomplish, but at least there's hope of success. A lot depends on your application. For example, you might be able to only have information sent by request, and only request when you are sure you'll have uninterrupted time to process, and thereby get away with SERIN. But a truly asynchronous SERIN or SERIN2 and an asynchronous USB is bound to be unreliable and error prone.
    Last edited by Charlie; - 12th June 2012 at 19:16.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts