# Raw Data using serial port

**URL:** <https://discourse.psychopy.org/t/raw-data-using-serial-port/37397>\
**Category:** Builder\
**Tags:** timing\
**Created:** [November 7, 2023, 5:55am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397 "2023-11-07T05:55:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanderinguser](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wanderinguser](https://discourse.psychopy.org/u/wanderinguser)\
**Post date:** [November 7, 2023, 5:55am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/1 "2023-11-07T05:55:16Z")

</div>

Hi,

I’m trying to follow the instructions as mentioned in the following link. I was wondering if Psychopy saves the output of the serial port anywhere or is is only keeps track of timestamps of input stimuli and serial port start and end. Any help with this is appreciated. Thanks in advance.

[https://psychopy.org/hardware/serialPortInstr.html](https://psychopy.org/hardware/serialPortInstr.html)

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [November 15, 2023, 11:09am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/2 "2023-11-15T11:09:01Z")

</div>

Hi @wanderinguser,

Not by default, however you can ask for the serial port to be read at a given point using `port.read()` where port is the name of your serial port if that’s helpful?

Thanks,

Kim

---

<div class="post-metadata">

**Author:** ![wanderinguser](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wanderinguser](https://discourse.psychopy.org/u/wanderinguser)\
**Post date:** [November 16, 2023, 5:19am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/3 "2023-11-16T05:19:59Z")

</div>

@Kimberley_Dundas Thanks for getting back to me. I tried to do it with coding instead of using the builder. The experiment runs without any error, but still, I cannot find the signal output anywhere, am I missing something in my code?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/0/c0d0cf4edbd5f2aca4c9cf54d1440e38bca12e09.jpeg)

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [November 16, 2023, 7:40am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/4 "2023-11-16T07:40:33Z")

</div>

I would highly recommend that you use Builder to add your stimuli etc., as this will give the best timing possible. It looks like you are using the Builder interface and adding code components here though, which is absolutely fine.

So the code you have here looks like it’s in the begin experiment tab. Those two if statements should be in an each frame tab, as the status of image\_2 needs to be checked on each frame. If they’re here, the first will never return true.

Could I just check what you’re wanting to receive from the serial port? Are you expecting it to send a response when the trigger is received?

Thanks,

Kim

---

<div class="post-metadata">

**Author:** ![wanderinguser](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wanderinguser](https://discourse.psychopy.org/u/wanderinguser)\
**Post date:** [November 17, 2023, 6:41pm UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/5 "2023-11-17T18:41:09Z")

</div>

@Kimberley_Dundas I tried Builder too using serial out input stimuli, getting the same result. I’m trying to get raw signal output data from serial output. To achieve that do I need to follow the change the code location from the experiment tab to each frame tab?

Warmly,  
Wanderingfaraz

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [November 17, 2023, 7:08pm UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/6 "2023-11-17T19:08:23Z")

</div>

Just checking I understand - do you need to send any triggers at all to your serial device? Or just read the output?

In both cases, the port.read()/port.readline() commands would need to be in the each frame tab, yes.

---

<div class="post-metadata">

**Author:** ![wanderinguser](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wanderinguser](https://discourse.psychopy.org/u/wanderinguser)\
**Post date:** [November 19, 2023, 7:42pm UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/8 "2023-11-19T19:42:38Z")

</div>

No exactly, I’m trying to show an image and get the raw signal out put somewhere recorded. A setup like this basically.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/3/4/34d979edc141e25c7db9303fa9ec3a8aee9f4c55.jpeg)

I changed the code to this format.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/1/5/15e027331411f2c32e2d0b191c3018890de58c58.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/9/1/9136bee92ad935dfd62b1c21d412df161de31dd9.jpeg)

The experiment starts and it shows the first image and never gets to the second one. The screen keeps showing the first image without quitting. This is the output of the program.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/0/70650b8bc593c7824bbee7248138dd5a4ff70ebc.jpeg)

Please let me know if I need to change anything to make it work.

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [November 20, 2023, 2:34pm UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/9 "2023-11-20T14:34:12Z")

</div>

The star just indicates that you have made unsaved changes to the code in that tab.

If you don’t need to send anything, you actually only need the final two lines of code in the each frame tab, so you’d have the following in the Begin Experiment tab:

```auto
import serial
port = serial.Serial('/dev/tty.usbserial-DP04VYDC', baudrate = 115200)
res = []

```

And then in the Each Frame tab:  
`res.append(port.readline())`

This would append the value read from the port on every frame. I should caution though that this will use a lot of memory (depending on what you’re collecting) and potentially cause dropped frames if there’s a lot of data being sent from the serial port.

Hope this helps,

Kim

---

<div class="post-metadata">

**Author:** ![wanderinguser](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wanderinguser](https://discourse.psychopy.org/u/wanderinguser)\
**Post date:** [November 25, 2023, 2:29am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/10 "2023-11-25T02:29:44Z")

</div>

Hi Kim,

Thanks for your response. This is what I currently have, it gets stuck on the text\_2 part with the loading logo appearing and never gets to the image\_2 part. Not sure if there is something wrong with my setup or if is this the same issue that you mentioned about memory. My laptop doesn’t show any sign of lagging at all. It would be really nice to know your input on this problem.

Best,

 ![Screenshot 2023-11-24 at 6.28.38 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/2/52c00ac1331cb2543555a4d31fad4c750471ca65.jpeg)  
 ![Screenshot 2023-11-24 at 6.28.58 PM](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/b/cbbc19e644d8c9762f9bf7965db81a8b646a52ff.jpeg)

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [November 27, 2023, 12:34pm UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/11 "2023-11-27T12:34:04Z")

</div>

Would you be able to share your .psyexp file at all so that I can take a look?

Thanks,

Kim

---

<div class="post-metadata">

**Author:** ![wanderinguser](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wanderinguser](https://discourse.psychopy.org/u/wanderinguser)\
**Post date:** [November 28, 2023, 2:53am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/12 "2023-11-28T02:53:36Z")

</div>

@Kimberley_Dundas Absolutely. I attached it to this message. Thanks in advance for your help.

[cat\_dog\_exp\_code.psyexp](https://discourse.psychopy.org/uploads/short-url/hd1LExQnf8zgUUYUUGyoQ1SdTef.psyexp) (35.7 KB)

Best,

---

<div class="post-metadata">

**Author:** ![Kimberley\_Dundas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/kimberley_dundas/32/32322_2.png) [@Kimberley\_Dundas](https://discourse.psychopy.org/u/Kimberley_Dundas)\
**Post date:** [December 7, 2023, 11:28am UTC](https://discourse.psychopy.org/t/raw-data-using-serial-port/37397/13 "2023-12-07T11:28:15Z")

</div>

This looks to be set up correctly to me @wanderinguser. Without seeing your setup and testing things out it’s difficult to pinpoint the problem.

You could perhaps try to insert a text component that is set up to show what’s being read from the port on every frame? I’ve attached what I mean in the file I’ve attached. I can’t test this out myself so hopefully it works.

If you think it would be helpful to set up some 1:1 support via a video call that’s something we can offer via our consultancy services? Of course, as we don’t know about every single piece of hardware we can’t guarantee a fix, but we can certainly try!

Thanks,

Kim  
[cat\_dog\_exp\_code.psyexp](https://discourse.psychopy.org/uploads/short-url/wwLcdKzlt3Ira5pzSFdA3KuLqF8.psyexp) (36.8 KB)
