USB VCP Acces denied with pySerial - serial-port

I just put my hand on a OpenMV Cam H7 Plus that is using micro-python. I am trying an example that is supposed to take a python program to ask for a picture from the camera and save it. As far as I understand (still new to python) is the camera IDE creates a virtual serial port and listens for a command. The main python program try to open the port (COM4) and is denied. The problem , I think, is the port is already in use. How can I get access?
IDE micro-python code
import sensor, image, time, ustruct
from pyb import USB_VCP
usb = USB_VCP()
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000) # Wait for settings take effect.
print("USB is a Com Port", usb.isconnected())
while(True):
cmd = usb.recv(4, timeout=5000)
if (cmd == b'snap'):
img = sensor.snapshot().compress()
usb.send(ustruct.pack("<L", img.size()))
usb.send(img)
Main python Code
import serial
import struct
port = 'COM4'
sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,
xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True)
sp.setDTR(True) # dsrdtr is ignored on Windows.
sp.write("snap")
sp.flush()
size = struct.unpack('<L', sp.read(4))[0]
img = sp.read(size)
sp.close()
with open("img.jpg", "w") as f:
f.write(img)
And in running the main i get the error:
File "C:\Users\Vincent\usbpcvtest\lib\site-packages\serial\serialwin32.py", line 62, in open
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)

I find out how it works. The Cam auto-run the main.py when you initialize the port with a program on another computer. So all i had to do is renamed my IDE micro-python code "main.py" and to put it in the root of the cam.
the only bug is if you start the IDE you need to reconnect the cam physicaly so that the cam boot the main.py before you start your program on the main computer.

Related

Using Apache Airflow Tool, Implement a DAG for a batch processing pipeline to get a directory from a remote system

Using Apache airflow tool, how can I implement a DAG for the following Python code. The task accomplished in the code is to get a directory from GPU server to local system. Code is working fine in Jupyter notebook. Please help to implement in Airflow...I'm very new to this. Thanks.
import pysftp
import os
myHostname = "hostname"
myUsername = "username"
myPassword = "pwd"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
print("Connection successfully stablished ... ")
src = '/path/src/'
dst = '/home/path/path/destination'
os.mkdir(dst)
sftp.get_d(src, dst, preserve_mtime=True)
print("Fetched source images from GPU server to local directory")
# connection closed automatically at the end of the with-block```
For SFTP duties, Airflow provides SFTOperator that you can use directly.
Alternatively it's corresponding SFTPHook can be used with a simple PythonOperator
I acknowledge there aren't many examples, but this might be helpful
For SSH-connection, see this

Transfer files through RDP connection

Trying to copy files from a remote desktop to my local.
Here is the code that tried...
import os
import os.path
import shutil
import sys
import win32wnet
def netcopy(host, source, dest_dir, username=None, password=None, move=False):
""" Copies files or directories to a remote computer. """
wnet_connect(host, username, password)
dest_dir = covert_unc(host, dest_dir)
# Pad a backslash to the destination directory if not provided.
if not dest_dir[len(dest_dir) - 1] == '\\':
dest_dir = ''.join([dest_dir, '\\'])
# Create the destination dir if its not there.
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
else:
# Create a directory anyway if file exists so as to raise an error.
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
if move:
shutil.move(source, dest_dir)
else:
shutil.copy(source, dest_dir)
Trying to figure out how to establish a connection and copy files over to my local.
New to python here...
Are you using an RDP client?
Is this windows linux or mac ?
Which app are you using?
Is this a code you wrote ?
Do you know what virtual channels are ?
Is NLA on?
THere is very little information you provided.
can you even connect ? Can you ping the server ?

Monkeyrunner. Connect to multiple devices at the same time

I used this script to connect to multiple Android devices at the same time (i.e. to send files via adb):
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import time
import sys
import time
import os
devices = os.popen('adb devices').read().strip().split('\n')[1:]
device1 = MonkeyRunner.waitForConnection( devices[0].split('\t')[0])
package = 'com.android.browser'
activity = 'com.android.browser.BrowserActivity'
runComponent = package + '/' + activity
device1.startActivity(component=runComponent)
MonkeyRunner.sleep(1)
device2 = MonkeyRunner.waitForConnection( devices[1].split('\t')[0])
package = 'com.android.browser'
activity = 'com.android.browser.BrowserActivity'
runComponent = package + '/' + activity
device2.startActivity(component=runComponent)
I used
adb 1.0.36 (Rev 1:7.0.0+r33-2) and
monkyrunner(Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)
But all I get is:
09:02:54 E/DeviceMonitor: Adb connection Error:EOF
09:02:54 E/DeviceMonitor: Connection attempts: 1
09:02:55 E/DeviceMonitor: Connection attempts: 2
09:02:56 E/DeviceMonitor: Connection attempts: 3
Any hints what to do?
Thanks!
(Original Thread: How to run Monkeyrunner script on multiple devices at the same time )
You can use AndroidViewClient/culebra which supports multiple devices. If I recall correctly, this was one of the limitations of monkeyrunner that AndroidViewClient solved.
You can simply generate the script using
$ culebra -Uu --multi-device --start-activity='com.android.chrome/com.google.android.apps.chrome.Main' -o multi-browser.py
which generates a unit test (-U), does not verify what's in the screen dump (-u), use multiple devices, starts a specific activity as a precondition and saves the generated script to multi-device.py.
Then, run the script as
$ multi-browser.py -s all
where -s specifies the serial numbers of the devices where you want the script to run, all in this case, and the browser will start on all of them.

Platform.io upload to Teensy 3.6 via SWD (J-LINK)

I am using the excellent http://platformio.org/ together with Visual Studio Code to develop for Teensy 3.6 (an Arduino compatible board).
This works great. But I wanted to do better debugging via SWD (serial wire debug).
So I disconnected the Arduino-compatible USB-chip and connected via SWD and JLINK instead. Similar to this: https://mcuoneclipse.com/2017/04/29/modifying-the-teensy-3-5-and-3-6-for-arm-swd-debugging/
I can flash firmware that I built via platformio using the "J-Link Lite" software just fine. Also I can run the J-Link GDB Server without a problem.
But I can't get the IDE integration to work.
My platformio.ini looks like this:
[env:teensy36]
platform = teensy
board = teensy36
framework = arduino
upload_protocol = jlink
debug_tool = jlink
Still the upload_protocol is ignored and when I invoke upload (platformio.exe run --target upload) via the IDE all I get is
Linking .pioenvs\teensy36\firmware.elf
Checking program size
text data bss dec hex filename
17348 172 2696 20216 4ef8 .pioenvs\teensy36\firmware.elf
Building .pioenvs\teensy36\firmware.hex
Uploading .pioenvs\teensy36\firmware.hex
Teensy Loader, Command Line, Version 2.1
Read ".pioenvs\teensy36\firmware.hex": 17520 bytes, 1.7% usage
Soft reboot is not implemented for Win32
Waiting for Teensy device...
(hint: press the reset button)
So it's still trying to upload via Arduino compatible USB connection instead of via SWD connection. How can I get platformio to change the upload method or upload_protocol?
From Project Configuration File platformio.ini, it provides an example of how to configure Jlink GDB server:
[env:bluepill_f103c8]
...
; Debug options
debug_tool = custom
debug_server =
JLinkGDBServer
-singlerun
-if
SWD
-select
USB
-port
2331
-device
STM32F103C8
If JLinkGDBServer.exe is not included in PATH, then you need to specify the full file name of JLinkGDBServer.exe.
I have tried this, it works.
There is another example of using JlinkGDBServerCL.exe - J-Link and ST Nucleo.

How to run from the real device monkeyrunner script?

I have written this code which is working in the emualator. How do I get it to work in a real device? Is just connecting the device and changing the location (device location of apk) enough?
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connect to the current device
device = MonkeyRunner.waitForConnection()
# Install package
device.installPackage('C:/android-sdk-windows/tools/lib/purchase.apk')
# Run activity
device.startActivity(component='com.mobilenetwork.purchase/.StartPage ')
# Importing time
import time
# Waiting for 10 secs to be launched on the emulator:
time.sleep(10)
device.press('KEYCODE_BUTTON_SELECT','DOWN','')
# Screenshot
time.sleep(10)
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('G:\\Screenshot\\screen_shot.png','png')
This looks like it should work on a real device just the way it is right now. If you are experiencing issues, you might want to try adding "MonkeyRunner.sleep(n)" (where n is a number) statements. Some devices do not wait for the last task to finish before asking for the next one.
You might also try configuring the package and activity before you connect to the device.

Resources