What is the Serial Ports library for?

So I was looking at the libraries today in the Snap! 7 dev, and I noticed a new one- Serial Ports. I played around with it for a bit and couldn't get it to do anything... or figure out what the blocks were even for. Does anyone know what these blocks are intended to do??

They are for communicating with serial devices.

There are devices such as an Arduino, that can be connected to lots of things such as LEDs, switches, motors etc

And you can send information to and from them with a computer, using a serial lead.

With these blocks, Snap can now communicate to such devices to interface to the real world.

An example would be controlling the motors on a robot

This is taking advantage of a fairly new browser feature, not in all browsers yet. We used to have to communicate with serial devices by writing an external program in Python or something that would set up a web server on localhost and respond to commands sent via the URL block.

This interface is a wonderful contribution by modestly talented Dariusz (aka: dardoro).
Don't just think Arduino, think any UART capable device: micro:bit, RP series, ESP series, mobile phones, etc.
Also, don't just think hardware. This addition also makes software interface to other worlds very easy. Case in point: MicroBlock's VM running on any of the supported 32 bit platforms. AppInventor over serial or Bluetooth connections.You have the ability to reach in any touch any piece of code or variable on these platforms in both directions to/from SNAP. For an example, check out the MicroBlocks Wiki under SNAP projects.
Serial Library enhances Snap tremendously and removes any communications barriers. Thank you Dariusz for creating it and thank you Jens for deeming it worthy to be a part of SNAP!

That sounds really cool. Will try it out!!!

Can firefox do this?

According to the Serial - Web APIs | MDN, only desktop Chrome & derivatives.

Now that @cymplecy has solved your problem/answered your question, why don't you just pin his comment as the solution by clicking on the checkmark (Unless you can't)?

you can only do that in a few categories, but you can always switch to #help, select a solution, and switch back.

Where could I have a example in action of this new blocks?

MicroBlocks WİKİ link Interacting with Snap! | MicroBlocks Wiki should take you to a section where two examples are provided.
One is a VM interface to Microblocks engine for SNAP to MB communication.
The other is a data streaming example by J.Maloney.

Serial library was develoed by Darius (user: @dardoro) and he may have further examples on how to talk to Arduino type microprocessors.

In general, you should be able to exchange data with anything that supports a UART based serial interface. If the device does not support an USB interface, then a USB-TTL interface board can be used as an intermediary to change the connection type. This will allow one to interface to devices like HC05 Bluetooth cards etc.

Another neat usecase is the exchange with an Android phone over the USB connection. You will need an OTA cable for that. And for easy software dev option look at APPINVENTOR. Examples for this are also provided in the link:

If you wish to stay within the confines of SNAP, then you can utilize either two USB ports on your PC or use a loopback connection to test the send and receive functions. Just make sure to set both sides to the same speed.

Enjoy.

"Serial Libary" is a minimal, self-describing, "Web Serial API" implementation for Snap!
The library can be used to streamline & simplify physical computing and real-world interactions with external hardware.

If you are familiar with Arduino IDE, "Serial Ports" act like "Serial Monitor". You can read data sent by the serial interface of your microcontroller board and control it by sending data.
If you are new to microcontrollers you may start working with them with Microblocks, the Snap! sibling, using the information provided by Turgut (tguneysu).

Do you try to work with some real hardware?

Yes, we would love to talk to an Arduino directly with Snap! (Although Snap4Arduino has been great.)

Would it be possible to create a version of Firmata Test in Snap! using the Web Serial API?

Absolutely. And with more flexibility than any imposed framework like firmata (not that anything is wrong with it). Usually, one thinks primarily in terms of physical interaction with methodologies like firmata. However, there is whole different software side, where one interacts with the code running on another system. Take a look at the MicroBlocks VM interface example, where not only the hardware of a micro:bit, but also the underlying VM routines can be interfaced with.
Also, serial comms can act as a gateway intermediary, driving other serial capable technologies like Bluetooth, RF, LaserComms.

This sounds terrific. Would it be possible to provide a demonstration in Snap! illustrating how the Web Serial block could be used to turn on Pin 13 on an Arduino Uno (turning on the LED on the Arduino board)?

image

Hi Glen
Here is demo program set for you:
SNAP: glenbullUARTDemo.xml (Use R-Click + "save link as")
ARDUINO: glenbullUARTDemo.ino

  • Plug your Arduino to the USB on the PC
  • Compile and load the Arduino program
  • You can test it by opening the Monitor and entering 0 or 1
    Monitor should be set to no line ending and 9600 baud.
    These can be changed later once you verify everything.
    If all is well, close the Monitor.
  • Start SNAP and load the program
  • Click the Green flag to start running.
  • Then select LEDON or LEDOFF on the sendSER block
    image
  • Click the block to send
  • Repeat with different settings
  • When you are done, click the DONE block to close everything.

NOTE:
Remember to not use the Arduino or SNAP editing cycles while the other program's Serial port use is active. During editing, COM port will conflict in either program, if the other is using it.

For simplicity sake, I have used a 0/1 (ascii 48 / 49) code to transmit commands. But text strings of any design can be used as well; just makes for a bit more work on the parsing side.

Line feed terminated msgs make the Serial blocks act right away. Without them, there is an inherent timeout that triggers eventually.

Enjoy.

Thanks so much! We'll look forward to exploring.

It's important to note, that, because of the browser restrictions, "user gesture" is required prior to accessing hardware. So when you open a shared project from the cloud, the auto-started script may not open the port properly.
Also, Serial Library is "byte-oriented". Data is transferred as a list of byte values.
And read is "non-blocking". It returns a list of bytes or "true" when no data is available.

I've made a sample with "read"
Arduino sketch:

void setup() {
  pinMode( LED_BUILTIN, OUTPUT);
  Serial.begin( 115200);
  while (!Serial){delay(50);};
  Serial.println( "Arduino at your service...");
}

byte on_off;
void loop() {
  if (Serial.available() > 0) {
      on_off = Serial.read();
      digitalWrite( LED_BUILTIN, (on_off == 0)? LOW : HIGH);
  }
}

Snap part - slightly redundant...
untitled script pic - 2022-01-16T000139.298

Thanks so much for developing the Web Serial block. It is going to open up a lot of possibilities.