Reading data from COM ports

Windows specific questions.
Post Reply
Iunin
Posts: 2
Joined: Jul 04, 2006 10:45
Location: Chernogolovka, Russia
Contact:

Reading data from COM ports

Post by Iunin »

Hi all!

I used QB to read data from 2 Mitutoyo indicators via serial ports. Recently I have to add one more indicator.

QB does not open COM3, so now I am moving to FreeBasic (under Windows'98). I found a strange thing. The COM1 port is working, while others do not.

Below is a part of code that works well with COM1, but does not if I change COM1 say to COM2 and i = 2. No data could be read.

Code: Select all

Option Explicit

DIM SHARED adro(1 TO 3) AS SHORT => {&H3F8, &H2F8, &H3E8}                   


DIM AS SHORT xc, m, i, ik, aa, a, sl 
DIM as string f, Spec, yyyy, dddd, k, ast, ain, ss, s 
DIM AS SINGLE T, T0, T1, r, tt, delay, timr

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
CLOSE 
i = 1
OPEN COM "COM1:9600,n,8,1,ds" FOR BINARY AS #1
    OUT(adro(i), 3)   'Reset the interface
    SLEEP 10
    OUT(adro(i), 13)
    SLEEP 10
    PRINT "   RS232 opened "

    OUT(adro(i), ASC("1")) 'Sending data request
    OUT(adro(i), 13)

'    Now waiting data ...
    timr = TIMER: ain = ""
    DO
        IF LOC(1) >= 1 THEN
		  ' LOC(...) gives the number of characters waiting:
		  ast = INPUT$(1, #1)
                  aa = ASC(ast)
		  ain = ain + ast
        END IF        
    LOOP WHILE aa <> 13 AND TIMER - timr < 3
    PRINT "ain", ain 
    a = INSTR(ain, "A")
CLOSE
SLEEP
END
The only difference in hardware I could find: COM1 uses IRQ3 while COM2 and COM3 share IRQ4.

Any suggestions?
coderJeff
Site Admin
Posts: 4351
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

I was able to reproduce a similiar problem using a real serial port and an old external modem.

I found that DTR was not being set to high on OPEN COM. (what QB did by default). The IRQ sharing and COM #'s shouldn't matter.

Thanks for the bug report, Fixed in CVS.

You should be able to work around it for now with this:

Code: Select all

''Set DTR
out( adro(i) + 4, inp( adro(i) + 4 ) or 1 )
Iunin
Posts: 2
Joined: Jul 04, 2006 10:45
Location: Chernogolovka, Russia
Contact:

Post by Iunin »

Thanks for respond!

I did not catch on the meaning of:
coderJeff wrote:

Code: Select all

''Set DTR
out( adro(i) + 4, inp( adro(i) + 4 ) or 1 )
But it works! However with COM2 only :-(. COM3 does not respond yet. So I am still at the DOS level...
coderJeff
Site Admin
Posts: 4351
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Some data communication equipment are more picky about the RS232 interface. DTR (Data terminal ready) is an output on the computer that tells the modem that it is ready to communicate. Some devices don't care, some devices do.

I'm not sure why it might work on on COM2 but not COM3:

Try compiling your code with '-exx' on the fbc command line and test if the OPEN COM statement actually succeeds. If it does not, you may need other switches on the open statement other than DS to get it to work.

Check the cables, dip-switches, connectors, or whatever else is between your program and the device. Often, very minor setup errors can cause communication to fail. Or try connecting with another terminal program to help determine if fbc is at fault.
Post Reply