Read com port

favory
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Read com port

Post 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š
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Raed com port

Post by Enrico Maria Giordano »

This is a sample:

Code: Select all | Expand

#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 NILSTATIC FUNCTION TASTI( nCom, nKey )    SENDSTR( nCom, CHR( nKey ) )    RETURN NILSTATIC 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 nComSTATIC 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 NILSTATIC 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
favory
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Read com port

Post 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š
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Read com port

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

Re: Read com port

Post 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?
Best regards
Milos

[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Read com port

Post by Enrico Maria Giordano »

This is che function to set the com port:

Code: Select all | Expand

BUILDCOMMDCB( "COM1:115200,N,8,1", @cDcb )


Try to play with the parameters.

EMG
mtajkov
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: Read com port

Post 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?
Best regards
Milos

[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Read com port

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

Re: Read com port

Post 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!
Best regards
Milos

[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Read com port

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

Re: Read com port

Post 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!
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Read com port

Post by Enrico Maria Giordano »

So, try

Code: Select all | Expand

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


EMG
favory
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Read com port

Post by favory »

command

Code: Select all | Expand

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


Code: Select all | Expand

*********************************************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.   endifreturn nComm 


I do not see any error ..? :cry:
User avatar
Enrico Maria Giordano
Posts: 8759
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: Read com port

Post by Enrico Maria Giordano »

favory wrote:

Code: Select all | Expand

if ! SetCommState( cDcb )


Code: Select all | Expand

if ! SetCommState( nComm, cDcb )


EMG
favory
Posts: 7
Joined: Thu Sep 17, 2009 8:10 am

Re: Read com port

Post by favory »

still does not work!
Post Reply