problem with communication (RS232)

problem with communication (RS232)

Postby mtajkov » Sat Jul 28, 2012 8: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: 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?
Best regards
Milos

[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
mtajkov
 
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: problem with communication (RS232)

Postby Antonio Linares » 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
User avatar
Antonio Linares
Site Admin
 
Posts: 42089
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: problem with communication (RS232)

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

Re: problem with communication (RS232)

Postby Antonio Linares » Sat Jul 28, 2012 1: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42089
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: problem with communication (RS232)

Postby mtajkov » Sat Jul 28, 2012 2:03 pm

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


 
Best regards
Milos

[ FWH 21.11 ] [ xHarbour 1.2.3 Intl. (SimpLex) (Build 20150603) ]
mtajkov
 
Posts: 130
Joined: Sun Mar 08, 2009 4:33 pm

Re: problem with communication (RS232)

Postby Antonio Linares » Sat Jul 28, 2012 2: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
User avatar
Antonio Linares
Site Admin
 
Posts: 42089
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: problem with communication (RS232)

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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 83 guests