Page 1 of 1

I/O Buffers trouble

Posted: Mon Jul 05, 2021 9:56 pm
by danisanba
Hi guys,

I run this code and always I am getting : Bad subscript line 15

What I am doing wrong ?

Code: [Local Link Removed for Guests]

D3=0:D10=1::D4=2:D9=3:D2=4:D1=5:D6=12:D7=13:D5=14:D8=15:D0=16

IOBUFF.DIM(1,3)

serial.mode 31250
ONSERIAL midi

wlog "load"
wait

midi:

serial.READ_IOBUFF(1)
a = IOBUFF.READ(1,0)
b = IOBUFF.READ(1,1)
c = IOBUFF.READ(1,2)
return

Re: I/O Buffers trouble

Posted: Tue Jul 06, 2021 10:56 am
by PeterN
I am not quite sure ...
... but assuming that more than one byte was send to your device, your code may be too fast.

The help file gives an example where the receive branch is slowed down a bit to get all the bytes
And it describes how to see how many bytes are there.
That might help to construct a loop to get only valid bytes.

Code: [Local Link Removed for Guests]


' I/O BUFFERS example to create a serial data logger
' receive bytes from the serial port and
' write them into the file /mylog.txt
' all the characters will be recorded
' including the ASCII 0 (NUL)
filename$ = "/mylog.txt"
 
' define the place where jump on message reception
onserial received
wait
 
received:
' waits for 10 millisec giving time to receive all the data
pause 10
' read the incoming data in the buffer 0
serial.read_iobuff(0)
size = iobuff.len(0)
print "received "; size; " bytes"
' appends the received data to the file
file.append_iobuff(0), filename$
return
I hope this was helpful for you

Re: I/O Buffers trouble

Posted: Tue Jul 06, 2021 5:29 pm
by danisanba
Hi PeterN,thank you for your reply.

I cant´t test my code because the compiler give me an error and I don´t understand where the mistake is. I have read the manual and it says

The I/O buffer can be read byte per byte using the function IOBUFF.READ(buff_num, position)
position can span from 0 (first byte) and the buffer length - 1 (last byte)
A = IOBUFF.READ(0, 7)' read in the variable A the byte 7 from the I/O buffer 0


I have this :
a = IOBUFF.READ(1,0) Works
b = IOBUFF.READ(1,1) Bad subscript line
c = IOBUFF.READ(1,2) Bad subscript line

The example program you have mentioned works, it save raw data to txt file as I have checked. I have a MIDI device send to my esp8266 serial port, 3 bytes h90 h0 h0, and I want to asign them into a variables a=90 b=0 c=0.

Thanks for helping me.

Re: I/O Buffers trouble

Posted: Tue Jul 06, 2021 6:04 pm
by bugs
Hi,

There may be nothing wrong with your program (or Annex). May I suggest, as a test, that you change the baudrate in the program to anything but the Midi rate and send the program some bytes from a serial terminal.
The reason for the test is that some USB chips on the ESP modules cannot work at 31250 baud. There is no error when you set the rate but the chip ignores it and uses the nearest rate it can.
My tests trying to get Midi to work showed that ESP modules using the CH340 chip would work but those using a Prolific chip would not.
https://groups.google.com/g/annex_wifi_ ... ZGpmqsBgAJ

Re: I/O Buffers trouble

Posted: Tue Jul 06, 2021 8:49 pm
by PeterN
Hi danisanba and bugs

Bugs may be right with the Baudrate as an additional problem.

But I understand that the sample program correctly wrote your three bytes to the file(?)
So the difference between this and your code is that there is not enough time for the buffer to be filled completely in your code.
Your program branches at the moment of receiving the first byte to the sub "midi" - but at this time there is only one byte to go into the buffer and your sub tries to read two more bytes. Your sub should simply give enough time for the next two bytes to come in before trying to fill the variables b and c. That's why I assumed it to be "too fast".
So if you insert e.g. a "pause 500" as the first line of "midi", the next two bytes should be received for sure as well in the background.
(Cicciocb inserted only a "pause 10" at his subroutine because of the high default serial baudrate.)

To make it perfect you could (after giving time for the incomming data) then read in a for-next-loop just exactly the number of bytes indicated by iobuff.len(1), as this indicates the actual number of received bytes in the buffer


Good luck!

Re: I/O Buffers trouble

Posted: Tue Jul 06, 2021 9:11 pm
by bugs
Sorry Peter, I miss-understood that the slower program had received the correct bytes. Just ignore me... ;)

Re: I/O Buffers trouble

Posted: Tue Jul 06, 2021 9:19 pm
by PeterN
Hi bugs

No need to ignore anything :-)
I remember my first experiments with Midi on a C64 - oh, that was even deep in the last century or rather millennium!

Re: I/O Buffers trouble

Posted: Wed Jul 07, 2021 1:39 pm
by danisanba
Hi bugs and PeterN.

Finally I got it. I realized than without anything connected in the serial port , at the start up of the program there is always a one byte hFF data coming from the serial port, only once.
The same behavior with the serial port connected to the midi device, always the data coming from the serial port starts with one byte hFF, only once.
So I decided to do a conditiconal with the serial.len in order to avoid this hFF data, I only treat the data with a 3 bytes minimum.
Once the program is started that hFF data never comes again.

Thank you for your support.

Regards