FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Readcomm & EnableCommNotification
Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Readcomm & EnableCommNotification
Posted: Wed Feb 04, 2009 12:00 PM
Hi,

I'm trying to convert my customers 16bit app to 32bit and i'm having a problem with Readcomm. Their pc is connected to a serial swipe card, so i'm using EnableCommNotification to initiate the reading of the comm port however, i seem to only read in 8 characters. I have copied my 16bit code (which works perfectly) and was wondering if there were other changes needed?

When my dialog is defined i do this:

oDlg:bCommNotify := { | nComm, nStatus | get_card( nComm, nStatus ) }

And on the ON INIT of the dialog, i have the following:

EnableCommNotification( nComm, oDlg:hWnd, 1, -1 ))

My Get_Card function:

FUNCTION get_card(m_port,stat)
                LOCAL startstr, buffer, sensorstr := ""
	buffer := SPACE(100)
	//turn off comm notification for this function
 	EnableCommNotification( m_port, wbent:hWnd, -1, -1 )
	FlushComm( m_port, 0 )						  
	//wait long enough for port to fill
	
	waitseconds(1)                        //Doesnt matter how long i increase this
	? ReadComm(m_port,@buffer) //Always returns 8


Any ideas?

Thanks in advance

Pete
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Readcomm & EnableCommNotification
Posted: Wed Feb 04, 2009 12:22 PM
PeterHarmes wrote:
	//turn off comm notification for this function
 	EnableCommNotification( m_port, wbent:hWnd, -1, -1 )


Try

EnableCommNotification( m_port, 0, 1, -1 )


and after reading

EnableCommNotification( m_port, wbent:hWnd, 1, -1 )


EMG
Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Readcomm & EnableCommNotification
Posted: Wed Feb 04, 2009 12:36 PM

Same results i'm afraid

Pete

Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Readcomm & EnableCommNotification
Posted: Fri Feb 06, 2009 10:46 AM

Anyone got any other ideas?

Regards,

Pete

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Readcomm & EnableCommNotification
Posted: Fri Feb 06, 2009 11:22 AM
Please build a self-contained sample of the problem. The following sample works fine here with a modem:

#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL oDlg

    LOCAL oGet, cTxt := ""

    LOCAL nCom

    DEFINE DIALOG oDlg;
           SIZE 500, 500;
           TITLE "Terminale"

    @ 0, 0 GET oGet VAR cTxt MEMO READONLY

    oGet:bKeyDown = { | nKey | Tasti( nCom, nKey ) }

    ACTIVATE DIALOG oDlg;
             ON INIT ( oGet:AdjClient(),;
                       nCom := APRICOM( oDlg, oGet ),;
                       IF( nCom < 0, oDlg:End(), ) );
             CENTER

    IF nCom >= 0; CLOSECOMM( nCom ); ENDIF

    RETURN NIL


STATIC FUNCTION TASTI( nCom, nKey )

    SENDSTR( nCom, CHR( nKey ) )

    RETURN NIL


STATIC FUNCTION APRICOM( oDlg, oGet )

    LOCAL nCom, cDcb

    BEGIN SEQUENCE
        nCom = OPENCOMM( "COM1", 16384, 16384 )

        IF nCom < 0
            ? "Errore di apertura della porta di comunicazione."
            BREAK
        ENDIF

        BUILDCOMMDCB( "COM1:115200,N,8,1", @cDcb )

        IF !SETCOMMSTATE( nCom, cDcb )
            ? "Errore di impostazione della porta di comunicazione."
            BREAK
        ENDIF

        oDlg:bCommNotify = { | nCom | Connect( nCom, oGet ),;
                                      EnableCommNotification( nCom, oDlg:hWnd, 1, -1 ) }

        IF !ENABLECOMMNOTIFICATION( nCom, oDlg:hWnd, 1, -1 )
            ? "Errore di abilitazione della notifica."
            BREAK
        ENDIF
    RECOVER
        nCom = -1
    END SEQUENCE

    RETURN nCom


STATIC FUNCTION CONNECT( nCom, oGet )

    LOCAL cStr

    ENABLECOMMNOTIFICATION( nCom, 0, 1, -1 )

    cStr = RECEIVESTR( nCom )

    cStr = STRTRAN( cStr, CHR( 13 ), "" )
    cStr = STRTRAN( cStr, CHR( 10 ), CRLF )

    oGet:Append( cStr )

    RETURN NIL


STATIC FUNCTION SENDSTR( nCom, cString )

    LOCAL nBytes := WRITECOMM( nCom, cString )

    RETURN nBytes = LEN( cString )


STATIC FUNCTION RECEIVESTR( nCom )

    LOCAL cBuf := SPACE( 1000 )

    RETURN LEFT( cBuf, READCOMM( nCom, @cBuf ) )


EMG
Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Fri Feb 06, 2009 11:47 AM
I've just created a test program, however this is worse than my application as readcomm always returns 0 :-)

#include "FiveWin.ch"

STATIC oDlg, nComm
STATIC cGet, oGet

function Main()
	cGet := Space(10)
   	SET _3DLOOK ON

   	DEFINE DIALOG oDlg TITLE "Test" 

   	@ 1,    2 SAY "Text..:" OF oDlg
   	@ 1,    6 GET oGet VAR cGet OF oDlg SIZE 60, 10 COLOR "W/G" PICTURE "@K"

   	@ 3,    7 BUTTON "&Ok" OF oDlg SIZE 30, 12 ACTION oDlg:End()
   	@ 3,   16 BUTTON "&Cancel" SIZE 30, 12 OF oDlg ACTION oDlg:End() CANCEL
	
	oDlg:bCommNotify := { | nComm, nStatus | get_card( nComm, nStatus ) }
	
   	ACTIVATE DIALOG oDlg CENTERED ON INIT ;
   	 	(openport(),;
		EnableCommNotification( nComm, oDlg:hWnd, 1, -1 ))
return nil

FUNCTION openport
	LOCAL  cDcb , initok , str_send, mScale, mComPort
	mComPort := "COM1"

	nComm    := OpenComm(mComPort,1024,128)
	IF nComm >= 0  
		str_send :=	mComPort+":9600,E,7,1"
		initok := BuildCommDcb(str_send , @cDcb )
		SetCommState( nComm, cDcb )
		EscapeCommFunction( nComm, "SETRTS")
		FlushComm( nComm, 0 )
	ELSE
		? "Port not functioning "
	ENDIF
RETURN NIL

FUNCTION get_card(m_port,stat)
	LOCAL startstr, buffer, sensorstr := ""
 	EnableCommNotification( m_port, oDlg:hWnd, 0, -1 )
	FlushComm( m_port, 0 )						  
	waitseconds(2)

	? ReadComm(m_port,@buffer)	
	EnableCommNotification( nComm, oDlg:hWnd, 1, -1 )
RETURN .T.
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Fri Feb 06, 2009 11:51 AM

Please try my sample.

EMG

Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Fri Feb 06, 2009 12:10 PM
This is weird...

Your example works perfectly.

If instead of readcomm, i call your function recevestr i get the first 8 chars, then the next 2 (the computer i'm attached to is sending 10 characters) any ideas why it would be doing this?

STATIC oDlg, nComm
STATIC cGet, oGet

function Main()
	cGet := Space(10)
   	SET _3DLOOK ON

   	DEFINE DIALOG oDlg TITLE "Test" 

   	@ 1,    2 SAY "Text..:" OF oDlg
   	@ 1,    6 GET oGet VAR cGet OF oDlg SIZE 60, 10 COLOR "W/G" PICTURE "@K"

   	@ 3,    7 BUTTON "&Ok" OF oDlg SIZE 30, 12 ACTION oDlg:End()
   	@ 3,   16 BUTTON "&Cancel" SIZE 30, 12 OF oDlg ACTION oDlg:End() CANCEL
	
	oDlg:bCommNotify := { | nComm, nStatus | get_card( nComm, nStatus ) }
	
   	ACTIVATE DIALOG oDlg CENTERED ON INIT ;
   	 	(openport(),;
		EnableCommNotification( nComm, oDlg:hWnd, 1, -1 ))
return nil

FUNCTION openport
	LOCAL  cDcb , initok , str_send, mScale, mComPort
	mComPort := "COM1"

	nComm    := OpenComm(mComPort,1024,128)
	IF nComm >= 0  
		str_send :=	mComPort+":9600,E,7,1"
		initok := BuildCommDcb(str_send , @cDcb )
		SetCommState( nComm, cDcb )
		EscapeCommFunction( nComm, "SETRTS")
		FlushComm( nComm, 0 )
	ELSE
		? "Port not functioning "
	ENDIF
RETURN NIL

FUNCTION get_card(m_port,stat)
	LOCAL startstr, buffer, sensorstr := ""
 	EnableCommNotification( m_port, 0, 1, -1 )
	FlushComm( m_port, 0 )						  
	waitseconds(2)

	//? ReadComm(m_port,@buffer)	
	
	Buffer := RECEIVESTR( m_port )
	? Buffer
	EnableCommNotification( nComm, oDlg:hWnd, 1, -1 )
RETURN .T.

STATIC FUNCTION RECEIVESTR( nCom )

    LOCAL cBuf := SPACE( 1000 )

RETURN LEFT( cBuf, READCOMM( nCom, @cBuf ) )
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Fri Feb 06, 2009 12:30 PM

Try to remove delays and flushes or other stuff one at a time.

EMG

Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Fri Feb 06, 2009 01:06 PM

Ok i've put some debug code into your example and your code is working the same as mine - when the 10 characters are received your function connect is called twice - the first time to receive 8 characters and the 2nd to receive the final 2 characters. Do you know why this is only receiving 8 chars at a time as this was not the case in the 16bit version of the communication functions?

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Fri Feb 06, 2009 02:34 PM

That's the normal and expected behavior of the communication functions.

EMG

Posts: 363
Joined: Wed Feb 15, 2006 02:06 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Mon Feb 09, 2009 08:14 AM

Really?!?!! - Thats a real pain - do you (or anyone else) know why this was changed between 16 & 32 bit versions?

Regards,

Pete

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Readcomm &amp; EnableCommNotification
Posted: Mon Feb 09, 2009 08:28 AM
PeterHarmes wrote:Really?!?!!


Yes. :-)

PeterHarmes wrote:- Thats a real pain - do you (or anyone else) know why this was changed between 16 & 32 bit versions?


No, it wasn't changed at all. Please note that serial communications is extremely slow. If you want to read all the characters just make a loop and exit when there are no more chars to read after a reasonable timeout. This is called "polling" that is very inefficient.

EMG

Continue the discussion