Read N bytes from a channel to a memory buffer

Anything QL Software or Programming Related.
Post Reply
User avatar
Andrew
Aurora
Posts: 786
Joined: Tue Jul 17, 2018 9:10 pm

Read N bytes from a channel to a memory buffer

Post by Andrew »

Is there any function that can read N bytes from a channel and store them at a specific address in memory ?
DJ Toolkit defines Fetch_Bytes : a$ = FETCH_BYTES(#channel, how_many) but reads to a string
I need something like FETCH_BYTES#channel, how_many, address
How can I achieve this in Basic?


User avatar
Artificer
Brittle Membrane
Posts: 120
Joined: Fri Nov 24, 2017 8:43 am

Re: Read N bytes from a channel to a memory buffer

Post by Artificer »

Hi Andrew,

Toolkit 2 : For example.


BGET#ch\position_in_file,a$(1 TO number of bytes)

a$ should be DIMed to the number of bytes to be read. The file position should be set to 0 or further on in the file as required, and the channel opened to the file.
The string can be POKED in to memory if required with POKE$ : - found in smsq/e or Turbo toolkit or DIY toolkit and perhaps else where.

Cheers


User avatar
Andrew
Aurora
Posts: 786
Joined: Tue Jul 17, 2018 9:10 pm

Re: Read N bytes from a channel to a memory buffer

Post by Andrew »

Artificer wrote:Hi Andrew,

Toolkit 2 : For example.


BGET#ch\position_in_file,a$(1 TO number of bytes)

a$ should be DIMed to the number of bytes to be read. The file position should be set to 0 or further on in the file as required, and the channel opened to the file.
The string can be POKED in to memory if required with POKE$ : - found in smsq/e or Turbo toolkit or DIY toolkit and perhaps else where.

Cheers
I wanted to avoid the POKE .
is there a way to find the address of a variable ? If I find the address of a$ then I do not need the poke


User avatar
Pr0f
QL Wafer Drive
Posts: 1298
Joined: Thu Oct 12, 2017 9:54 am

Re: Read N bytes from a channel to a memory buffer

Post by Pr0f »

didn't Simon Goodwin's MEM device offer the ability to write to memory as a device - not entirely sure how safe this would be given that the way QL uses memory, you could be writing over anything (slave blocks, Basic, loaded drivers, and there is no memory protection as such, unless the MEM device verifies the address is ok before letting you scribble into it.

The Mem device was in the DIY toolkit.


User avatar
NormanDunbar
Forum Moderator
Posts: 2251
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Read N bytes from a channel to a memory buffer

Post by NormanDunbar »

Volume N, for Networking at http://simon.mooli.org.uk/QL/DIY_Toolkit.html. Volume Z, Array Search, might show you how to get the address of a variable. But beware of fiddling w=in the bowels of strings and arrays using their address. They may/will have "metadata" stored there too, which SuperBASIC will need.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
martyn_hill
Aurora
Posts: 909
Joined: Sat Oct 25, 2014 9:53 am

Re: Read N bytes from a channel to a memory buffer

Post by martyn_hill »

Hi Andrew

The underlying QDOS 'FSTRG' TRAP is designed the way you need - reading bytes from a channel, directly in to a bounded memory buffer.

To access this IO Trap From SBasic, you could use DIYTK's 'QTRAP' extension (Volume T), providing '3' for the TRAP ID/Operation Key, thus:

QTRAP #<input_channel>, <trap_key> [, param]{0..5}

Read-up the associated docs from Simon's DIYTK article on the Trap extensions to find out how to specify the D/A Register params - and how to read the contents of relevant registers upon return, with reference to the the QDOS Trap definition (also available in Simon's docs.)

3 IO.FSTRG Fetches a string of bytes

Parameters Results
D1 D1.W number of bytes fetched
D2.W length of buffer D2 preserved
D3.W Timeout D3 preserved
A0.L Channel ID A0 preserved
A1.L base of buffer A1 updated pointer to buffer
A2 A2 preserved

Errors : NC,NO,EF

Good luck!


User avatar
Artificer
Brittle Membrane
Posts: 120
Joined: Fri Nov 24, 2017 8:43 am

Re: Read N bytes from a channel to a memory buffer

Post by Artificer »

Hi Andrew,

You might like also to take a look at FREAD and FWRITE in the TinyToolkit. FREAD reads a number of bytes from a channel into memory at a given address. Syntax :

bytes_read=FREAD(#chan,buffer_address,size_of_bytes_to_read)

Tiny toolkit is on Dilwyn Jones toolkits downloads page.

Cheers


User avatar
Andrew
Aurora
Posts: 786
Joined: Tue Jul 17, 2018 9:10 pm

Re: Read N bytes from a channel to a memory buffer

Post by Andrew »

Thank you Artificier and Martyn - that was what I was looking for !


Post Reply