Page 1 of 1

Detecting if a PC is connected

Posted: Tue Jan 13, 2015 3:17 pm
by cooper
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

Re: Detecting if a PC is connected

Posted: Tue Jan 13, 2015 5:23 pm
by kfricke
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.

Re: Detecting if a PC is connected

Posted: Wed Jan 14, 2015 10:00 am
by cooper
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!

Re: Detecting if a PC is connected

Posted: Wed Jan 14, 2015 10:06 pm
by Damien
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.

Re: Detecting if a PC is connected

Posted: Thu Jan 15, 2015 12:12 am
by dhylands
If we exposed pin PA9 in the pins.csv file then you could check for the presence of vbus in boot.py.

Re: Detecting if a PC is connected

Posted: Thu Jan 15, 2015 7:54 am
by cooper
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

Re: Detecting if a PC is connected

Posted: Thu Jan 15, 2015 9:59 am
by cooper
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...