View previous topic :: View next topic |
Author |
Message |
spilz
Joined: 30 Jan 2012 Posts: 220
|
[Solved]USB / Windows 10 : how to detect serial port open |
Posted: Fri Dec 04, 2015 9:19 am |
|
|
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
|
|
Posted: Fri Dec 04, 2015 9:27 am |
|
|
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
|
|
Posted: Fri Dec 04, 2015 9:38 am |
|
|
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
|
|
Posted: Fri Dec 04, 2015 11:34 am |
|
|
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
|
|
Posted: Fri Dec 04, 2015 1:10 pm |
|
|
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
|
|
Posted: Fri Dec 04, 2015 1:14 pm |
|
|
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 |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Fri Dec 04, 2015 2:02 pm |
|
|
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
|
|
Posted: Fri Dec 04, 2015 2:40 pm |
|
|
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
|
|
Posted: Sat Dec 05, 2015 2:22 am |
|
|
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
|
|
Posted: Sat Dec 05, 2015 3:12 am |
|
|
you're right,
I have to check it on Hercules and it works fine
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
|
|
Posted: Sat Dec 05, 2015 3:32 am |
|
|
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. |
|
|
|