Page 1 of 1

problem with communication (RS232)

PostPosted: Sat Jul 28, 2012 8:43 am
by mtajkov
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: Select all  Expand view
#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?

Re: problem with communication (RS232)

PostPosted: Sat Jul 28, 2012 11:40 am
by Antonio Linares
Try to call SysRefresh() between these two calls:

FlushComm(nComRam,1)

SysRefresh()

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

Re: problem with communication (RS232)

PostPosted: Sat Jul 28, 2012 12:55 pm
by mtajkov
Thanks, but does not work ..

Re: problem with communication (RS232)

PostPosted: Sat Jul 28, 2012 1:50 pm
by Antonio Linares
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() ?

Re: problem with communication (RS232)

PostPosted: Sat Jul 28, 2012 2:03 pm
by mtajkov
Code: Select all  Expand view
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


 

Re: problem with communication (RS232)

PostPosted: Sat Jul 28, 2012 2:51 pm
by Antonio Linares
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

Re: problem with communication (RS232)

PostPosted: Sun Jul 29, 2012 11:41 am
by mtajkov
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!