Re: Pcomm Serial Port interface

new topic     » goto parent     » topic index » view thread      » older message » newer message

> Here is the declarition in Pcomm.ew:
> }}}
<eucode>
>    sio_read_=define_c_func(Pcomm_,"sio_read",{C_INT, C_INT, C_INT}, C_INT)
> 
> and the actuall function
> 	--(int port, char *buf, int len)
>    global function sio_read(integer port, integer lpRBuf, integer len)
> 		return c_func(sio_read_,{port,lpRBuf,len})
>    end function
> </eucode>
{{{

> 
> Here is the info from the Pcomm documentation:
> Language      Syntax
> C/C++	      int WINAPI sio_read(int port, char *buf, int len)
> Visual Basic  Function sio_read(ByVal port As Long,ByRef buf As Byte ,ByVal
> len As Long) As Long
> Delphi        function sio_read(port: Longint; buf: PChar; len: Longint):
> Longint;

Hi Alex

I would imagine it would be something like (untested):

include get.e -- for wait_key()
include machine.e -- for allocate() and free()
include msgbox.e  -- Pcomm seems to need this but doesn't include it
include Pcomm.ew -- for Pcomm library

-- program initialisation
constant BUFSIZE = 100 -- how many bytes to read at once
constant COMPORT = 1   -- COM1
integer len            -- bytes returned from sio_read...
sequence tmp, data
atom buffer

len = sio_open(COMPORT) -- "len" is just a convenient integer
if len != SIO_OK then
        len = message_box("Can't open com port", "Error", MB_OK)
    abort(1)
end if

buffer = allocate(BUFSIZE)
if buffer = 0 then
   puts(1, "Unable to allocate buffer\n")
   abort(1)
end if

-- somewhere else in the program...

-- reading complete buffer on COM2, assuming data is waiting
data = {}
len = -1 -- to enter the next loop
while len != 0 do
    len = sio_read(COMPORT, buffer, BUFSIZE)
    if len != 0 then -- avoid reading if no data returned
        tmp = peek({buffer, len}) -- returns sequence of bytes in tmp{}
        data = append(data, tmp)
    end if
end while


-- use the data
if length(data) then
len = message_box(sprintf("%d bytes read", {length(data)}), "Data present",
    MB_OK)
    for i = 1 to length(data) do
        puts(1, i)
    end for
else
    len = message_box("No Data", "No Data", MB_OK)
end if

free(buffer)
len = sio_close(COMPORT)
len = wait_key() -- to see the output


The basic idea is that you use allocate to get the pointer to the memory, and
peek to read the result into a sequence, you can then use this sequence any way
you want.

This is not really tested though, all I get is "No Data" smile

Gary

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu