FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour problem with communication (RS232)
Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
problem with communication (RS232)
Posted: Sat Jul 28, 2012 08:43 AM
I need to do my permanent communication with the device. If in a query (WriteComm) do not get the response, I have to know. Otherwise, communication stops!
Code (fw): Select all Collapse
#include "FiveWin.ch"

static hBmpKon:= {  "green" , "red" }


function Main()

   DEFINE DIALOG oDlg RESOURCE "Ulaz" 
 
     REDEFINE BTNBMP oBmpRam ID 32 OF oDlg RESOURCE "sRed" ;
                   ACTION (nComRam:=InitRam(), WriteComm(nComRam, "$01"+"6"+chr(13))  ) // Start

      oDlg:bCommNotify = { | nComUlz, nStatus |oCont:SetBmp( hBmpKon[ 1 ] ), BytesAtPort( nComRam, nStatus ),; 
                                      EnableCommNotification( nComRam, oDlg:hWnd, 1, -1) }
   
    REDEFINE BITMAP oCont RESOURCE "Red";
                   ADJUST ID 1022;
                   WINDOW oDlg

    .
    .
    .
    .

   ACTIVATE DIALOG oDlg 


   CloseComm( nComUlz )

return nil

//----------------------------------------------------------------------------//


static function BytesAtPort( nComRam, nStatus )
  
   local cBufRam := Replicate(Chr(255),1024)

    
      ReadComm( nComRam, @cBufRam )  
 
         cBufRam:=StrTran(cBufRam,Chr(255),'')
        
    .
    .
    .
    .


   FlushComm(nComRam,1) 

 WriteComm(nComRam, "$01"+"6"+chr(13)) 

return nil


If for any reason do not get a response to the command WriteComm (nComRam, "$ 01" + "6" + chr (13)) should execute oConto: SetBmp (hBmpKon [0]). How?
Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: problem with communication (RS232)
Posted: Sat Jul 28, 2012 11:40 AM

Try to call SysRefresh() between these two calls:

FlushComm(nComRam,1)

SysRefresh()

WriteComm(nComRam, "$01"+"6"+chr(13))

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: problem with communication (RS232)
Posted: Sat Jul 28, 2012 12:55 PM

Thanks, but does not work ..

Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: problem with communication (RS232)
Posted: Sat Jul 28, 2012 01:50 PM

WriteComm() returns the amount of written bytes, so you could check it:

MsgInfo( WriteComm(nComRam, "$01"+"6"+chr(13)) )

Could you post the code for InitRam() ?

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: problem with communication (RS232)
Posted: Sat Jul 28, 2012 02:03 PM
Code (fw): Select all Collapse
static function InitRam(mPortRam,oBmpRam,oComUlz,nBitUlz)

   local cDcb, nError, nBytes
   local nCommUlz := OpenComm( "COM"+mPortRam, val(mBtUlz), val(mBrUlz))


        if nCommUlz < 0 
            MsgStop("Error Open COM"+mPortRam) 
        oComUlz:enable()
        return .f.
        endif 

   if ! BuildCommDcb( "COM" + mPortRam + ":"+rtrim(nBitUlz)+",N,8,1", @cDcb )
      MsgStop( "Error BUILD!" )
      oComUlz:enable()
      return .f.
   endif


   if ! SetCommState( cDcb  )
      MsgStop( "Error SETCOMM!" )
      oComUlz:enable()
      return .f.
   endif


   if !ENABLECOMMNOTIFICATION( nCommUlz, oDlgRam:hWnd, 1, -1 ) 
      MsgStop("Error Open COM"+mPortRam)
      oComUlz:enable()
      return .f.
   endif 

   if FlushComm( nCommUlz, 1 ) != 0
         MsgInfo("FlushComm Error")
   endif

   if FlushComm( nCommUlz, 0 ) != 0
         MsgInfo("FlushComm Error")
   endif

          sBmpPet=""
          sBmpBar=""

oBmpRam:SetFile(cOsnDir+"\bitmaps\sgreen.bmp")
oBmpRam:Refresh()

         LogFile( cF, { "Ulaz - Otvoren COM"+mPortRam  } )

return nCommUlz
Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: problem with communication (RS232)
Posted: Sat Jul 28, 2012 02:51 PM

Please notice that you have to do in 32 bits:

SetCommState( nComm, cDcb )

instead of:

SetCommState( cDcb )

Replace it in your app and try it again, thanks

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 130
Joined: Sun Mar 08, 2009 04:33 PM
Re: problem with communication (RS232)
Posted: Sun Jul 29, 2012 11:41 AM

Unfortunately I am using 16 bit (Fivewin 1.92) because the 32 bit version can not be simultaneously open 4 ports if it is appropriate. And that is a big problem. Do you know if you have the latest versions to the same limit?

The problem I have now solved by introducing timer that periodically checks nStatus but I'm not happy about it. I'm looking for a better solution!

Sorry for my English!

Best regards

Milos



[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]

Continue the discussion