Read com port

Read com port

Postby favory » Sat Apr 09, 2011 7: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š
favory
 
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Raed com port

Postby Enrico Maria Giordano » Sat Apr 09, 2011 10:14 am

This is a sample:

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: 8712
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Read com port

Postby favory » Sun Apr 10, 2011 7: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š
favory
 
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Read com port

Postby Enrico Maria Giordano » 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
User avatar
Enrico Maria Giordano
 
Posts: 8712
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Read com port

Postby mtajkov » 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) ]
mtajkov
 
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: Read com port

Postby Enrico Maria Giordano » Fri Apr 15, 2011 10:39 am

This is che function to set the com port:

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


Try to play with the parameters.

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

Re: Read com port

Postby mtajkov » 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) ]
mtajkov
 
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: Read com port

Postby Enrico Maria Giordano » 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
User avatar
Enrico Maria Giordano
 
Posts: 8712
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Read com port

Postby mtajkov » 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) ]
mtajkov
 
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: Read com port

Postby Enrico Maria Giordano » Fri Apr 15, 2011 1: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
User avatar
Enrico Maria Giordano
 
Posts: 8712
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Read com port

Postby favory » Fri Apr 15, 2011 3: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!
favory
 
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Read com port

Postby Enrico Maria Giordano » Fri Apr 15, 2011 5:38 pm

So, try

Code: Select all  Expand view
BUILDCOMMDCB ("COM1: 9600, N, 8.1", @ cDcb)


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

Re: Read com port

Postby favory » Fri Apr 15, 2011 7:19 pm

command
Code: Select all  Expand view
BUILDCOMMDCB( "COM1:9600,N,8,1", @cDcb )
has no effect on the com port! :(


Code: Select all  Expand view
*********************************************

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:
favory
 
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Read com port

Postby Enrico Maria Giordano » Fri Apr 15, 2011 8:05 pm

favory wrote:
Code: Select all  Expand view
if ! SetCommState( cDcb )


Code: Select all  Expand view
if ! SetCommState( nComm, cDcb )


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

Re: Read com port

Postby favory » Fri Apr 15, 2011 8:17 pm

still does not work!
favory
 
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 70 guests