BytesAvailable_Callback error in digitizer

Hello!

I am trying to use the Polhemus digitizer via Brainstorm's Digitize feature. It works correctly on my MacBook but not on the Windows Desktop (Windows 10 Enterprise, 64 bit) my lab uses for the actual recordings. I am getting the following error:

Error using panel_digitize>BytesAvailable_Callback
Too many input arguments.

Error in instrcb (line 42)
        feval(val{1}, obj, eventStruct, val{2:end});
 
Warning: The BytesAvailableFcn is being disabled. To enable the callback property
either connect to the hardware with FOPEN or set the BytesAvailableFcn property.

I have tried running both the legacy and the updated Digitize. The versions of MATLAB that I have used are R2022b and R2024b. Since I have the legacy version of Digitize running on R2022b on my MacBook, I tried that on the Windows machine but it didn't work.

How can I proceed? Is it a permission issue? While troubleshooting, I also re-installed Brainstorm as an admin by launching MATLAB as an admin and then installing BS.

Please let me know if there's any other information that I can provide.

Thanks!
Robin

Hi Robin, is that message the full error message?

It partially resample these other messages previously reported in the forum:

Hi Raymundo,

It is the full error message that I get when I press on the stylus button to collect the first point on the legacy version of Digitize. However, when I start the Digitize application, I do get the following warnings before I collect any point:

Warning: instrfind will be removed in a future release. There is no simple
replacement for this. 
Warning: serial will be removed in a future release. Use serialport instead.
If you are using serial with icdevice, continue using serial in this MATLAB release.

When I run the updated version of Digitize, I do not get any warnings when Digitize starts, but I do get a similar but longer error message when I press the stylus button to collect the first point. Here is that error in case that's helpful:

Warning: Error occurred while executing the listener callback for event DataWritten
defined for class matlabshared.asyncio.internal.InputStream:
Error using panel_digitize_2024>BytesAvailable_Callback
Too many input arguments.

Error in internal.Serialport/callbackFunction (line 1420)
            obj.BytesAvailableFcn(obj, dataAvailableInfo);

Error in internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 973)
                        obj.Transport.BytesAvailableFcn = @obj.callbackFunction;

Error in matlabshared.seriallib.internal.Serial/onDataReceived

Error in
matlabshared.seriallib.internal.Serial>@(varargin)obj.onDataReceived(varargin{:})

Error in matlabshared.asyncio.internal.Channel/onDataReceived (line 507)
                notify(obj.InputStream, 'DataWritten', ...

Error in matlabshared.asyncio.internal.Channel>@(source,data)obj.onDataReceived() (line
429)
                                         @(source, data) obj.onDataReceived()); 
> In matlabshared.asyncio.internal/Channel/onDataReceived (line 507)
In matlabshared.asyncio.internal.Channel>@(source,data)obj.onDataReceived() (line 429) 

Thanks,
Robin

Check first that the device communication is OK, see this link:
https://neuroimage.usc.edu/brainstorm/Tutorials/TutDigitize#Device_communication

Can you share the Settings that you are using for the Digitize panel?
Have you checked that the Polhemus configuration is inline with these parameters?

@Marc.Lalancette, any more suggestions?

All the device communication steps work as described on the tutorial page. I do get the error about BytesAvailable_Callback like above, but running s = instrfind() after pressing the stylus button once does show that 94 bytes are available (as should be the case for fastrak).

Here's a screenshot of the Digitize settings I am using:

Polhemus device is set as per these settings and is working correctly with the same settings on my MacBook.

Thanks,
Robin

My guess would have been something with the Matlab version 2024b, but it should work with 2022. So my next best guess is that you have function(s) on your Matlab path that are overriding the one we're expecting. I'd suggest focusing on the new panel to avoid getting confused here. And please try with a clean Matlab environment in both 2022 and 2024b and to confirm if you get the same error. I can try with 2024b later to see if I can reproduce it.

Hi Marc,

Sorry for the late response. I am only able to access the recording computer every Wednesday and I will test out your recommendations this coming Wednesday.

I'm not sure what you mean by a clean MATLAB environment. Both the R2022 and R2024b MATLAB installations were fresh installs and Brainstorm was the only thing I have tried running on them.

Do you think the issue might be there because one of the optional packages/toolboxes (supplied with MATLAB; e.g., "deep learning toolbox", "signal processing toolbox", etc.) has a function which is interfering with the Digitize application? In that case, I can request a fresh install with only MATLAB and Simulink and see if the error still occurs. I do not have the permission to install MATLAB with a license; I do have admin rights on that computer though.

And sure, I can just focus on the new panel (instead of the legacy panel) in future communications to avoid any confusion.

Thanks,
Robin

I just mean without anything else added to the Matlab path that could override standard functions. I did not suspect official MATLAB toolboxes, but maybe other tools like SPM, Fieldtrip, etc. But I'm not so sure anymore after reading your messages again. Thanks for already testing with instrfind. (I'll update this on the tutorial since we should now use serialportfind. I don't think that causes any issue though.)

I installed 2024b here and it works fine. I recommend:

  1. Use the new interface.
  2. Put a breakpoint in panel_digitize_2024.m, where it tries to read new data from the serial port object:
    Line 1040 (in my version): data = char(readline(Digitize.SerialConnection));
  3. Click to acquire a point, which will pause execution at the breakpoint.
  4. Check that Digitize.SerialConnection looks normal, e.g.:
Digitize.SerialConnection
ans = 
  Serialport with properties:

                 Port: "COM4"
             BaudRate: 9600
                  Tag: ""
    NumBytesAvailable: 94

Check that it's using the correct "readline":

which readline
C:\Program Files\MATLAB\R2024b\toolbox\matlab\serialport\interface\+internal\Serialport.m  % internal.Serialport method

Try to run that line (copy it to the command line, or highlight and press F9), and please copy again the full error message, if any. If it's similar as you already posted above, you can try to "step in" when at the breakpoint, to dig deeper and try to figure out why the error is happening, but it seems to be inside Matlab's serialport object, as if it's not communicating properly with your device, before the data gets back to Brainstorm. So there's not much we can do on our side if we can't reproduce it.

Since it's working for you on another computer, the issue could be on how it's plugged into the computer (usb adapter?) or how the device and port in the computer are configured (rate, parity, etc.).

Cheers

Hi,

Thank you for the detailed and clear instructions. It's a fresh install and only has MATLAB and Brainstorm on it.

Using the new interface on R2024b and putting the breakpoint at the said line (Line 1542 in my case), I got the following error when I clicked the stylus button to collect a point:

Warning: Error executing listener callback for PostSet event on TotalBytesWritten
dynamic property in object of matlabshared.asyncio.internal.Channel class:
Error using panel_digitize_2024>BytesAvailable_Callback
Too many input arguments.

Error in matlabshared.transportlib.internal.client.GenericClient/callbackFunction
(line 946)
            obj.BytesAvailableFcn(obj.CallbackSource, dataAvailableInfo);
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
matlabshared.transportlib.internal.client.GenericClient>@(varargin)obj.callbackFunction(varargin{:})
(line 663)
                        obj.Transport.BytesAvailableFcn = @obj.callbackFunction;
                                                           ^^^^^^^^^^^^^^^^^^^^
Error in
matlabshared.transportlib.internal.DataReceivedHandlerMixin/valuesWrittenUpdated

Error in
matlabshared.transportlib.internal.DataReceivedHandlerMixin>@(src,evt)obj.valuesWrittenUpdated(src,evt)

Error in matlabshared.asyncio.internal.Channel/onPropertyChanged (line 573)
            obj.(name) = value;
            ^^^^^^^^^^
Error in
matlabshared.asyncio.internal.Channel>@(source,data)obj.onPropertyChanged(data.Name,data.Value)
(line 469)
                                                         @(source, data)
                                                         obj.onPropertyChanged(data.Name,
                                                         data.Value));
                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
> In matlabshared.asyncio.internal/Channel/onPropertyChanged (line 573)
In matlabshared.asyncio.internal.Channel>@(source,data)obj.onPropertyChanged(data.Name,data.Value) (line 469) 

When I try to print out what's in Digitize.SerialConnection, I get: Unable to resolve the name 'Digitize.SerialConnection'.

The output of which readline matched exactly with what you got.

Please note that the error I showed earlier was for R2022b; the error for R2024b is also related to BytesAvailable_Callback but the same as what I'm getting after putting a breakpoint on the suggested line:

Warning: Error executing listener callback for PostSet event on TotalBytesWritten
dynamic property in object of matlabshared.asyncio.internal.Channel class:
Error using panel_digitize_2024>BytesAvailable_Callback
Too many input arguments.

Error in matlabshared.transportlib.internal.client.GenericClient/callbackFunction (line
946)
            obj.BytesAvailableFcn(obj.CallbackSource, dataAvailableInfo);
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
matlabshared.transportlib.internal.client.GenericClient>@(varargin)obj.callbackFunction(varargin{:})
(line 663)
                        obj.Transport.BytesAvailableFcn = @obj.callbackFunction;
                                                           ^^^^^^^^^^^^^^^^^^^^
Error in
matlabshared.transportlib.internal.DataReceivedHandlerMixin/valuesWrittenUpdated

Error in
matlabshared.transportlib.internal.DataReceivedHandlerMixin>@(src,evt)obj.valuesWrittenUpdated(src,evt)

Error in matlabshared.asyncio.internal.Channel/onPropertyChanged (line 573)
            obj.(name) = value;
            ^^^^^^^^^^
Error in
matlabshared.asyncio.internal.Channel>@(source,data)obj.onPropertyChanged(data.Name,data.Value)
(line 469)
                                                         @(source, data)
                                                         obj.onPropertyChanged(data.Name,
                                                         data.Value));
                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
> In matlabshared.asyncio.internal/Channel/onPropertyChanged (line 573)
In matlabshared.asyncio.internal.Channel>@(source,data)obj.onPropertyChanged(data.Name,data.Value) (line 469) 

Yes, the device is connected via a serial-to-USB adapter which is working for the other computer. The serial port configurations in the Device Manager for this Windows machine seem to match the required configurations.

Please suggest how can I proceed.

Thanks,
Robin

Before opening the Digitizer2024 panel, set a breakpoint in L1398, this is the first line in the CreateSerialConnection. Then open the Digitizer2024 panel and run execute line by line after the breakpoint to check if there is a trouble in opening the serial connection.

Check that the created Digitize.SerialConnection looks as Marc:

@Marc.Lalancette, can you run a quick test on the master branch?
So we can all tests the exactly same code

Was that during it being paused at the breakpoint?

Seems I'm not using the same version, so I'll go try the main branch later today as Raymundo suggested.

Ok, so I can reproduce this with the current Brainstorm branch. I'll investigate today.

Hi Raymundo,

After setting the breakpoint at L1398, I opened BS and then Digitize panel. This opened the MATLAB script for the panel and took me to the breakpoint. After that, I executed the commands line by line until I finished the whole section for creating the serial connection and the next line execution automatically jumped to an early line (~L150).

After pressing the stylus button at this point, I did get the same output as Marc:

Digitize.SerialConnection

ans = 

  Serialport with properties:

                 Port: "COM7"
             BaudRate: 9600
                  Tag: ""
    NumBytesAvailable: 94

  Show all properties, functions

To answer Marc's question, yes I got the Unable to resolve the name 'Digitize.SerialConnection' error when the script was paused at the breakpoint. I'd be happy to test that without the breakpoint as well if needed.

How should I proceed?

Thanks,
Robin

@parthchholak, we figured out the trouble. See commit:
Please update your Brainstorm instance (to 11-Dec-2024) and try it again

Yup, that fixed it!! Thanks so much. :slight_smile:

There's a new issue with it now. I can start a separate bug report for it but since it might be related to what you just updated in the software for this issue, I am bringing it up here.

Whenever, I use the delete last point button, I get the following error:

***************************************************************************
** Error: Line 1100: Matrix index is out of range for deletion.
** 
** Call stack:
** >panel_digitize_2024.m>PlotCoordinate at 1100
** >panel_digitize_2024.m>DeletePoint_Callback at 948
** >bst_call.m at 28
** >panel_digitize_2024.m>@(h,ev)bst_call(@DeletePoint_Callback) at 1656
** 
***************************************************************************

Best,
Robin

Indeed, same here. It's fine to keep going in this thread.

@parthchholak, This is now solved at Commit 10c902a
Please update your Brainstorm instance.

Indeed that fixed the "Delete last point" issue, and I was able to run a full digitization successfully. However, the saved digitization files are not visible in the BS interface. I did press "Save in database and exit" and can see the saved digitization files in the "brainstorm_db" folder, but they are not visible in the BS UI.