FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour WriteComm()
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
WriteComm()
Posted: Thu Feb 20, 2014 09:59 PM

Is there any way to "timeout" the WriteComm function.

I want to be able to allow the user the ability to Scan for my device on a com port.
I generate a list of all available com ports in the system, then want to try and find my device by doing a writecomm/readcomm to see which com port the device responds on.

If my device is on the first com port in the list it connects and everything is ok.
If it's not the first device, the WriteComm function just locks the system.

I have verified it's the WriteComm function by placing a MsgInfo() before and after... I never get to see the second MsgInfo().

Any ideas?

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 12:58 AM

Jeff,

Maybe this thread will help. There seems to be a error trap in the code shown in the second message.

viewtopic.php?f=6t=20082p=106019hilit=writecomm#p106019

James

&&&

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 01:36 AM
Hi James,

Are you referring to:

Code (fw): Select all Collapse
if ( nBytes := WriteComm( nComm, "ATZ0" + Chr( 13 ) ) ) < 0
   nError = GetCommError( nComm )
   MsgStop( "Error initializing modem!" )
   return .f.
endif


If so, unfortunately it doesn't make any difference.
If there is no device to write to it simply locks up the program waiting for something to write to :-)

This is what I tried:

Code (fw): Select all Collapse
IF (nBytes := WriteComm( nComm, "D8" ) ) < 0 
    MsgInfo("Nothing to connect to.")
ENDIF


If there is a device, when I send "D8" the device will respond with "06"
Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 09:38 AM

Jeff,

I'm using WriteComm() and it doesn't lock even without a device connected.

EMG

Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 01:02 PM

Hi Enrico,

Can you please send me a code sample showing the complete connection routine you are using?

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 01:39 PM
Jeff,

Here it is:

Code (fw): Select all Collapse
#include "Fivewin.ch"


FUNCTION MAIN()

    LOCAL nCom, cDcb

    nCom = OPENCOMM( "COM1", 16384, 16384 )

    IF nCom < 0
        ? "Communication port open error"
        RETURN NIL
    ENDIF

    BUILDCOMMDCB( "COM1:19200,N,8,1", @cDcb )

    IF !SETCOMMSTATE( nCom, cDcb )
        ? "Communication port set error"
        RETURN NIL
    ENDIF

    IF !SENDSTR( nCom, "AT" + CHR( 13 ) )
        ? "Communication port write error"
        RETURN NIL
    ENDIF

    CLOSECOMM( nCom )

    ? "OK"

    RETURN NIL


STATIC FUNCTION SENDSTR( nCom, cString )

    RETURN WRITECOMM( nCom, cString ) = LEN( cString )


EMG
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 07:56 PM

Unfortunately that didn't solve the problem :(

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 08:08 PM
Here is a cut down version of my code.
I have tested this code and it still locks up the program.

Code (fw): Select all Collapse
#include "Fivewin.ch"

Function FindCom()
   LOCAL cData:="", cDataRead:=Space(8), cDCB, nComm
    
   nComm:= OpenComm("COM4", 16384,16384 )

   IF nComm < 0 
        MsgInfo("Error opening port")
   ENDIF

   BuildCommDcb( "COM4:9600,N,8,1", @cDcb ) 

   IF ! SetCommState(  nComm, cDcb )
      MsgInfo("SetCommState Error")
      CloseComm(nComm)
   ENDIF

   MsgInfo("Just before writing to com port")
   IF ! SendStr( nComm, "D8" )
      MsgInfo("No Connection but passed problem with locking up")     //never gets this far
   ENDIF
   MsgInfo("After writing to com port")
Return Nil

STATIC FUNCTION SendStr( nComm, cString )
RETURN WriteComm( nComm, cString ) = LEN(cString)
Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 09:58 PM
Jeff,

This works:

Code (fw): Select all Collapse
#include "Fivewin.ch"

Function FindCom()
   LOCAL cDCB, nComm
    
   nComm:= OpenComm("COM4", 16384,16384 )

   IF nComm < 0 
        MsgInfo("Error opening port")
        RETURN NIL
   ENDIF

   BuildCommDcb( "COM4:9600,N,8,1", @cDcb ) 

   IF ! SetCommState(  nComm, cDcb )
      MsgInfo("SetCommState Error")
      CloseComm(nComm)
      RETURN NIL
   ENDIF

   MsgInfo("Just before writing to com port")
   IF ! SendStr( nComm, "D8" )
      MsgInfo("No Connection but passed problem with locking up")     //never gets this far
   ENDIF
   MsgInfo("After writing to com port")
Return Nil

STATIC FUNCTION SendStr( nComm, cString )
RETURN WriteComm( nComm, cString ) = LEN(cString)


EMG
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 11:39 PM

Enrico,

Do you get a SetCommState error when you run the test?

If so, that might explain why yours works.
I do not get a SetCommState error.

I took your last test, compiled it and I get the "Just before writing to com port" message then nothing. It just hangs.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: WriteComm()
Posted: Fri Feb 21, 2014 11:59 PM

Jeff,

Enrico's code works for me (XP SP3, FWH 13.04/xHarbour). There is no SetCommState error.

I see you are using an older version of FWH. I am emailing you a copy of my test EXE so you can test it on your hardware.

Do you also have another PC you can test on?

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: WriteComm()
Posted: Sat Feb 22, 2014 12:11 AM

Jeff,

I forgot to mention that I changed the code from COM4 to COM1 since my hardware only has one com port (nothing connected to it).

If you need me to recompile using COM4 let me know.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Sat Feb 22, 2014 12:17 AM

Hi James,

Can you please recompile with COM4.

With your version, I get the SetCommState error since I have no COM1

I have tried my compiled version on other PCs with the same result.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: WriteComm()
Posted: Sat Feb 22, 2014 01:53 AM

Thanks James. File received.
Still the same problem...

I wonder if this could be because the COM ports I am working with are Bluetooth Serial Ports??????

I will have to try this on a pc with a standard com port and see if it makes any difference although that won't solve my problem :(

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: WriteComm()
Posted: Sat Feb 22, 2014 02:29 AM
Jeff,

bluetooth serial ports


Ah, you have been keeping secrets. Are these special serial ports with bluetooth also built in, or are they standard serial ports with bluetooth adtapters plugged into them? I have never worked with them, so I know nothing about them.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10