Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP and ReadFile() on WIN32API

Status
Not open for further replies.

morenop

IS-IT--Management
Apr 13, 2004
2
US
I'm trying to read from the COM1 port from a scanner, I do the test of the (createfile) but when I do the Readfile() I get nothing, can someone help?

This is the code :
clear
hPort =0
DECLARE INTEGER CreateFile IN kernel32;
STRING lpFileName, INTEGER dwAccess, INTEGER dwShareMode,;
INTEGER lpSecurityAttr, INTEGER dwCreationDisp,;
INTEGER dwFlagsAndAttr, INTEGER hTemplateFile

DECLARE INTEGER ReadFile IN kernel32;
INTEGER hFile,;
STRING @ lpBuffer,;
INTEGER nNumberOfBytesToRead,;
INTEGER @ lpNumberOfBytesRead,;
INTEGER lpOverlapped

DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
DECLARE INTEGER GetLastError IN kernel32

DECLARE INTEGER FormatMessage IN kernel32;
INTEGER dwFlags, INTEGER lpSource, INTEGER dwMessageId,;
INTEGER dwLanguageId, INTEGER @lpBuffer,;
INTEGER nSize, INTEGER Arguments

DECLARE RtlMoveMemory IN kernel32 As CopyMemory;
STRING @Destination, INTEGER Source, INTEGER nLength

cPort="COM1:"
?"Testing port " + m.cPort + ":", TestPort(m.cPort)
wait
?"Contents :"
??ReadCont(hport)

FUNCTION TestPort(cPort)
#DEFINE FILE_SHARE_READ 1
#DEFINE FILE_SHARE_WRITE 2
#DEFINE OPEN_EXISTING 3
#DEFINE GENERIC_READ 0x80000000
#DEFINE GENERIC_WRITE 0x40000000
#DEFINE FILE_FLAG_OVERLAPPED 0x40000000
#DEFINE INVALID_HANDLE_VALUE -1
#DEFINE FILE_ATTRIBUTE_NORMAL 128

LOCAL lnErr

* hPort = CreateFile(cPort, GENERIC_READ, 0,0,;
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)

hPort = CreateFile(cPort,;
BITOR(GENERIC_READ,GENERIC_WRITE),;
0,0, OPEN_EXISTING,;
BITOR(FILE_FLAG_OVERLAPPED,FILE_ATTRIBUTE_NORMAL), 0)

IF hPort = INVALID_HANDLE_VALUE
lnErr = GetLastError()
RETURN "Error " + TRANSFORM(lnErr) +;
". " + GetErrorMessage(lnErr)
ELSE
* = CloseHandle(hPort)
RETURN "Ok"
ENDIF

Function ReadCont(hport)
?Fgets(hport,255)
cbuff=Space(255)
dwread=0
=ReadFile(hport,@cbuff,len(cbuff),@dwread,0)
??"data:",cbuff
If ReadFile(hport,@cbuff,len(cbuff),@dwread,0)>0
??"data:",cbuff
Else
Endif
= CloseHandle(hPort)
Return Cbuff


FUNCTION GetErrorMessage(lnErr)
#DEFINE FORMAT_MESSAGE_ALLOCATE_BUFFER 256
#DEFINE FORMAT_MESSAGE_FROM_SYSTEM 4096
#DEFINE FORMAT_MESSAGE_IGNORE_INSERTS 512

LOCAL dwFlags, lpBuffer, lnLength, lpResult
dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER +;
FORMAT_MESSAGE_FROM_SYSTEM + FORMAT_MESSAGE_IGNORE_INSERTS

lpBuffer = 0
lnLength = FormatMessage(dwFlags, 0, lnErr, 0, @lpBuffer, 0,0)
IF lnLength <> 0
lpResult = REPLI(Chr(0), 500)
= CopyMemory (@lpResult, lpBuffer, lnLength)
RETURN STRTRAN(LEFT(lpResult, lnLength), Chr(13)+Chr(10), "")
ELSE
RETURN "[]"
ENDIF
 
Why not just use the MSCOMM ActiveX control?
You can't handle COM port that way in VFP. You didn't use WaitForSingleObject anywhere in your code, also you didn't set all settings of COM (parity, speed, stop bits etc.)
Check On that site there is a Serial Comunication library for C++ if you could manage C++Code you could easily build a COM object that can communicate via COM ports.
But my advice is use MSCOMM it is easest way.

Borislav Borissov
 
I now is easier with MSCOMM but it seems that it sends some garbage sometimes, that why I'm trying to use "RAW stuff", I set the COM settings on the system properties, assuming it will take them as they are. What is the "WaitForSingleObject"? (I'm new on this)....
 
The WaitForSingleObject function returns when one of the following occurs:· The specified object is in the signaled state. The time-out interval elapses.

Also, go to that link I posted, the man describes how to comunicate very well.

BTW why MSCOMM sends garbage? Maybe it isn't set right?

Borislav Borissov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top