1. Pcomm Serial Port interface
- Posted by woodengineer <woodengineer at aol.com> Nov 26, 2006
- 1156 views
- Last edited Nov 27, 2006
Hello! I am new to Euphoria and try to get Euphoria to read the input from my Scanner connected to my PC-Serial Port. I am using the Pcomm.dll interface. The function I am trying to get to work is the sio_read function which should read a block of Data. I don't know what variables to setup in Eu to call this function. Here is the declarition in Pcomm.ew:
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
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; Please keep in mind that I am totally new to Eu. If someone could provide a piece of sample code I would be eternally greatful!! Thanks in advance!!! Alex
2. Re: Pcomm Serial Port interface
- Posted by ags <eu at 531pi.co.nz> Nov 26, 2006
- 1130 views
- Last edited Nov 27, 2006
> 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" Gary
3. Re: Pcomm Serial Port interface
- Posted by Mario Steele <eumario at trilake.net> Nov 30, 2006
- 1125 views
Wow, I'm surprised I was the first one to notice this...... Kinda sad..... Anyways, the function should be defined as such:
sio_read_=define_c_func(Pcomm_,"sio_read",{C_INT,C_POINTER,C_INT},C_INT) global function sio_read(integer port, atom lpRBuf, integer len) return c_func(sio_read_,{port,lpRBuf,len}) end function
Then in your code:
-- include get.e -- for wait_key() << Isn't needed, since message_box() is being used. include machine.e -- For allocate(), free() include msgbox.e -- For nice Message Box include Pcomm.e -- Need to fix the definition with what is provided above function MySIORead(integer port, integer len) atom mem integer ret sequence bytes mem = allocate(len) ret = sio_read(port,mem,len) if ret = 0 then free(mem) return {} else bytes = peek({mem,ret}) free(mem) return bytes end if end function sequence my_scan, tmp object void my_scan = {} void = sio_open(1) while 1 do tmp = MySIORead(1,100) -- Only grab 100 bytes at a time if equal(tmp,{}) then break else my_scan &= tmp end end void = sio_close(1) if length(data) then void = message_box(sprintf("%d bytes read",{length(data)}), "Data Present",MB_OK) else void = message_box(sprintf("No data returned.","No Data",MB_OK) end if
Amazing what you can code, with not so much pratice in the language for a while, and alot of pratice in another language. *********** DISCLAIMER *********** The above code, is in no way garunteed to work, or not cause a major malfunction. Use at your own Risk! *********** DISCLAIMER *********** Mario Steele http://enchantedblade.trilake.net Attaining World Dominiation, one byte at a time...