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

[Solved]USB / Windows 10 : how to detect serial port open

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



Joined: 30 Jan 2012
Posts: 220

View user's profile Send private message

[Solved]USB / Windows 10 : how to detect serial port open
PostPosted: Fri Dec 04, 2015 9:19 am     Reply with quote

Hello everyone,

I'm using 18F2550 with USB on a lot of projects.

in general, my electronic has 2 ways to work, powered by USB, if I open a serial port during the first 5s, the chip return data through USB, else it return data through UART.

Until Win7, this code worked well.

But now, on Win10, the board ALWAYS act as if I opened a virtual usb port com, and try to send data through USB, even when I didn't.

does someone has a solution or an explanation ?

thanks for your help

Spilz

Code:
#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

#include <usb_cdc.h>


void main(void)
{
   int use_usb = 0;
   int cmp = 0;
   
   usb_cdc_init();
   usb_init();

   while(cmp++ < 50){
      if(usb_connected()){
         usb_task();
         if(usb_enumerated()){
            use_usb = 1;
            cmp = 50;
         }
      }
      delay_ms(100);
   }

   if(use_usb == 1){
      printf(usb_cdc_putc, "USE USB");
   }
   else{
      printf("USE UART");
   }

   while (TRUE)
   {
      ...
   }
}



SOLUTION :

Testing dte present :
Code:
if(usb_enumerated() && usb_cdc_carrier.dte_present == true ){

Be sure the PC software is dte compatible


Last edited by spilz on Fri Nov 25, 2016 1:24 pm; edited 4 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 9:27 am     Reply with quote

Use usb_carrier.dte_present
Have your Windows code set DTE when it opens the port.

This is reliable on all Windows versions I have tested.

Win7 SP3 and later test if the port will open, when they initialise the driver.
spilz



Joined: 30 Jan 2012
Posts: 220

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 9:38 am     Reply with quote

hello Ttelmah,

I don't control the windows part (I use free software like hercules).

I don't understand what is "usb_carrier.dte_present", it's in CCS ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 11:34 am     Reply with quote

Standard terminal programs will control the DTE line. So your free software will work. You only get problems when software enables the port, and does not set the flow control bits.
The structure "usb_carrier", contains bits reflecting the status of the imaginary 'serial' port. The bit .dte_present, is set when the host enables the imaginary DTE line, to say it is ready.

So usb_carrier.dte_present will go 'TRUE', when a package enables the DTE line to say that the a terminal device is attached to the port.
spilz



Joined: 30 Jan 2012
Posts: 220

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 1:10 pm     Reply with quote

I tryed to modify like this :
Code:
if(usb_enumerated() && (usb_carrier.dte_present == true)){
   use_usb = 1;
   cmp = 50;
}


but I get :
*** Error 12 "18F2550 RF.c" Line 269(34,45): Undefined identifier usb_carrier
spilz



Joined: 30 Jan 2012
Posts: 220

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 1:14 pm     Reply with quote

I'm not sure to be clear,

My issue is when I connect the board to my computer only for power supply and I don't want to use USB to send data.
In my code, when using Win10, I never have "use_usb = 0", but I would like Smile
jeremiah



Joined: 20 Jul 2010
Posts: 1358

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 2:02 pm     Reply with quote

spilz wrote:
I tryed to modify like this :
Code:
if(usb_enumerated() && (usb_carrier.dte_present == true)){
   use_usb = 1;
   cmp = 50;
}


but I get :
*** Error 12 "18F2550 RF.c" Line 269(34,45): Undefined identifier usb_carrier


Take a look at the file you included, usb_cdc.h

It has the correct name/spelling of that variable. This is something very important if you wish to do complex projects: you need to spend time reading through the driver file and study what it does and how. Otherwise you will just shoot blindly and may get bugs you don't intend.
spilz



Joined: 30 Jan 2012
Posts: 220

View user's profile Send private message

PostPosted: Fri Dec 04, 2015 2:40 pm     Reply with quote

I try this
Code:
   usb_cdc_init();
   usb_init();
   
   UART_USB = 0;
   cmp=0;
   while(cmp++ < 50){
      if(usb_cdc_connected()) {
         usb_task();
         if (usb_enumerated() && usb_cdc_carrier.dte_present ){
            UART_USB = 1;
            cmp = 50;
         }
      }
      delay_ms(100);
   }


but actually it never become true, even when I open serial port on windows 7.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Dec 05, 2015 2:22 am     Reply with quote

Hercules has a button in it's serial config for whether it is to enable DTR/DTE.

It needs to be set.

Most terminal packages set this by default.
spilz



Joined: 30 Jan 2012
Posts: 220

View user's profile Send private message

PostPosted: Sat Dec 05, 2015 3:12 am     Reply with quote

you're right,

I have to check it on Hercules and it works fine Smile

thanks for your help, everything works on win7 and win10.

one more question, I don't think it's necessarry to open a thread for that :

with #IF , how can I test if a variable already exists ?

this one does not work :
Code:
int test;
#ifdef test

void func(){
   test = 1;
}
#endif
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Dec 05, 2015 3:32 am     Reply with quote

The preprocessor doesn't know about C variables. Better to use a #define like:
Code:

#define ENABLE_TEST

#ifdef ENABLE_TEST
int test;

void func(){
   test = 1;
}
#endif


This then creates the variable for you.

The Macro language is all done _before_ compilation. Variables don't yet exist.
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