PDA

View Full Version : Serin and usbout in the same code-problem?



rubisharmax
- 10th June 2012, 07:50
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:


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,

Charlie
- 10th June 2012, 14:35
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.

rubisharmax
- 10th June 2012, 22:11
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.

mackrackit
- 11th June 2012, 05:12
This may help you in setting up the USB.
http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING

Charlie
- 12th June 2012, 19:12
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.