# Send image via serialport

**URL:** <https://forums.openmv.io/t/send-image-via-serialport/1186>\
**Category:** OpenMV Boards\
**Created:** [March 31, 2019, 10:27pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186 "2019-03-31T22:27:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [March 31, 2019, 10:27pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/1 "2019-03-31T22:27:31Z")

</div>

Hi. I’ve checked [this topic](http://forums.openmv.io/viewtopic.php?t=365) for clues and found this [GitHub](https://github.com/openmv/openmv/blob/master/scripts/examples/02-Board-Control/usb_vcp.py) example.

I’d like to ask for a tutorial how it should work. I’m a years long programmer, but haven’t touched python yet. All packages are installed, camera connected. The script (on PC) should get a captured frame from the camera.

Should be the camera connected via USB-microUSB cable or usbc?  
How can I ensure the script doesnt return “Device or resource busy”?

I’ve inserted pyb.LED(3).on() before while within main.py on camera to see if it runs - LED didn’t light up.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 31, 2019, 10:30pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/2 "2019-03-31T22:30:51Z")

</div>

Hi, can you provide an example for how you are running the script. The OpenMV Cam literally executes MicroPython code. We supply the IDE so that you can easily run scripts on it. Were you trying the LED via that?

As for the send images to the PC part. Getting the camera to do that is pretty easy. However, you need to decide on a serialization format for how the image will be sent in the byte stream and what script running on the PC side will receive the data.

That said, the whole point of the camera is not to send images off it however. It runs MicroPython and has methods on board so that you can process the image onboard.

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [March 31, 2019, 10:42pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/3 "2019-03-31T22:42:25Z")

</div>

Does it have to be done with the IDE? I’m simply running it from terminal with ‘python3’. I used what I linked from the Github. Saved the code to the camera main.py and the commented part on PC and ran it.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 31, 2019, 11:07pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/4 "2019-03-31T23:07:31Z")

</div>

Um, you can run code from the PC on the camera. That is fine. However, it’s hard to debug because if there are any issues in the code it will just print a stack trace and exit. It’s better to use the IDE to get everything but the serial output working and then once that is done switch the serial output on. Doing work with cameras is quite visual. It’s important to be able to see the frame buffer.

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [April 5, 2019, 12:52pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/5 "2019-04-05T12:52:54Z")

</div>

The camera is running as intended (snapshot() placed before if(cmd…) to check it in OpenMV IDE).

[Here is a screenshot](https://imgur.com/a/CORv1wl) of what’s happening. I had to change

```python
sp.write("snap")

```

to

```python
sp.write(input("snap"))

```

from [this GitHub code](https://github.com/openmv/openmv/blob/master/scripts/examples/02-Board-Control/usb_vcp.py), as it threw me an error - serial message must be bytes.

EDIT: So, I haven’t changed anything and it suddenly created the file `snapshot.jpg`, however its empty (0 bytes).

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 5, 2019, 3:42pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/6 "2019-04-05T15:42:05Z")

</div>

Hi, the string “snap” is suppose to be sent to the camera. Not to the screen asking the user. If the message must be in bytes change “snap” to b"snap" or ‘snap’.encode(‘utf-8’)

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [April 5, 2019, 4:08pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/7 "2019-04-05T16:08:29Z")

</div>

Oh, nice - b"snap" worked.

Okay, so variable size returns cca `2200` every time I run the code. The error is on:

```python
with open("snapshot.jpg", "w") as f:
    f.write(img)

```

Specifically `_f.write(img)_`. **TypeError: write() argument must be str, not bytes**

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 5, 2019, 4:13pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/8 "2019-04-05T16:13:41Z")

</div>

Change the “w” in open to “wb”.

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [April 5, 2019, 4:15pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/9 "2019-04-05T16:15:17Z")

</div>

> [@](#):
>
> Change the “w” in open to “wb”.

Holy-Moly, you’re an angel. Thank you!

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 5, 2019, 4:47pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/10 "2019-04-05T16:47:08Z")

</div>

You might be running the wrong python version if these were problems. 3 versus 2.7

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [April 7, 2019, 11:46pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/11 "2019-04-07T23:46:55Z")

</div>

Suddenly my camera doesn’t work. I’ve solved the issue with it sending the image. Now if I connect the camera to the PC (Ubuntu) I can open the camera folder, but can’t open the file within it (main.py) - Either the file does not exist or you have no permission to it (I always had permission). The camera works in OpenMV IDE still, if I run the saved code with the Play button in left bottom, the camera runs. However, I cant open the main.py, nor save a file to the camera (Tools → Save open script to camera). What can I do about it?

The camera after plugging in flashes in green a couple of times, one white, then multiple red (as I remember it lit up for some seconds in red, didn’t flash).

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 8, 2019, 12:02am UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/12 "2019-04-08T00:02:21Z")

</div>

Hmm, I’ve seen similar things before due to weird file permissions. Um, format the onboard drive. Then save a new main.py to the camera using OpenMV IDE.

You can format the uPy drive in Linux or just reflash the whole camera firmware to fix this.

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [April 8, 2019, 12:05am UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/13 "2019-04-08T00:05:34Z")

</div>

Is that also done by `Tools->Reset OpenMV Cam` in OpenMV IDE?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 8, 2019, 12:07am UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/14 "2019-04-08T00:07:22Z")

</div>

No, that just does a hard reset of the board as if you pressed a reset switch.

---

<div class="post-metadata">

**Author:** ![filipj](https://avatars.discourse-cdn.com/v4/letter/f/f6c823/32.png) [@filipj](https://forums.openmv.io/u/filipj)\
**Post date:** [April 8, 2019, 12:22am UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/15 "2019-04-08T00:22:39Z")

</div>

Solved. If someone browsed for an answer: Clicking on bottom line in IDE on `Firmware version`, remove filesytem- Yes, blahblah, solves it.

One (hopefully last) question: what causes the device to be busy? Sometimes I can’t send the Serial.write as it throws the error

```python
[Errno 16] could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'

```

or

```python
serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

```

Why is it busy? The red led for saving is turned off, the camera is running, no other device is connected to the PC. Can I somehow wait for the camera to get ready?

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [April 8, 2019, 2:10am UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/16 "2019-04-08T02:10:40Z")

</div>

> [@](#):
>
> One (hopefully last) question: what causes the device to be busy? Sometimes I can’t send the Serial.write as it throws the error
> 
> ```python
> [Errno 16] could not open port /dev/ttyACM0: [Errno 16] Device or resource busy: '/dev/ttyACM0'
> 
> ```
> 
> or
> 
> ```python
> serial.serialutil.SerialException: read failed: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
> 
> ```
> 
> Why is it busy? The red led for saving is turned off, the camera is running, no other device is connected to the PC. Can I somehow wait for the camera to get ready?

If the IDE is connected to the camera you can’t use the serial port.

---

<div class="post-metadata">

**Author:** ![matt992](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@matt992](https://forums.openmv.io/u/matt992)\
**Post date:** [November 11, 2020, 1:29pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/17 "2020-11-11T13:29:11Z")

</div>

I am working on a GUI project with Qt5 and I want to connect Arduino Portent with GUI on Windows via USB connection. I want to send some parameters and receive an image from Arduino, I tried with USB\_VCP example but I do not receive image. I can send bits but not an image. If I set “timeout: none” then the program is in step 7. Is there any better way to display the image in the user program?

 ![error_VCP_1.PNG](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/9491a1469eb46d208dabb7956ac73d26d3d921b0.png)  
 ![error_VCP.PNG](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/5f9c4a1bbd5012e56371eb246168795b20e0196c.png)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 11, 2020, 5:53pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/18 "2020-11-11T17:53:45Z")

</div>

If you are using Qt then you should just take the IDE code and integrate that into your app. The IDE has all the hooks in it already to talk to the camera. The openmv plugin is under qtcreator/SRC/plugins/openmv

---

<div class="post-metadata">

**Author:** ![matt992](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@matt992](https://forums.openmv.io/u/matt992)\
**Post date:** [November 12, 2020, 10:50am UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/19 "2020-11-12T10:50:55Z")

</div>

I tried with OpenMV Cam H7 Plus and this code works but with Arduino Portenta it doesn’t work.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [November 12, 2020, 4:13pm UTC](https://forums.openmv.io/t/send-image-via-serialport/1186/20 "2020-11-12T16:13:54Z")

</div>

The Portenta is really new right now. Please submit bug issues on what’s not working. The debug protocol definitely should work however.
