A problem about serial write

Issue #235 resolved
Wenjie Wu created an issue

Thanks for the great project!

I have a problem about serial write

serial write in MicroPython

from microbit import *

while True:
    if button_a.get_presses():
        power_data = [0x8D, 0x3E, 0x11, 0x01, 0x13, 0x0D]
        uart.write(bytearray(power_data))
        sleep(1000)

get the result: `8D 3E 11 01 13 0D` from the USB port (as expected)

serial write in microblocks

get the result: `FA 10 01 FA 11 01` from the USB port (unexpected)

What went wrong?

System Environment: MacOS 10.15.7 Chrome 96.0.4664.93

Comments (25)

  1. Wenjie Wu reporter

    Hi @Turgut Guneysu ,

    Thank you for your reply.

    I want to output serial data from the micro:bit to a computer. I use Pyserial to read the output. No matter what I write with serial write, I can't get the corresponding output. What should I do?

  2. Turgut Guneysu

    Hi Wenjie,

    Here is a printout of the test I did with your code.

    The problem you were having has to do with the way you have the micro:bit connected. SInce the USB port is being used for the VM traffic to the IDE, the actual UART data travels over the pins RX=0 and TX=1. See link: https://wiki.microblocks.fun/special_pins

    What you need is a TTL to USB converter that will allow you to connect pins 0 and 1 of micro:bit to the USB port. This USB can be the same USB port you are using for MicroBlocks, or another additional one just for the UART traffic. If you use the one for MicroBlocks, make sure to disconnect the USB icon in the IDE top menu, so your traffic will not conflict with the VM traffic.

    Here are two pics of the setup:

    Also, pay attention to the voltages: micro:bit is 3.3V and the PC is 5V. On the TTL2USB converter there is voltage jumper that needs to set to 3.3, so you don’t fry the micro:bit.

    In the picture, I have only connected the micro:bit TX to the PC, since there was nothing coming back. If you have 2-way traffic, then you obviously need both pins.

    Hope this will help you.

  3. John Maloney repo owner

    As I understand it, the goal is to transmit data from the board back to a program running on the host computer. The solution suggested by Turgut will accomplish that in a way that uses a different hardware serial port so that the connection is completely independent of communications between the IDE and and the microcontroller. It is a clean solution but it does require extra hardware.

    An alternative is to send data through the IDE-board communication channel. That will require a little more software effort on the receiving side, but no extra hardware. A very simply way to do this is to use the “broadcast” block to send the data you want. You don’t need to use any of the serial blocks, just the “broadcast” block like this:

    You could put that block in a forever loop with at wait so that it sends updates periodically – say, once a second. You’d also use the “join” block to create the data string from variables or sensor values.

    To receive the data, you’ll need to quit from the MicroBlocks IDE (or disconnect), then open a serial connection from some other program on your computer, perhaps written in Python or Snap!. That program will now see a stream of broadcast messages in the MicroBlocks serial protocol, which is documented here.

    That’s just a quick sketch, but I hope it is enough to get you started.

  4. Wenjie Wu reporter

    @Turgut Guneysu Thank you for your detailed and clear guidance ! Just like @John Maloney said:

    It is a clean solution but it does require extra hardware.

    As I understand it, the goal is to transmit data from the board back to a program running on the host computer. 

    @John Maloney Yes! I do hope to reach the goal.

    Let me provide more information about what I want to do:

    I would like to use microbit to control the sphero RVR(With microblocks), I can [do it with makecode and micropython](https://sdk.sphero.com/microbit) (the command data from the board to sphero RVR from USB port) , so I want to translate [the commands](https://github.com/sphero-inc/sphero-sdk-microbit-python/blob/master/sphero.py) to microblocks as a library.

    I will try your advice @John Maloney Thanks !

  5. Wenjie Wu reporter

    Hi @John Maloney , I can  transmit string(ASCII) data from the board back to sphero RVR with broadcast, but I dont konw how to transmit byte data(I need to do it in my use case)

  6. John Maloney repo owner

    Hi, @Wenjie Wu.

    Thank you for explaining what you want to do. For that goal, my suggestion is not helpful because MicroBlocks cannot send raw serial data over the USB connection. Sorry for the confusion.

    As Turgut explained, you need to connect the micro:bit v2’s serial pins to the Sphero RVR. But you may not need a USB-serial adaptor to do that. From the Raspberry Pi connection diagram here, it looks like you can just connect the micro:bit v2’s serial pins (pins 0 and 1) and ground pin to the RVR. You can use alligator clip jumper wires or a breakout board plus jumper wires to make those connections.

    The MicroBlock serial commands send and receive data over a second serial port connected to pins 0 and 1 of the micro:bit v2. The USB connector is reserved for communications between the MicroBlocks IDE and the micro:bit. The nice thing about this it supports live coding – you can experiment with the serial blocks and test scripts using those blocks as you work. That makes development and testing much easier.

    Note: You’ll need a micro:bit v2 to do this (as showing in Turgut’s photos). Unfortunately, the original micro:bit does not have second serial port.

    Let us know if you get this working!

  7. Wenjie Wu reporter

    hi @John Maloney Thank you for your detailed guidance!It works for me !

    here is a demo video.

    I like microblocks so much that I have used it for several projects:

    Thank you for your great work!

    When I finish the driver library for RVR, I will create a pull request.

  8. Bernat Romagosa

    Hi, @Wenjie Wu !

    WOW! I’ve watched all of your videos and I loved them all. The huge NeoPixel mat is awesome!

    By the way, I also have a Sonoff device and never managed to use it with MicroBlocks. Can you maybe share a bit about how you did it? What FTDI did you use? I eventually managed to flash the device with the MicroBlocks VM, but I could never connect to it from MicroBlocks after that.

    Also, I noticed in some videos you’re using a modified version of MicroBlocks called “CodeLab”. Is that a version you made yourself? Can you share a bit more about the reasons behind it? If we’re missing features that you think are essential, let’s talk about it 🙂

  9. John Maloney repo owner

    Thanks for the pull request! It will be in the next Pilot release.

    Your videos are really amazing! Are you a teacher? If so, what ages do you teach? Your blog posts are really amazing!

    I’m also curious about your CodeLab fork of MicroBlocks. It’s fine to fork it, of course, but we’d like to learn how to make MicroBlocks even better.

  10. John Maloney repo owner

    By the way, I sent your RVR video to a Katie Henry at the Micro:bit Educational Foundation.

  11. Wenjie Wu reporter

    hi @Bernat Romagosa , @John Maloney ,

    microblocks is so good that there is nothing I can improve!

    The reason I deployed CodeLab fork of MicroBlocks is because of the limited network in China : (

    I'll keep in sync with the dev branch regularly, and I'll commit all my work to the microblocks repository as well.

    By the way, I am a big fan of smalltalk(also a big big fan of Alan Kay😄).

    @John Maloney I love all your great designs: morphic、scratch、gpblocks and microblocks.

    @Bernat Romagosa S4A and Snap4Arduino also are great projectss!

    I designed Codelab Adapter with reference to s4a and ROS. Here are some demo videos.

    In this video, microblocks and Scratch are connected together using Codelab Adapter (Everything is a message!)

  12. Wenjie Wu reporter

    By the way, I also have a Sonoff device and never managed to use it with MicroBlocks. Can you maybe share a bit about how you did it? What FTDI did you use? I eventually managed to flash the device with the MicroBlocks VM, but I could never connect to it from MicroBlocks after that.

    hi @Bernat Romagosa , Everything I know is already recorded here ! Maybe I did not explain clearly enough :( Maybe that the google translation is not clear. Sorry for my bad English, I could have written an English post.

    Can you give me some more details and let me know where you are stuck?

  13. Wenjie Wu reporter

    By the way, I sent your RVR video to a Katie Henry at the Micro:bit Educational Foundation.

    @John Maloney Thanks again !

  14. John Maloney repo owner

    Katie loved the video and asks if it would be okay to share it with other at the Micro:bit Foundation and Sphero team. Is that okay? She can cc you if you like. You can email us at interest@microblocks.fun (don’t post your email here; it might be picked up by a spam robot).

  15. Wenjie Wu reporter

    Your videos are really amazing! Are you a teacher? If so, what ages do you teach? Your blog posts are really amazing!

    @John Maloney Thank you for your encouragement !

    I am a software engineer working for a non-profit organization(CodeLab). I also sometimes give lessons to teenagers. I sometimes give lessons to teenagers, so I might be considered an amateur teacher :)

  16. Bernat Romagosa

    Everything I know is already recorded here ! Maybe I did not explain clearly enough :( Maybe that the google translation is not clear. Sorry for my bad English, I could have written an English post.

    I didn’t check whether I could use google translate to go through the instructions. I will do so, thank you!!! 🙂

  17. Wenjie Wu reporter

    Katie loved the video and asks if it would be okay to share it with other at the Micro:bit Foundation and Sphero team. Is that okay? 

    @John Maloney Of course! please feel free to use it.

    We like microbit so much that we make a lot of things with it. Here are some of my particular favorites:

  18. Wenjie Wu reporter

    @Bernat Romagosa The built-in translation function of chrome browser is very useful 😄

  19. Log in to comment