Read com port
Read com port
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š
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š
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Raed com port
This is a sample:
EMG
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 ) )
Re: Read com port
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š
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š
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read com port
You can test my sample using a modem or just look at the code and follow it as a guideline.
EMG
EMG
Re: Read com port
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?
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read com port
This is che function to set the com port:
Try to play with the parameters.
EMG
Code: Select all
BUILDCOMMDCB( "COM1:115200,N,8,1", @cDcb )
EMG
Re: Read com port
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?
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read com port
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
EMG
Re: Read com port
BUILDCOMMDCB ("COM1: 115200, N, 8.1", @ cDcb) simply does not work! Com port settings are the same as before the execution of commands!
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read com port
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
EMG
Re: Read com port
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!
Sorry for my English!
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read com port
So, try
EMG
Code: Select all
BUILDCOMMDCB ("COM1: 9600, N, 8.1", @ cDcb)
Re: Read com port
command has no effect on the com port!
I do not see any error ..?
Code: Select all
BUILDCOMMDCB( "COM1:9600,N,8,1", @cDcb )
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
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read com port
favory wrote:Code: Select all
if ! SetCommState( cDcb )
Code: Select all
if ! SetCommState( nComm, cDcb )