Detecting if a PC is connected

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
cooper
Posts: 23
Joined: Mon Oct 20, 2014 8:40 am

Detecting if a PC is connected

Post by cooper » Tue Jan 13, 2015 3:17 pm

Hello,

I am trying to get the micropython to switch into different modes depending on whether a PC is connected or not.
If a PC is not connected I want it to run a script without any input (this part is easy enough), but if a PC is connected I don't wait it to do anything unless instructed to do so.
The reason for this is because the main script would be doing some logging, once a pc is connected I don't want it to log because I want to retrieve the preexisting logs.

Is this possible? and if so how would I go about achieving this?

Thanks in advance

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Detecting if a PC is connected

Post by kfricke » Tue Jan 13, 2015 5:23 pm

You should be able to accomplish this by reading the following documentations: The key to success should be to alter the boot.py file and integrate a decision if you see a serial device over USB. If no CDC is available the USB connenction can be used for power supply only. Caveats may be burried in timing during the boot process.

IIRC, somewhere else Damien described to keep the user button pressed during startup to force the pyboard into specific modes of operation. I believe he did that by altering the boot.py as well. Check Tutorial 8. Making the pyboard act as a USB mouse with this in mind. A possible solution should be straight forward then.

cooper
Posts: 23
Joined: Mon Oct 20, 2014 8:40 am

Re: Detecting if a PC is connected

Post by cooper » Wed Jan 14, 2015 10:00 am

I must have missed the pyb.have_cdc() function in the documentation.
With this I can just let the pyboard boot normally and only run the script if the CDC is absent.

Danke Schoen!

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: Detecting if a PC is connected

Post by Damien » Wed Jan 14, 2015 10:06 pm

pyb.have_cdc() won't work (it'll always return False) in boot.py. The USB device is only initialised after boot.py is run, so that you can configure the USB (eg to be HID mode) in boot.py.

So you can do what you want, but you need to make the check in main.py.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Detecting if a PC is connected

Post by dhylands » Thu Jan 15, 2015 12:12 am

If we exposed pin PA9 in the pins.csv file then you could check for the presence of vbus in boot.py.

cooper
Posts: 23
Joined: Mon Oct 20, 2014 8:40 am

Re: Detecting if a PC is connected

Post by cooper » Thu Jan 15, 2015 7:54 am

I might have missed something but pyb.have_cdc() always returns False unless a terminal is open on the PC.

The reasoning for detecting a PC is because I don't want the pyboard to write to flash when a computer is connected as I want to retrieve files from it:

Code: Select all

logEN = pyb.have_cdc()
if not logEN:
		gpslog = open("logs/alllog.txt", "a")
		pyb.LED(3).on()
		gpslog.write(buffer1 + "\n")
		pyb.LED(3).off()
		gpslog.close()
So unless a terminal is open on my PC, it will log try to the SD card.

I'd like to go off on a tangent here, my script is also logging another serial port and putting the data in a separate log. When logging is disabled, i.e. not writing to flash and only sending data over the vcp, the program works fine. When logging is enabled, I get dropped bytes occasionally, and then after a short while OSerror 5. Could someone please explain why that is happening and how to prevent it? Ultimately I would like to stick the pyboard in a box and leave it for at least a day and not have to worry about OSerror5.

Thanks

cooper
Posts: 23
Joined: Mon Oct 20, 2014 8:40 am

Re: Detecting if a PC is connected

Post by cooper » Thu Jan 15, 2015 9:59 am

OSerror 5. Could someone please explain why that is happening and how to prevent it?
It would seem that I am trying to write too many bytes at once to the SD card. I found that reducing this to 500 bytes fixes the OSError: 5 Issue. But I am still getting missing bytes here and there...

Post Reply