Serial port

Post Reply
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Serial port

Post by Enrico Maria Giordano »

I'm trying to use the serial port through the emulator using the following test sample:

Code: Select all

#include "Fwce.ch"


FUNCTION MAIN()

    LOCAL oWnd

    LOCAL cCom := " "

    LOCAL cCod := ""

    LOCAL oTmr, hSer, oGet

    DEFINE WINDOW oWnd;
           TITLE "Test seriale"

    @ 1, 1 SAY "Numero porta:"

    @ 1, 10 GET cCom;
            SIZE 30, 20;
            PICTURE "9";
            VALID !EMPTY( cCom )

    @ 3, 1 GET oGet VAR cCod MEMO;
           SIZE 200, 100

    @ 10, 1 BUTTON "Apri porta";
            SIZE 100, 20;
            ACTION hSer := APRI( cCom )

    @ 10, 20 BUTTON "Esci";
             SIZE 70, 20;
             ACTION oWnd:End();
             CANCEL

    DEFINE TIMER oTmr OF oWnd;
           INTERVAL 1000;
           ACTION LEGGI( oTmr, hSer, oGet )

    ACTIVATE WINDOW oWnd;
             ON INIT oTmr:Activate()

    IF hSer != -1; CLOSEHANDLE( hSer ); ENDIF

    oTmr:End()

    RETURN NIL


#define GENERIC_READ 0x80000000 
#define GENERIC_WRITE 0x40000000
#define OPEN_EXISTING 3 
#define FILE_ATTRIBUTE_NORMAL 0x00000080 


STATIC FUNCTION APRI( cCom )

    LOCAL hSer := CREATEFILE( "COM" + cCom + ":", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL )

    IF hSer != -1
        MSGINFO( "Porta COM" + cCom + " aperta correttamente" )
    ELSE
        MSGINFO( "Impossibile aprire la porta COM" + cCom )
    ENDIF

    RETURN hSer


STATIC FUNCTION LEGGI( oTmr, hSer, oGet )

    LOCAL cTxt := ""

    LOCAL nChr := 0

    IF hSer = NIL .OR. hSer = -1; RETURN NIL; ENDIF

    oTmr:DeActivate()

    WHILE nChr != 0
        nChr = BIN2L( READBYTE( hSer ) )
        cTxt += CHR( nChr )
    ENDDO

    IF !EMPTY( cTxt )
        oGet:VarPut( cTxt )
        oGet:Refresh()
    ENDIF

    oTmr:Activate()

    RETURN NIL
Someone could test it on a real pocket PC?

A question: is it possible (and needed) to set the port parameters (ie. baud rate, stop bits) and how?

EMG
User avatar
Richard Chidiak
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France
Contact:

Re: Serial port

Post by Richard Chidiak »

EnricoMaria wrote:I'm trying to use the serial port through the emulator using the following test sample:

Code: Select all

#include "Fwce.ch"


FUNCTION MAIN()

    LOCAL oWnd

    LOCAL cCom := " "

    LOCAL cCod := ""

    LOCAL oTmr, hSer, oGet

    DEFINE WINDOW oWnd;
           TITLE "Test seriale"

    @ 1, 1 SAY "Numero porta:"

    @ 1, 10 GET cCom;
            SIZE 30, 20;
            PICTURE "9";
            VALID !EMPTY( cCom )

    @ 3, 1 GET oGet VAR cCod MEMO;
           SIZE 200, 100

    @ 10, 1 BUTTON "Apri porta";
            SIZE 100, 20;
            ACTION hSer := APRI( cCom )

    @ 10, 20 BUTTON "Esci";
             SIZE 70, 20;
             ACTION oWnd:End();
             CANCEL

    DEFINE TIMER oTmr OF oWnd;
           INTERVAL 1000;
           ACTION LEGGI( oTmr, hSer, oGet )

    ACTIVATE WINDOW oWnd;
             ON INIT oTmr:Activate()

    IF hSer != -1; CLOSEHANDLE( hSer ); ENDIF

    oTmr:End()

    RETURN NIL


#define GENERIC_READ 0x80000000 
#define GENERIC_WRITE 0x40000000
#define OPEN_EXISTING 3 
#define FILE_ATTRIBUTE_NORMAL 0x00000080 


STATIC FUNCTION APRI( cCom )

    LOCAL hSer := CREATEFILE( "COM" + cCom + ":", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL )

    IF hSer != -1
        MSGINFO( "Porta COM" + cCom + " aperta correttamente" )
    ELSE
        MSGINFO( "Impossibile aprire la porta COM" + cCom )
    ENDIF

    RETURN hSer


STATIC FUNCTION LEGGI( oTmr, hSer, oGet )

    LOCAL cTxt := ""

    LOCAL nChr := 0

    IF hSer = NIL .OR. hSer = -1; RETURN NIL; ENDIF

    oTmr:DeActivate()

    WHILE nChr != 0
        nChr = BIN2L( READBYTE( hSer ) )
        cTxt += CHR( nChr )
    ENDDO

    IF !EMPTY( cTxt )
        oGet:VarPut( cTxt )
        oGet:Refresh()
    ENDIF

    oTmr:Activate()

    RETURN NIL
Someone could test it on a real pocket PC?

A question: is it possible (and needed) to set the port parameters (ie. baud rate, stop bits) and how?

EMG
Enrico

Tested on HP IPAQ 6515

I tested port 1 opened OK without any change. No need to set baud, parity...etc
If i set 2 or any other value, it will not open correct.

If you need any further test, let me know

Richard
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Serial port

Post by Enrico Maria Giordano »

Thank you! But I need to know if the characters received from the serial port are displayed on the multiget. Can you test it?

EMG
User avatar
Richard Chidiak
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France
Contact:

Re: Serial port

Post by Richard Chidiak »

EnricoMaria wrote:Thank you! But I need to know if the characters received from the serial port are displayed on the multiget. Can you test it?

EMG
Sorry Enrico, I don't get exactly what you're looking after

Once the port opened correct (nothing is displayed in the multiget)

If you enter data in the multiget the data is kept after opening the com port

Richard
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Serial port

Post by Enrico Maria Giordano »

I need to display in the multiget the data received from a reader device connected to the serial port.

EMG
User avatar
Jon Munro
Posts: 42
Joined: Sun Oct 09, 2005 11:47 am
Location: Brisbane Australia
Contact:

Post by Jon Munro »

Enrico
I've compiled your program and run it with the emulator. It displays characters received OK with a few changes:

Try:

WHILE nChr != 236 .OR. nChr != 10
nChr = ( READBYTE( hSer ) )
cTxt += CHR( nChr )
ENDDO

Previously this was looping so never had a chance to display. Is it possible to transmit a 'prefix' character before your data and a suffix character (eg Chr(10) as above) afterwards so valid data is detected more easily? Notice that 'bin2l' is not required.
I use freeware 'BillSerialMonitor' from www.symcod.com to send test data.
I tried using a port of Telepathy with some success but have now decided to use Antonio's solution as the best (as usual!).

hth
User avatar
Jon Munro
Posts: 42
Joined: Sun Oct 09, 2005 11:47 am
Location: Brisbane Australia
Contact:

Post by Jon Munro »

Enrico
Sorry I meant .AND. !!

WHILE nChr != 236 .AND. nChr != 10
nChr = ( READBYTE( hSer ) )
cTxt += CHR( nChr )
ENDDO

hth
Post Reply