Read Com port
Read Com port
Hi,
I have a analytical scale connected to a com port .
I test sample testcom3.prg and it works fine but it return the information in 2 parts, (2 MsgInfo box) .
The total lenght is 18 string .
How to return the total 18 strings from the BytesAtPort function ??
Thanks
#include "FiveWin.ch"
function Main()
local oDlg, nComm := InitComm()
DEFINE DIALOG oDlg TITLE "Testing Comm functions"
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 )
if ! BuildCommDcb( "COM1:9600,n,8,1", @cDcb )
MsgStop( "Error BUILD!" )
return .f.
endif
#ifdef __CLIPPER__
if ! SetCommState( cDcb )
#else
if ! SetCommState( nComm, cDcb )
#endif
MsgStop( "Error SETCOMM!" )
return .f.
endif
return nComm
function BytesAtPort( nComm, nStatus )
local cBuffer := Space(20 )
Msginfo( nSTATUS)
ReadComm( nComm, @cBuffer ) // <<<<---- program will lock here
Msginfo( AllTrim( cBuffer ))
return nil
I have a analytical scale connected to a com port .
I test sample testcom3.prg and it works fine but it return the information in 2 parts, (2 MsgInfo box) .
The total lenght is 18 string .
How to return the total 18 strings from the BytesAtPort function ??
Thanks
#include "FiveWin.ch"
function Main()
local oDlg, nComm := InitComm()
DEFINE DIALOG oDlg TITLE "Testing Comm functions"
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 )
if ! BuildCommDcb( "COM1:9600,n,8,1", @cDcb )
MsgStop( "Error BUILD!" )
return .f.
endif
#ifdef __CLIPPER__
if ! SetCommState( cDcb )
#else
if ! SetCommState( nComm, cDcb )
#endif
MsgStop( "Error SETCOMM!" )
return .f.
endif
return nComm
function BytesAtPort( nComm, nStatus )
local cBuffer := Space(20 )
Msginfo( nSTATUS)
ReadComm( nComm, @cBuffer ) // <<<<---- program will lock here
Msginfo( AllTrim( cBuffer ))
return nil
Re: Read Com port
I've got the same problem.
BytesAtPort() is returning 2 times.
1. 1 byte
2. from 2nd byte to the rest.
For your example,
1. 1 byte
2. 17 bytes.
I'm waiting for reply too.
Thanks in advance,
BytesAtPort() is returning 2 times.
1. 1 byte
2. from 2nd byte to the rest.
For your example,
1. 1 byte
2. 17 bytes.
I'm waiting for reply too.
Thanks in advance,
Regards,
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
Re: Read Com port
Hi,
Could we have some help about this ?
What is the right way to read all the info from a COM port .
Thanks
Could we have some help about this ?
What is the right way to read all the info from a COM port .
Thanks
-
- Posts: 39
- Joined: Fri Aug 22, 2014 6:21 am
Re: Read Com port
In the first post, you say that the example Testcom3 returns two msginfo, and it is just so: first the status, then the text received.
If you don't need the status, delete it.
>> What is the right way to read all the info from a COM port .
It's difficult/simple to answer the question. In fact you read all the info, if on the other side the instrument send you all the info.
If send you a single control character, you need to respond to that control character.
It depends on the interface protocol...
If you don't need the status, delete it.
>> What is the right way to read all the info from a COM port .
It's difficult/simple to answer the question. In fact you read all the info, if on the other side the instrument send you all the info.
If send you a single control character, you need to respond to that control character.
It depends on the interface protocol...
Re: Read Com port
Hi,
I have delete the MsgInfo for the status .
I have only this :
ReadComm( nComm, @cBuffer ) // <<<<---- program will lock here
Msginfo( AllTrim( cBuffer ))
But it gives me 2 times the MsgInfo , first with a part of the info and the second with the rest .
What can i do to read all in 1 step and then add it to a dbf file .
Thanks
I have delete the MsgInfo for the status .
I have only this :
ReadComm( nComm, @cBuffer ) // <<<<---- program will lock here
Msginfo( AllTrim( cBuffer ))
But it gives me 2 times the MsgInfo , first with a part of the info and the second with the rest .
What can i do to read all in 1 step and then add it to a dbf file .
Thanks
-
- Posts: 39
- Joined: Fri Aug 22, 2014 6:21 am
Re: Read Com port
>>ReadComm( nComm, @cBuffer ) // <<<<---- program will lock here
>>Msginfo( AllTrim( cBuffer ))
Excuse me, but I do not understand how from these lines, if you run only once, can result two msginfo...
In any way, what is the total string?
>>Msginfo( AllTrim( cBuffer ))
Excuse me, but I do not understand how from these lines, if you run only once, can result two msginfo...
In any way, what is the total string?
Re: Read Com port
I suppose there is a buffer but ???
Any help ?
Thanks
Any help ?
Thanks
-
- Posts: 39
- Joined: Fri Aug 22, 2014 6:21 am
Re: Read Com port
Hi Jack,
I don't use the library you're using, so let's go for trial...
The Readcomm function expects Cbuffer as:
A string variable supplied by reference where ReadComm() will place the read bytes.
The length of the initial supplied value specifies the wished number of bytes to be read.You may use, i.e., Space( ... ) to initialize this buffer.
So, set the buffer exactly to 18 bytes, otherwise the function waits for two more characters.
Also, delete bCommNotify and EnableCommNotify, and enter a button to run BytesAtPort
Test and good luck.
I don't use the library you're using, so let's go for trial...
The Readcomm function expects Cbuffer as:
A string variable supplied by reference where ReadComm() will place the read bytes.
The length of the initial supplied value specifies the wished number of bytes to be read.You may use, i.e., Space( ... ) to initialize this buffer.
So, set the buffer exactly to 18 bytes, otherwise the function waits for two more characters.
Also, delete bCommNotify and EnableCommNotify, and enter a button to run BytesAtPort
Test and good luck.
Re: Read Com port
Hi,
It works with the last suggestion ==> delete : bCommNotify and EnableCommNotify
Thanks for this feed back.
It works with the last suggestion ==> delete : bCommNotify and EnableCommNotify
Thanks for this feed back.
Re: Read Com port
Hi Jack,
How do you get automatic readcom() function to replace oDlg:bCommNotify ?
How do you get automatic readcom() function to replace oDlg:bCommNotify ?
Jack wrote:Hi,
It works with the last suggestion ==> delete : bCommNotify and EnableCommNotify
Thanks for this feed back.
Regards,
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
Re: Read Com port
Dutch,
I use a timer .
DEFINE TIMER oTmr INTERVAL 6000 ACTION (BytesAtPort( nComm),oLbx:Refresh()) OF oWnd
ACTIVATE TIMER oTmr
function BytesAtPort( nComm, nStatus )
local cBuffer:=space(18)
ReadComm( nComm, @cBuffer )
select RAWPOIDS
append blank
replace data with cBuffer
return nil
I use a timer .
DEFINE TIMER oTmr INTERVAL 6000 ACTION (BytesAtPort( nComm),oLbx:Refresh()) OF oWnd
ACTIVATE TIMER oTmr
function BytesAtPort( nComm, nStatus )
local cBuffer:=space(18)
ReadComm( nComm, @cBuffer )
select RAWPOIDS
append blank
replace data with cBuffer
return nil
Re: Read Com port
Dear Jack,
Thanks, but it doesn't fix all case. If there is 2nd process coming during 1st process that doesn't finish.
Thanks, but it doesn't fix all case. If there is 2nd process coming during 1st process that doesn't finish.
Jack wrote:Dutch,
I use a timer .
DEFINE TIMER oTmr INTERVAL 6000 ACTION (BytesAtPort( nComm),oLbx:Refresh()) OF oWnd
ACTIVATE TIMER oTmr
function BytesAtPort( nComm, nStatus )
local cBuffer:=space(18)
ReadComm( nComm, @cBuffer )
select RAWPOIDS
append blank
replace data with cBuffer
return nil
Regards,
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Read Com port
This is a working sample of a simple terminal:
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
Dear EMG,
I will test, thanks a lot.
I will test, thanks a lot.
Enrico Maria Giordano wrote:This is a working sample of a simple terminal:
EMGCode: 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 ) )
Regards,
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)
Dutch
FWH 19.01 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio
FWPPC 10.02 / Harbour for PPC (FTDN)
ADS V.9 / MySql / MariaDB
R&R 12 Infinity / Crystal Report XI R2
(Thailand)