FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Read com port
Posts: 7
Joined: Thu Sep 17, 2009 08:10 AM
Read com port
Posted: Sat Apr 09, 2011 07:28 AM

I'm trying to read a data from the COM port:

include "FiveWin.ch"

function Comm()

local oDlg, nComm := InitComm()

DEFINE DIALOG oDlg TITLE "Testing Comm functions"

*** timet ***
* DEFINE TIMER oTmr INTERVAL 100 ACTION BytesAtPort( nComm, nStatus ) OF oWnd
* ACTIVATE TIMER oTmr


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

ACTIVATE DIALOG oDlg ;
ON INIT EnableCommNotification( nComm, oDlg:hWnd, 20, 20 )

CloseComm( nComm )

return nil


function InitCOMM()

local cDcb, nError, nBytes
local nComm := OpenComm( "COM1", 1024, 128 )
local cF := 'commTest.log'

FErase( cF )

if nComm=1
LogFile( cF, { 'Otvoren COM 1 port ' + '9600' } )
endif

if ! BuildCommDcb( "COM1:9600,n,8,1", @cDcb )
MsgStop( "Error BUILD!" )
return .f.
endif

if ! SetCommState( cDcb )
MsgStop( "Error SETCOMM!" )
return .f.
endif

return nComm


function BytesAtPort( nComm, nStatus )

local cBuffer := Replicate(Chr(255),1024)
local cF := 'commTest.log'

  ReadComm( nComm, @cBuffer )  // <<<<---- program will lock here

     cBuffer:=StrTran(cBuffer,Chr(255),'')

        LogFile( cF, {  cBuffer  } )

return nil

everything is fine but the result is:

09.04.2011 08:01:52: Otvoren COM 1 port 9600
09.04.2011 08:01:52:
09.04.2011 08:01:52:
09.04.2011 08:01:52:
09.04.2011 08:01:52:
09.04.2011 08:01:52:
09.04.2011 08:01:52:
09.04.2011 08:01:52:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:53:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:54:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:55:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:56:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:57:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:58:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:01:59:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:00:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:01:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:02:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:03:
09.04.2011 08:02:04:
09.04.2011 08:02:04:
09.04.2011 08:02:04:
09.04.2011 08:02:04:
09.04.2011 08:02:04:
09.04.2011 08:02:04:
09.04.2011 08:02:04:

the com port is currently connected mouse

I have no experience with this. I need any help!

best regards
Miloš

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Raed com port
Posted: Sat Apr 09, 2011 10:14 AM
This is a sample:

Code (fw): Select all Collapse
#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: 7
Joined: Thu Sep 17, 2009 08:10 AM
Re: Read com port
Posted: Sun Apr 10, 2011 07:04 AM

I'm not sure what should be the result. How can conclude that the port is opened, read and write is OK but with no results. These are the values ​​of variables:

04/10/2011 08:46:59: NCOM = 1
04/10/2011 08:46:59: CSTR =
04/10/2011 08:46:59: Nbytes = 0
04/10/2011 08:46:59: nkey = E
04/10/2011 08:46:59: LEFT (cBuf, READCOMM (NCOM, cBuf @)) =

In what could be the problem?

best regards
Miloš

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Read com port
Posted: Sun Apr 10, 2011 10:27 AM

You can test my sample using a modem or just look at the code and follow it as a guideline.

EMG

Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: Read com port
Posted: Fri Apr 15, 2011 10:26 AM

before the start of your examples status of the com port is 1200, none, 7,1 (if in the Windows taskbar set to 9600, none, 8.1). After starting examples port status is not changed. The program does not report any error but does not even make the com port setting. Why? What could be the problem?

Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Read com port
Posted: Fri Apr 15, 2011 10:39 AM
This is che function to set the com port:

Code (fw): Select all Collapse
BUILDCOMMDCB( "COM1:115200,N,8,1", @cDcb )


Try to play with the parameters.

EMG
Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: Read com port
Posted: Fri Apr 15, 2011 11:00 AM

anything that I type does not respond to commands. If you execute the setup hyper terminal and after that run all the example of doing well?

Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Read com port
Posted: Fri Apr 15, 2011 11:04 AM

Sorry, I didn't understand you question. Anyway, you can test my sample using a modem or another PC connected to the serial port.

EMG

Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: Read com port
Posted: Fri Apr 15, 2011 11:40 AM

BUILDCOMMDCB ("COM1: 115200, N, 8.1", @ cDcb) simply does not work! Com port settings are the same as before the execution of commands!

Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Read com port
Posted: Fri Apr 15, 2011 01:43 PM

You can't see the port setting in the control panel. Please use a device connected to the com port (ie. a modem or another PC) to test my sample.

EMG

Posts: 7
Joined: Thu Sep 17, 2009 08:10 AM
Re: Read com port
Posted: Fri Apr 15, 2011 03:02 PM

I am using Datalogic barcode scanner. If the DOS prompt and typing mode COM1: 9600, n, 8.1 everything is working properly, and you and my program. But the initialization with the command BUILDCOMMDCB ("COM1: 115200, N, 8.1", @ cDcb) can not do that. In setting the com port is not changing!

Sorry for my English!

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Read com port
Posted: Fri Apr 15, 2011 05:38 PM
So, try

Code (fw): Select all Collapse
BUILDCOMMDCB ("COM1: 9600, N, 8.1", @ cDcb)


EMG
Posts: 7
Joined: Thu Sep 17, 2009 08:10 AM
Re: Read com port
Posted: Fri Apr 15, 2011 07:19 PM
command
Code (fw): Select all Collapse
BUILDCOMMDCB( "COM1:9600,N,8,1", @cDcb )
has no effect on the com port! :-)


Code (fw): Select all Collapse
*********************************************

function InitCOMM()

   local cDcb, nError, nBytes
   local nComm := OpenComm( "COM1", 1024 , 128)


   if ! BuildCommDcb( "COM1:9600,N,8,1", @cDcb )
      MsgStop( "Error BUILD!" )
      return .f.
   endif


   if ! SetCommState( cDcb )
      MsgStop( "Error SETCOMM!" )
      return .f.
   endif


return nComm


I do not see any error ..? :-)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Read com port
Posted: Fri Apr 15, 2011 08:05 PM
favory wrote:
Code (fw): Select all Collapse
if ! SetCommState( cDcb )


Code (fw): Select all Collapse
if ! SetCommState( nComm, cDcb )


EMG
Posts: 7
Joined: Thu Sep 17, 2009 08:10 AM
Re: Read com port
Posted: Fri Apr 15, 2011 08:17 PM

still does not work!