WriteComm() Question

WriteComm() Question

Postby Jeff Barnes » Tue Mar 13, 2007 11:25 pm

Hi Everybody,

I am trying to send a command to a serial device and just want to make sure I am doing this correctly:

From the device manual:
>>Command: “Dn” where n is configuration # in ASCII (1-2,7-8,A-D)<<

I want to send D1 to the serial port so I use:

WriteComm( nComm, "D1" )

where nComm:= OpenComm( "Com5", 1024, 128 )

Is this correct?


Thanks,
Jeff
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Postby Jeff Barnes » Tue Mar 13, 2007 11:28 pm

Sorry, I forgot to mention that the device will send a response and I'd like to know how to read this response.

Again from the device manual:
>>Response *: <ACK>
>>Denied Response *: <NAK> if out of range data
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm() Question

Postby Enrico Maria Giordano » Wed Mar 14, 2007 7:41 am

This is a sample of a simple ASCII terminal:

Code: Select all  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby areang » Wed Mar 14, 2007 1:17 pm

Yes that is work samples, Jeff !

I used it for send char to my mobile phone

Many thank to Enrico again..!


Areang
areang
 
Posts: 128
Joined: Mon Jul 31, 2006 3:23 pm

Postby Jeff Barnes » Wed Mar 14, 2007 3:45 pm

Thanks guys.

I was thinking that my command was not being sent but after a bit more reading on my device I found that I had to send the command within the first 5 seconds of the bluetooth connection.

It looks like it is working now.


Jeff
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 64 guests