USB to Serial Problem

For issues with communication ports, protocols, etc.
Post Reply
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

USB to Serial Problem

Post by Dinosaur »

Hi All

I am trying to connect to an ADAM-4016 from my laptop, before I install it into an Industrial machine.
Using Win7 and a Prolific USB to serial adapter.

When I connect with the Utility provided by the supplier, it all works.
When I use TeraTerm terminal Emulator, it replies with data.

However from within my small test code in FB, I cannot get a response.

Code: Select all

Sub Adam4016
    With AdamA2D
        Open "COM10:9600,n,8,1,cs0,ds0,cd0,rs" For Random As #1
        TimeShow
        .StartTime = Times.mSec
        .RxStr = ""
        Print #1,"#01" + Chr(&H0D);
        Do
    	    TimeShow
            If Times.mSec - .StartTime > 50 Then Exit do
        Loop
        Print "Out of Delay":LOC(1)
        .RxStr = Input(LOC(1),#1)                  'put them in string
        Print .RxStr
    End With
End Sub
I have extended the delay to 2sec but no data.
Also Loc(1) prints blank, so no char in the buffer.

Is there a driver issue, that is not being loaded by FB ?
In device manager, the port shows itself as Com10:
OR, is there something obvious that I am doing wrong ?

Regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: USB to Serial Problem

Post by TJF »

OPEN "..." FOR RANDOM on a COM port? I never used this. I'd go with FOR BINARY and output the return values of the port operations:

Code: Select all

OPEN "COM10:9600,n,8,1,cs0,ds0,cd0,rs" FOR BINARY AS #1
IF ERR > 0 THEN
  ?"Couldn't open COM 10"
ELSE
  ...
  IF PUT (#1, , "#01" & CHR(&H0D)) THEN ?"Couldn't write"
  ...
  CLOSE #1
END IF
The return values may point you to the problem.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Re: USB to Serial Problem

Post by phishguy »

Code: Select all

Open "COM10:9600,n,8,1,cs0,ds0,cd0,rs" For Random As #1
In windows should be:

Code: Select all

Open Com "COM10:9600,n,8,1,cs0,ds0,cd0,rs" For Random As #1
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: USB to Serial Problem

Post by MichaelW »

This is not complete, but hopefully it will provide an idea of what Windows “sees”.

Code: Select all

#include "windows.bi"
#include "LastErrorStr.bas"

dim as HANDLE hCom
dim as COMMPROP cp

dim as zstring*6 comPort

for n as integer = 1 to 10

    comPort = "COM" & str(n)

    hCom = CreateFile( comPort, _
                       GENERIC_READ or GENERIC_WRITE, _
                       0, _
                       NULL, _
                       OPEN_EXISTING, _
                       0, _
                       NULL )

    if hCom = INVALID_HANDLE_VALUE then
        print comPort, LastErrorStr()
    else
        if GetCommProperties( hCom, @cp ) = 0 then
            print comPort, LastErrorStr()

        else
            print
            print comPort
            with cp
                print "wPacketLength        ";.wPacketLength
                print "wPacketVersion       ";.wPacketVersion
                print "dwServiceMask        ";.dwServiceMask
                print "dwReserved1          ";.dwReserved1
                print "dwMaxTxQueue         ";.dwMaxTxQueue
                print "dwMaxRxQueue         ";.dwMaxRxQueue
                print "dwMaxBaud            ";
                select case .dwMaxBaud
                    case BAUD_075
                        print "75 bps"
                    case BAUD_110
                        print "110 bps"
                    case BAUD_134_5
                        print "134.5 bps"
                    case BAUD_150
                        print "150 bps"
                    case BAUD_300
                        print "300 bps"
                    case BAUD_600
                        print "600 bps"
                    case BAUD_1200
                        print "1200 bps"
                    case BAUD_1800
                        print "1800 bps"
                    case BAUD_2400
                        print "2400 bps"
                    case BAUD_4800
                        print "4800 bps"
                    case BAUD_7200
                        print "7200 bps"
                    case BAUD_9600
                        print "9600 bps"
                    case BAUD_14400
                        print "14400 bps"
                    case BAUD_19200
                        print "19200 bps"
                    case BAUD_38400
                        print "38400 bps"
                    case BAUD_56K
                        print "56K bps"
                    case BAUD_57600
                        print "57600 bps"
                    case BAUD_115200
                        print "115200 bps"
                    case BAUD_128K
                        print "128K bps"
                    case BAUD_USER
                        print "Programmable"
                    case else
                        print "?"
                end select
                print "dwProvSubType        ";
                select case .dwProvSubType
                    case PST_FAX
                        print "PST_FAX"
                    case PST_LAT
                        print "PST_LAT"
                    case PST_MODEM
                        print "PST_MODEM"
                    case PST_NETWORK_BRIDGE
                        print "PST_NETWORK_BRIDGE"
                    case PST_PARALLELPORT
                        print "PST_PARALLELPORT"
                    case PST_RS232
                        print "PST_RS232"
                    case PST_RS422
                        print "PST_RS422"
                    case PST_RS423
                        print "PST_RS423"
                    case PST_RS449
                        print "PST_RS449"
                    case PST_SCANNER
                        print "PST_SCANNER"
                    case PST_TCPIP_TELNET
                        print "PST_TCPIP_TELNET"
                    case PST_UNSPECIFIED
                        print "PST_UNSPECIFIED"
                    case PST_X25
                        print "PST_X25"
                    case else
                        print "?"
                end select
                print "dwProvCapabilities   ";
                select case .dwProvCapabilities
                    case PCF_16BITMODE
                        print "PCF_16BITMODE"
                    case PCF_DTRDSR
                        print "PCF_DTRDSR"
                    case PCF_INTTIMEOUTS
                        print "PCF_INTTIMEOUTS"
                    case PCF_PARITY_CHECK
                        print "PCF_PARITY_CHECK"
                    case PCF_RLSD
                        print "PCF_RLSD"
                    case PCF_RTSCTS
                        print "PCF_RTSCTS"
                    case PCF_SETXCHAR
                        print "PCF_SETXCHAR"
                    case PCF_SPECIALCHARS
                        print "PCF_SPECIALCHARS"
                    case PCF_TOTALTIMEOUTS
                        print "PCF_TOTALTIMEOUTS"
                    case PCF_XONXOFF
                        print "PCF_XONXOFF"
                    case else
                        print "?"
                end select
                print "dwSettableParams     ";
                select case .dwSettableParams
                    case SP_BAUD
                        print "SP_BAUD"
                    case SP_DATABITS
                        print "SP_DATABITS"
                    case SP_HANDSHAKING
                        print "SP_HANDSHAKING"
                    case SP_PARITY
                        print "SP_PARITY"
                    case SP_PARITY_CHECK
                        print "SP_PARITY_CHECK"
                    case SP_RLSD
                        print "SP_RLSD"
                    case SP_STOPBITS
                        print "SP_STOPBITS"
                    case else
                        print "?"
                end select
                print "dwSettableBaud       ";
                select case .dwSettableBaud
                    case BAUD_075
                        print "75 bps"
                    case BAUD_110
                        print "110 bps"
                    case BAUD_134_5
                        print "134.5 bps"
                    case BAUD_150
                        print "150 bps"
                    case BAUD_300
                        print "300 bps"
                    case BAUD_600
                        print "600 bps"
                    case BAUD_1200
                        print "1200 bps"
                    case BAUD_1800
                        print "1800 bps"
                    case BAUD_2400
                        print "2400 bps"
                    case BAUD_4800
                        print "4800 bps"
                    case BAUD_7200
                        print "7200 bps"
                    case BAUD_9600
                        print "9600 bps"
                    case BAUD_14400
                        print "14400 bps"
                    case BAUD_19200
                        print "19200 bps"
                    case BAUD_38400
                        print "38400 bps"
                    case BAUD_56K
                        print "56K bps"
                    case BAUD_57600
                        print "57600 bps"
                    case BAUD_115200
                        print "115200 bps"
                    case BAUD_128K
                        print "128K bps"
                    case BAUD_USER
                        print "Programmable"
                    case else
                        print "?"
                end select
                print "wSettableData        ";
                select case .wSettableData
                    case DATABITS_5
                        print "DATABITS_5"
                    case DATABITS_6
                        print "DATABITS_6"
                    case DATABITS_7
                        print "DATABITS_7"
                    case DATABITS_8
                        print "DATABITS_8"
                    case DATABITS_16
                        print "DATABITS_16"
                    case DATABITS_16X
                        print "DATABITS_16X"
                    case else
                        print "?"
                end select
                print "wSettableStopParity  ";
                select case .wSettableStopParity
                    case STOPBITS_10
                        print "STOPBITS_10"
                    case STOPBITS_15
                        print "STOPBITS_15"
                    case STOPBITS_20
                        print "STOPBITS_20"
                    case PARITY_NONE
                        print "PARITY_NONE"
                    case PARITY_ODD
                        print "PARITY_ODD"
                    case PARITY_EVEN
                        print "PARITY_EVEN"
                    case PARITY_MARK
                        print "PARITY_MARK"
                    case PARITY_SPACE
                        print "PARITY_SPACE"
                    case else
                        print "?"
                end select
                print "dwCurrentTxQueue     ";.dwCurrentTxQueue
                print "dwCurrentRxQueue     ";.dwCurrentRxQueue
                print "dwProvSpec1          ";.dwProvSpec1
                print "dwProvSpec2          ";.dwProvSpec2
            end with
            print
            sleep
        end if

    end if

    CloseHandle( hCom )

next

sleep

Code: Select all

''=============================================================================
#include "windows.bi"
''=============================================================================

''--------------------------------------------------------------------------
'' This function returns the system-defined error string for the last-error
'' code value.
''
'' Note that the last-error code value is meaningful only immediately after
'' a function that sets the last-error code value returns an error. See the
'' function documentation to determine if the function does so.
''
'' Note also that you cannot depend on subsequent successful function calls
'' to clear the last-error code value. To reliably clear the value call the
'' SetLastError function with the dwErrCode parameter set to ERROR_SUCCESS.
''--------------------------------------------------------------------------

function LastErrorStr() as string

    dim as string * 1024 buffer
    dim as integer p
    FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,_
                   0,_
                   GetLastError(),_
                   0,_
                   strptr( buffer ),_
                   1024,_
                   0 )
    buffer = rtrim( buffer )
    p = instr( buffer, chr(13) )
    if p then buffer = left( buffer, p - 1 )

    return buffer

end function
GetCommProperties
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: USB to Serial Problem

Post by Dinosaur »

Hi All

Thanks guys for the suggestions.
Not allowed out to play today to try, but will do on Monday. (even though it is Australia Day holiday)

My fear was that I would have to solder all the handshaking links on a DB9 plug , and I don't have
the bits to do that with me.

Will let you know what solved the problem.

Regards
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: USB to Serial Problem

Post by Dinosaur »

Hi All

Open Com "COM10:9600,......." is what solved the problem.
Can honestly say that I have only ever used the com port with FBDos, so that was new to me.

Michael , the sample program kept throwing a "Bad parameter" error on Com 10 when I run the routine.
Yet the port works with the above statement.

Regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: USB to Serial Problem

Post by TJF »

Thanks for feedback!

It confirms, checking return values from I/O functions is always a good habit.
Post Reply