Page 1 of 2

Read com port

Posted: Sat Apr 09, 2011 7:28 am
by favory
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š

Re: Raed com port

Posted: Sat Apr 09, 2011 10:14 am
by Enrico Maria Giordano
This is a sample:

Code: Select all

#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

Re: Read com port

Posted: Sun Apr 10, 2011 7:04 am
by favory
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š

Re: Read com port

Posted: Sun Apr 10, 2011 10:27 am
by Enrico Maria Giordano
You can test my sample using a modem or just look at the code and follow it as a guideline.

EMG

Re: Read com port

Posted: Fri Apr 15, 2011 10:26 am
by mtajkov
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?

Re: Read com port

Posted: Fri Apr 15, 2011 10:39 am
by Enrico Maria Giordano
This is che function to set the com port:

Code: Select all

BUILDCOMMDCB( "COM1:115200,N,8,1", @cDcb )
Try to play with the parameters.

EMG

Re: Read com port

Posted: Fri Apr 15, 2011 11:00 am
by mtajkov
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?

Re: Read com port

Posted: Fri Apr 15, 2011 11:04 am
by Enrico Maria Giordano
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

Re: Read com port

Posted: Fri Apr 15, 2011 11:40 am
by mtajkov
BUILDCOMMDCB ("COM1: 115200, N, 8.1", @ cDcb) simply does not work! Com port settings are the same as before the execution of commands!

Re: Read com port

Posted: Fri Apr 15, 2011 1:43 pm
by Enrico Maria Giordano
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

Re: Read com port

Posted: Fri Apr 15, 2011 3:02 pm
by favory
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!

Re: Read com port

Posted: Fri Apr 15, 2011 5:38 pm
by Enrico Maria Giordano
So, try

Code: Select all

BUILDCOMMDCB ("COM1: 9600, N, 8.1", @ cDcb)
EMG

Re: Read com port

Posted: Fri Apr 15, 2011 7:19 pm
by favory
command

Code: Select all

BUILDCOMMDCB( "COM1:9600,N,8,1", @cDcb )
has no effect on the com port! :(

Code: Select all

*********************************************

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 ..? :cry:

Re: Read com port

Posted: Fri Apr 15, 2011 8:05 pm
by Enrico Maria Giordano
favory wrote:

Code: Select all

if ! SetCommState( cDcb )

Code: Select all

if ! SetCommState( nComm, cDcb )
EMG

Re: Read com port

Posted: Fri Apr 15, 2011 8:17 pm
by favory
still does not work!