Web serial block typeError

Hi all!
I'm using serial block of @dardoro and I'm having a problem. The same exercise on Snap works on two PCs and on three it doesn't work.
It seems that an error occured in Webser write block.
The error is

TypeError
Cannot read the properties of undefined (reading 'close')
I attach a photo of it

Is it possible that the exercise is working faster or lower than Arduino?
I set baud rate of both at 9600 and 115200 on every PC and, as I said, on 2 of it it works. On the other 3 it doesn't.

Why?

Can you help me?

Do you use the same Arduino board to do those tests?

If a port is already opened before writing, and the permission is granted in the port selection dialog, there may be a pending write on another thread.
I've made a debug version of the library - warning, JS extension must be enabled. If you run this block once before your scripts
untitled script pic (29)
some extra infos will be displayed at the JS console.

As a makeshift, you may try to add wait(0) around write to avoid a race condition.

Thanks @dardoro for your answer!
So, I have just to add this block at the beginning of my script, right?
Actually, I'm using two different board but they are always Arduino Uno. Anyway, they work similarly so on two PCs the work and on the others they don't.
The port is not already opened, I'm pretty sure about this, because the TypeError occurs both when I start the script for the first time I open Chrome and when I start it afterwards.

About your last tip....what do you mean?

Must I use a wait block?
arduino Matrici 10 script pic

{Yes} ^2 :slight_smile:

If you run the debug version of "Serial Port" you may look at the Debug@Chrome F12>Console. There may be extra details like permission error, writer being locked etc.

I see.
Ok, I put the debug block and this is the result.

TypeError: Cannot read properties of undefined (reading 'locked')
    at eval (eval at Process.reportJSFunction (threads.js?version=2025-03-30:1448:21), <anonymous>:156:40)
    at SpriteMorph.eval (eval at Process.reportJSFunction (threads.js?version=2025-03-30:1448:21), <anonymous>:168:16)
    at Process.reportApplyExtension (threads.js?version=2025-03-30:892:16)
    at Process.doApplyExtension (threads.js?version=2025-03-30:882:10)
    at Process.evaluateBlock (threads.js?version=2025-03-30:862:36)
    at Process.evaluateContext (threads.js?version=2025-03-30:811:21)
    at Process.runStep (threads.js?version=2025-03-30:710:14)
    at threads.js?version=2025-03-30:398:18
    at Array.forEach (<anonymous>)
    at ThreadManager.step (threads.js?version=2025-03-30:393:20)

It says reading 'locked' but on Snap it said reading 'close'.

About the wait block.....are you mean this?


In this script I send 48 in decimal that is 0 in ASCII to Arduino, then I set the variable "ricevutoTOUCH" to the value Arduino give to me

Sorry, MB, I have no board at hand so only dry run the code.
There is modified version
untitled script pic (30)

Ok, I have tried to use wait block around write and it seems that now is better.
I had to reduce from 1 second to 0,2 seconds because I need that Arduino answers to me as much in real time as possible.
For this I tried to use just one wait before write block instead of one before and one after.

Now the error is this

Error: Port busy, pending write.
    at eval (eval at Process.reportJSFunction (threads.js?version=2025-03-30:1:1), <anonymous>:159:54)
    at SpriteMorph.eval (eval at Process.reportJSFunction (threads.js?version=2025-03-30:1:1), <anonymous>:171:16)
    at Process.reportApplyExtension (threads.js?version=2025-03-30:892:16)
    at Process.doApplyExtension (threads.js?version=2025-03-30:882:10)
    at Process.evaluateBlock (threads.js?version=2025-03-30:862:36)
    at Process.evaluateContext (threads.js?version=2025-03-30:811:21)
    at Process.runStep (threads.js?version=2025-03-30:710:14)
    at threads.js?version=2025-03-30:398:18
    at Array.forEach (<anonymous>)
    at ThreadManager.step (threads.js?version=2025-03-30:393:20)

It happens when the script has been running for a few seconds.

I tryed to add

Serial.flush()

in Arduino code before every

Serial.write()

I don't know if I should have done it before.
Anyway the error is still there

untitled script pic (34)
This version retries 20 times on busy (locked) port (1s total).
Also every write is logged to the console so interleaved operation can be detected.

Hi @dardoro sorry for late answers.
I tried your new block and I have many "write" and "write.done" lists in console, but how can I send you anything?
Anyway, using wait(0.05) block before the write blocks I noticed that the error doesn't occur anymore

PM are blocked here.
"copy console" with the right click menu and paste here. Limit it to a few lines surrounding write.err entry.

The debug version of the library has built-in waiting on busy. Does it not work without an external wait?


Is there something special about those troublesome computers?
They are new or old; desktop or laptop; which OS; the same model etc.


Writing a single byte should be almost instant and not lock the serial port.
Even at 9600BPS it should take less than 1 ms to write a byte.
Maybe there are power sleep/wake cycles on the USB hardware, causing a substantial delay in sending data.