microsoft / pxt-microbit

A Blocks / JavaScript code editor for the micro:bit built on Microsoft MakeCode
https://makecode.microbit.org
Other
708 stars 588 forks source link

USB serial monitor doesn't work on 9600 baud rate #3537

Open irukard opened 3 years ago

irukard commented 3 years ago

I got use case when I need to communicate with NovaFitnes SDS011 Air Quality Sensor as well as send measured and postprocessed data to PC via serial port. Since I didn't find the way to open two serial connections in MakeCode (and BTW that would be wonderful), I've decided to use:

serial.redirect(
SerialPin.USB_TX,
SerialPin.P2,
BaudRate.BaudRate9600
)

It was possible because SDS011 has an automatic mode, which send 10 byte frame every 1 second. Baud rate of 9600 was chosen because of SDS011 configuration. And here is funny thing. On 115200 (not supported by SDS) device shows up in MakeCode Serial Monitor almost instantly. On 9600, device isn't showing up... unless I open connection with correct baud rate in another program (e.g. Putty).

I don't know if it's a bug or feature. I would really appreciate the second serial port or working 9600 baud rate in MakeCode's Serial Monitor.

irukard commented 3 years ago

@pelikhan, @abchatra I would really appreciate your expertise :)

irukard commented 3 years ago

Yea... I know it's stupid... But this works ok:

        serial.setBaudRate(BaudRate.BaudRate115200)
        serial.writeValue("PM25", SDS011.pm25Value())
        serial.writeValue("PM10", SDS011.pm10Value())
        serial.setBaudRate(BaudRate.BaudRate9600)
pelikhan commented 3 years ago

We always assume a baud rate of 115200 in the MakeCode client. Not quite sure how we would fix this besides with the "stupid" fix. Are looking to display data in the MakeCode ui?

irukard commented 3 years ago

Yes. I need MakeCode UI graphs... Our teachers know how to use it and I would really try to avoid explaining how to grab serial data using Putty.

Correct me if I am wrong. Because SDS011 data might be read in background, and sending to PC could be in background as well, the only option I see is creating blocks for sending data with use of semaphores to avoid baud rate changes in the middle of SDS011 transmission and vice versa.

irukard commented 3 years ago

Does Data Streamer use the same serial? https://github.com/microsoft/pxt-hacking-stem

pelikhan commented 3 years ago

Lemme see if we can give you an api to avoid serial since that is not what is used internally to send data to the client.


From: Krzysztof Daszuta notifications@github.com Sent: Friday, October 30, 2020 3:52:23 PM To: microsoft/pxt-microbit pxt-microbit@noreply.github.com Cc: Peli de Halleux jhalleux@microsoft.com; Mention mention@noreply.github.com Subject: Re: [microsoft/pxt-microbit] USB serial monitor doesn't work on 9600 baud rate (#3537)

Does Data Streamer use the same serial? https://github.com/microsoft/pxt-hacking-stemhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fpxt-hacking-stem&data=04%7C01%7Cjhalleux%40microsoft.com%7Ce3ad5455909e4480501408d87ce36907%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637396663445689008%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=oukJ6Cai8gnEThA4%2B7KQjDLtGDoZOUcSVglNJ2Mi7zM%3D&reserved=0

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fpxt-microbit%2Fissues%2F3537%23issuecomment-719599050&data=04%7C01%7Cjhalleux%40microsoft.com%7Ce3ad5455909e4480501408d87ce36907%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637396663445689008%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=E1Sl5eib52qh4Kv9DuJl5kSQrsGuytup7DRtrPAlrkU%3D&reserved=0, or unsubscribehttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAA73QKLJ5Y55ZN2SDOBOI7DSNLHKPANCNFSM4TE77IPQ&data=04%7C01%7Cjhalleux%40microsoft.com%7Ce3ad5455909e4480501408d87ce36907%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637396663445698965%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=jpG81txJo9s%2Fn1rcCV%2F86cTDqteqfK%2FYnoocBl32gFE%3D&reserved=0.

irukard commented 3 years ago

I could potentially use a new instance of MicroBitSerial instead of using uBit.serial. However cpp extension isn't compiling in 3.1 branch right now, and I really need 3.1's serial. So I will stay with that "stupid" workaround.

pelikhan commented 3 years ago

You can try using console.log or console.logValue to send values to MakeCode directly without using serial.

irukard commented 3 years ago

I saw console.log in makecode docs, but not in microbit one. I will try.

irukard commented 3 years ago

You can try using console.log or console.logValue to send values to MakeCode directly without using serial.

No I can't. It uses the same baud rate and same redirection. Basically it's the same serial. Probably that's why there are no blocks for that in editor.

pelikhan commented 3 years ago

Right, everything goes through serial in micro:bit and we expect 115200 on the client side. So you'll need some kind of lock when you change the baud rate.

microbit-mark commented 3 years ago

Also raised in https://support.microbit.org/a/tickets/46936(private)

This program works on the hardware and you can view output in eg CoolTerm, but not in the MakeCode device console view.

image

Is this something that can be fixed @abchatra @pelikhan

abchatra commented 3 years ago

@microbit-mark . Program needs serial write line to start parsing. So add the it after write value or at certain count to start the values to appear. image

This is how the apis worked for a long time.

microbit-mark commented 3 years ago

@abchatra I don't think that is the issue here. The program in https://github.com/microsoft/pxt-microbit/issues/3537#issuecomment-891747055 works if the baud rate is set to 115200 (default). You can see the output in the device console

This works:

image

This does not:

image
abchatra commented 3 years ago

@pelikhan @mmoskal do you know more about why the baud rate makes a difference?

abchatra commented 3 years ago

@microbit-mark Also add a new line and see if that fixes the issue

mmoskal commented 3 years ago

You would need to set the interface chip to the 9600 baud rate. I don't think we ever do that, but there's a CMSIS Dap command to do so (it's just hard-coded to 115200 right now).

You can try to use external serial monitor program and specify baud rate there.

microbit-mark commented 3 years ago

You can try to use external serial monitor program and specify baud rate there.

Yeah it works fine if I set the baud rate in the MakeCode program and use https://coolterm.en.lo4d.com/ to read the serial output. It just means I have to use an external tool, rather than MakeCode to monitor output.

microbit-mark commented 3 years ago

Re-opening this as a feature request to support additional baud rates in the serial console log

abchatra commented 3 years ago

@microbit-mark this is enhancement request for daplink right? Can you move this issue to the daplink repo?

mmoskal commented 3 years ago

Daplink allows this - you would need to add UI in MakeCode with list of baud rates. IMHO this is too complicated for our users.

microbit-mark commented 3 years ago

For my understanding then, Am I right in thinking that you can use the set serial baud rate block to change the baud rate on the hardware...

image

...but this is not reflected in the browser serial console, as @pelikhan mentioned, you expect 115200 and it is this that would require additional UI?

If that is the case then it does seem complex.

@abchatra I think we should at least note that the console serial log can only be used with the default baud rate. I'd expect to see this on https://makecode.microbit.org/reference/serial/set-baud-rate which doesn't appear to exist at the moment. << cc @ganicke

Darshnik253 commented 2 years ago

I am making project smart in that i have combined bluetooth and usb serial communication code. But if i run both simultaneously then its not working(getting error 020 sad icon). so could you help in that?