WriteComm()

WriteComm()

Postby Jeff Barnes » Thu Feb 20, 2014 9: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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby James Bott » 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=6&t=20082&p=106019&hilit=writecomm#p106019

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: WriteComm()

Postby Jeff Barnes » Fri Feb 21, 2014 1:36 am

Hi James,

Are you referring to:

Code: Select all  Expand view

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: Select all  Expand view

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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby Enrico Maria Giordano » Fri Feb 21, 2014 9:38 am

Jeff,

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: WriteComm()

Postby Jeff Barnes » Fri Feb 21, 2014 1: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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby Enrico Maria Giordano » Fri Feb 21, 2014 1:39 pm

Jeff,

Here it is:

Code: Select all  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: WriteComm()

Postby Jeff Barnes » Fri Feb 21, 2014 7:56 pm

Unfortunately that didn't solve the problem :(
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby Jeff Barnes » Fri Feb 21, 2014 8:08 pm

Here is a cut down version of my code.
I have tested this code and it still locks up the program.

Code: Select all  Expand view

#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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby Enrico Maria Giordano » Fri Feb 21, 2014 9:58 pm

Jeff,

This works:

Code: Select all  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8315
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: WriteComm()

Postby Jeff Barnes » 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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby James Bott » 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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: WriteComm()

Postby James Bott » 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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: WriteComm()

Postby Jeff Barnes » 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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby Jeff Barnes » Sat Feb 22, 2014 1: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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: WriteComm()

Postby James Bott » Sat Feb 22, 2014 2: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 98 guests