trying out OPEN COM. some trouble.

General FreeBASIC programming questions.
Dr. Honeydew
Posts: 1
Joined: Apr 25, 2006 18:56

trying out OPEN COM. some trouble.

Postby Dr. Honeydew » Apr 25, 2006 19:01

Hello all,

I am trying out FreeBasic for the first time. I want to use it to open a serial port on my PC, etc., but have been having some problems.
Here is the simple code I tried to test it with:

OPEN COM "COM1:600,N,8,2" AS 2
FOR I=1 TO 10
BEEP
WRITE #2,I,"HELLO",CHR$(13)
NEXT
CLOSE 2

I tried variations on using "#" with the file number, etc., because I was confused by what the convention was.

My PC is connected to a Mac Classic that has a terminal program open, and I was able to communicate directly from PC to Mac and from the Mac to my PC, using a terminal program on my PC.

Not sure what's up. Any input would be much appreciated. Thanks kindly for your time.

Cheers,
Dr.H
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Postby Antoni » Apr 25, 2006 19:31

Dr H.
Some ideas:
The terminal program in your Mac should be set to the same speed, nr of bits, stop bits and parity, and to use no handshake.

With no handshake i get better resulsta adding a dt to the port setup.

Try to create a string with your conter and your message and use PRINT #. I'm not sure if WRITE # is implemented por comms.

I don't know if it's an omission but the Wiki does'nt mention a speed of 600bps.

Two stop bits is a little strange (I think it was used with teletypes when 5 data bits was a standard) perhaps this setting is not well tested. Try a single stop bit.

Perhaps my experiments can help you:

Code: Select all

'minimal terminal program by Antoni. Ported from qb  3/2006
'open com "COM1:9600,N,8,1 ,ds, dt" as #1  works with a loopback plug in the serial port: pin 2  connected to pin 3
'open com "COM2:9600,N,8,1 , dt" as #1    communicates with an USR modem
'---------------------------------------------------------------------------
'NOTES
'  new syntax for open com:
'  eof checking for serial port empty input buffer does not work in fb,
'   changed to loc 
'  cr alone did a crlf in QB, in fb does just a cr, added check at end of line
'  added sleeps to release processor time
'---------------------------------------------------------------------------
color ,0:cls
open com "COM2:9600,N,8,1 ,dt" as #1
print err
color 7
DO
 'get key
 sleep 15
 K$ = INKEY

 'process key pressed
 IF LEN(K$) THEN
    SELECT CASE ASC(K$)
   
    'enter ends message and sends it
    CASE 13:
      IF LEN(TX$) THEN PRINT #1, TX$: TX$ = "" :print chr(10)
   
    'backspace erases rightmost char
    CASE 8
      IF LEN(TX$) THEN
        TX$ = LEFT$(TX$, LEN(TX$) - 1)
        p = POS(0): LOCATE , p - 1: PRINT " "; : LOCATE , p - 1
      END IF
   
    'escape quits
    CASE 27
      EXIT DO
   
    'any other key is added to message
    CASE ELSE
      TX$ = TX$ + K$: PRINT K$;
    END SELECT

 END IF

 'read receive buffer while there are chars
 WHILE loc(1)
    sleep 15
    RX$ = RX$ + INPUT$(LOC(1), 1)
    T! = TIMER + TIMEOUT
 WEND

 'if rx timeout, display received message and clear buffer
 IF TIMER > T! THEN
    IF LEN(RX$) THEN 
       COLOR 15,0: PRINT RX$;
       COLOR 7,0:
       if rx$[len(rx$)-1]=13 then print chr(10);  'qb did'nt require that
       RX$ = "" 
    end if   
    T! = TIMER + TIMEOUT
 END IF
LOOP

CLOSE
END


And unless you were able to communicate with QB previously, be patient and keep trying. Serial port is VERY tricky.
go4it
Posts: 1
Joined: Jul 02, 2006 2:55

Postby go4it » Jul 02, 2006 19:28

Thanks to antoni, I am able to use the serial port.
but I did not wanted to build a whole line before sending.

So I made an example for a modem.

At the beginning the modem is checked by sending "AT"

Code: Select all

---------------------------------------------------------------------------
'NOTES from ANTONI
'  new syntax for open com:
'  eof checking for serial port empty input buffer does not work in fb,
'   changed to loc
'  cr alone did a crlf in QB, in fb does just a cr, added check at end of line
'  added sleeps to release processor time
'-----------------comments addeds-----------------
' Changed for character by character output
' ready to launch AT modem commands.
' like ATDT01234567 + <ENTER-key> dials to number 01234567
' ---------------------------------------------------------------------------
color ,0:cls
open com "COM4:9600,N,8,1 ,dt" as #1
IF err <> 0 THEN print "ERROR = "; err
DIM AT AS STRING
AT="AT"                     

PRINT #1,AT   ' test if modem is ready.
color 5
DO
 'get key
 sleep 15
 K$ = INKEY
 
 'process key pressed
IF LEN(K$) THEN PUT #1,,K$
K$=""
   


 'read receive buffer while there are chars
 WHILE loc(1)
    sleep 15
    RX$ = RX$ + INPUT$(LOC(1), 1)
    T! = TIMER + TIMEOUT
 WEND

 'if rx timeout, display received message and clear buffer
 IF TIMER > T! THEN
    IF LEN(RX$) THEN
       COLOR 15,0: PRINT RX$;
       COLOR 7,0:
       
       RX$ = ""
    end if   
    T! = TIMER + TIMEOUT
 END IF
LOOP

CLOSE
END
[/code]

Return to “General”

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests