Author Topic: need help with OPEN COM  (Read 406 times)

need help with OPEN COM
« on: February 11, 2019, 12:58:35 PM »
Hi. Am new to the forum. I've been a QB 4.5 user for many years, using it to control electronic lab instruments via RS-232 to make voltage measurements and log test data. I was very pleased to find QB64 as a modern alternative that can run in Windows 10. I wish to ask for the forum's help to get my test system up and running again.

My original QB4.5 program used OPEN FOR RANDOM for communicating with a digital multimeter. It sends a command to the meter, reads and discards the echo that the meter sends back, then reads the prompt that the meter sends back. See following snippet of the original QB 4.5 program:

MeterSetup:
OPEN "COM2:1200,N,8,,CS,DS,CD" FOR RANDOM AS #11 'open path to Fluke 45
Cmd$ = "REMS;VDC;RANGE 3;RATE M;FORMAT 1"
GOSUB ProgramFluke

ProgramFluke:
PRINT #11, Cmd$
LINE INPUT #11, Result$    'clears the echo response from meter
Prompt$ = INPUT$(5, #11)   'reads the prompt returned by the meter
RETURN


Running this same program in QB64 causes error "Bad file mode" at PRINT #11, Cmd$ I figured this is because of the description of OPEN COM from the QB64 Wiki: "Currently, QB64 only supports OPEN FOR RANDOM access using the GET/PUT commands in BIN mode." I don't know what BIN mode is, so I tried changing the mode from RANDOM to OUTPUT. However, I realize that it won't be able to read anything from the meter in OUTPUT mode, so I CLOSE the path after sending Cmd$ and try to re-open it in INPUT mode as follows:

MeterSetup:
OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR OUTPUT AS #11 'open path to Fluke 45
Cmd$ = "REMS;VDC;RANGE 3;RATE M;FORMAT 1"
GOSUB ProgramFluke
CLOSE #11
OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
LINE INPUT #11, Result$    'clears the echo response from meter
Prompt$ = INPUT$(5, #11)   'reads the prompt returned by the meter

ProgramFluke:
PRINT #11, Cmd$
RETURN

This succeeds in sending the Cmd$ to the meter which gets set to the desired settings, but I get error message "Bad file name" for the OPEN FOR INPUT statement. Why? The COM port had worked fine as OUTPUT just moments earlier. How can I now read what the meter is sending back?

I'd appreciate any help you can offer. Thanks!


Offline Cobalt

  • At 60 I become highly radioactive!
Re: need help with OPEN COM
« Reply #1 on: February 11, 2019, 02:30:08 PM »
you need to use GET #, and PUT # when communing with the COM ports with QB64

little example from my robotics control, controlling a remote fired static mounted firearm.
in Simulation it, sends fire command PUT #1, and checks weapon status with GET #1 over and over checking for weapon jam, ammo depleted and weapon mounting secured.

Code: [Select]
OPEN "COM1:9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
B1 = 197 'Command byte
B2 = 21 'Motor control byte
B12$ = CHR$(B1) + CHR$(B2)
PUT #1, , B12$
DO
 PUT #1, , B12$
 _DELAY .03
 GET #1, , B3~%%
 GET #1, , B4~%%
 GET #1, , B5~%%
 LOCATE 1, 1: PRINT B3~%%
 PRINT B4~%%; B5~%%
LOOP UNTIL INKEY$ = CHR$(27)
CLOSE

On a side note, I would move away from using GOSUB and RETURN
« Last Edit: February 11, 2019, 02:32:13 PM by Cobalt »
Granted after becoming radioactive I only have a half-life!

Offline Pete

  • Cuz I sez so, varmint!
Re: need help with OPEN COM
« Reply #2 on: February 11, 2019, 02:44:12 PM »
Sorry I can't be of much help with com ports. What I can do is offer up some additional resources on top of what Cobalt posted...

http://www.qb64.net/wiki/index.php?title=Port_Access_Libraries

This is an interesting article from someone who used third-party software to open the port, which then allowed QB64 to communicate back and forth to a device: https://social.microsoft.com/Forums/en-US/ed0e97aa-ea86-49dd-b5b0-01fd41c82cf2/using-com-port-with-qb64

You could also ask Clippy (Ted) at http://petesqbsite.com/ Ted has done a lot of hobby work with com ports with QB4.5 and probably with QB64, too. Tell him you voted for Hillary. He'll not only get your program up and running, he'll also send you a fruit basket. Just don't tell him I sent you, or one of those items of fruit might be a ticking and nobody likes fruit stains, and other stains, on their walls.

Hopefully you can get Cobalt's RANDOM access example working in your program, and won't need the above.

I have to climb back in my clown car now. I'd say it's not going to drive away by itself, but it's a Google Clown car, so...

Pete


 

Re: need help with OPEN COM
« Reply #3 on: February 11, 2019, 04:12:05 PM »
I've been messing around with reading data from a Seaward PAT tester and I found it easier to use GET and PUT rather than line input. One thing I did notice in your code :-

Code: [Select]
OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
LINE INPUT #11, Result$    'clears the echo response from meter
You are opening for input as #12 then trying to read from #11
Oh look - a sig file :-)

Re: need help with OPEN COM
« Reply #4 on: February 11, 2019, 04:43:49 PM »
Bit of a sample from my code below. It reads data from the PAT tester and outputs it to a CSV file. The Seawards file system is a bit weird with no fixed length and random entry lengths and sometimes pauses sending so the code relies on a pause of 8 seconds with no data transfer to decide it's finished.

Code: [Select]
DIM bytestr AS STRING * 1 'one byte transfers

' port$ is set earlier so that I can use various comm ports
' comms can be set to 9600, 19200 or 28800 in my program
' outputs to a CSV file opened as #2

OPEN "COM" + port$ + ":" + comms$ + ",N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1

DO 'main loop

    'receive data in buffer when LOC > 0
    IF LOC(1) THEN
        GET #1, , bytestr

        IF (ASC(bytestr) = 13 AND (style = 1 OR style = 5)) THEN bytestr = ","
        IF (ASC(bytestr) = 10 AND style = 1) THEN bytestr = ""

        IF (bytestr <> "") THEN PRINT #2, bytestr;
        IF (bytestr <> "") THEN ptime = TIMER
        ck$ = ck$ + bytestr

        IF LEN(ck$) > 11 THEN ck$ = RIGHT$(ck$, 11)

    END IF

    ptime2 = TIMER
    IF (ptime2 >= (ptime + 8)) THEN ck$ = "end of data"


LOOP UNTIL LCASE$(ck$) = "end of data"

CLOSE #1
Oh look - a sig file :-)

Offline Pete

  • Cuz I sez so, varmint!
Re: need help with OPEN COM
« Reply #5 on: February 11, 2019, 04:45:55 PM »
Ah, Axeman is on to something there, so try...

Code: [Select]
MeterSetup:
OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
LINE INPUT #12, Result$    'clears the echo response from meter
Prompt$ = INPUT$(5, #12)   'reads the prompt returned by the meter

It would be neat if that is all that is needed to get it working If so, you owe Axeman a non-ticking fruit basket!

Pete

Re: need help with OPEN COM
« Reply #6 on: February 11, 2019, 05:07:12 PM »
Non ticking would be good :-)
Oh look - a sig file :-)

Offline Cobalt

  • At 60 I become highly radioactive!
Re: need help with OPEN COM
« Reply #7 on: February 11, 2019, 06:24:46 PM »
some of it might be dependent on the interface as well. Might vary which way is the best. The interfaces I use are circa 1990.



just pick yourself up one of these babies and you don't have to worry about your ticking fruit baskets anymore,
"Rock-Island-Arsenal-EODH-Explosive-Ordinance-Disposal-oh"
SOLD! 675$

 hey look that ones located in Virginia! Maybe Steve could pick it up for ya? Bidding starts at $5!
Thing is those ticking pineapples are really hard to bite! and don't get me started on the pomegranates!
« Last Edit: February 13, 2019, 03:46:09 PM by Cobalt »
Granted after becoming radioactive I only have a half-life!

Re: need help with OPEN COM
« Reply #8 on: February 12, 2019, 11:41:05 AM »
Thanks for all the input, everyone. It will take a few days before I can get back to work on this matter, as I'm trying to squeeze it in among my other work duties. I'll post the results here.

Axeman - Thanks for pointing out the #12 vs. #11 discrepancy; that was just a typo as I was composing the original post.

Pete & Cobalt - I promise not to lob any ticking pineapples or other tropical fruit on this forum! :-)

-Joe

Re: need help with OPEN COM
« Reply #9 on: February 12, 2019, 01:20:50 PM »
My programs that use com ports open the port in RANDOM access mode use
ordinary PRINT to output (remembering to end with ";" to stop extra crud like
CR & LF), and INPUT$ for input.  Like:

init$ = "ATEO"                                                ' random modem setting
open "com1:9600,n,8,1" for random as #1
print #1,init$;
while not(eof(1))
   response$=reponse$+input$(1,#1)
wend
print response$

It works better if you plug it in.

Re: need help with OPEN COM
« Reply #10 on: February 12, 2019, 01:28:28 PM »
Just been taking another look at your code :-

OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
LINE INPUT #11, Result$    'clears the echo response from meter
Prompt$ = INPUT$(5, #11)   'reads the prompt returned by the meter

I'm confused by line 3. Can't say I've seen input used in that way before but it is a long time since I did much serious basic programing. I assume you are trying to read the output from your Fluke into Prompt$. If that's the case why not use

INPUT #12, prompt$ 'reads the prompt returned by the meter

So the code would become

Code: [Select]
MeterSetup:
OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR OUTPUT AS #11 'open path to Fluke 45
Cmd$ = "REMS;VDC;RANGE 3;RATE M;FORMAT 1"
GOSUB ProgramFluke
CLOSE #11
OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
LINE INPUT #12, Result$    'clears the echo response from meter
INPUT #12, prompt$ 'reads the prompt returned by the meter

' rest of prog goes here to stop ProgramFluke ending with a return without gosub error

ProgramFluke:
PRINT #11, Cmd$
RETURN
Oh look - a sig file :-)

Re: need help with OPEN COM
« Reply #11 on: February 27, 2019, 05:20:15 PM »
Well, I've continued trying but have come to the conclusion that QB64 just doesn't work the way I need it to. Having OPEN COM for RANDOM work only in BIN mode is a hindrance.

Another weird thing: When trying Axeman's suggestion in the post immediately above, I get a "Bad file name" error when it tries to OPEN COM2 for INPUT after having closed it for OUTPUT. Why isn't #12 (or #-whatever) a satisfactory file name? (Actually I use COM4 rather than COM2 but I don't think this should matter.)

Even if I could open COM4 for INPUT as well as OUTPUT, it would become a pain for all the flip-flopping the COM port would have to do when writing to and reading from the Fluke test instrument.

Granted, my lack of skill as a programmer may be at the root of all this. However, while scouring the Web I came across Liberty BASIC and downloaded it. It does support OPEN COM for RANDOM and now I am able to write to and read from the instrument just as I could with QB 4.5 . So I'm finally on the road to accomplishing my task.

Thanks to everyone for all the suggestions.


Re: need help with OPEN COM
« Reply #12 on: April 04, 2019, 12:58:51 PM »
The command PRINT# does not work with COM ports. Instead you must use PUT#.
The variable length strings like cmd$ contains 2 bytes for the lenght of string wich are transmited in put, you must extract in single char, like this:
Code: [Select]
SUB PrintCOM (envio$) STATIC
  DIM car AS STRING * 1
  FOR i = 1 TO LEN(envio$)
    car = MID$(envio$, i, 1)
    PUT #1, , car
  NEXT i
  car = CHR$(13)
  PUT #1, , car
END FUNCTION

Re: need help with OPEN COM
« Reply #13 on: April 07, 2019, 09:27:23 AM »
PRINT # works with COM ports.  I've been doing so for decades.  As long as one remembers to put ; after the string, there's no problem with it.  Heck, sometimes one even wants the CR/LF, like in "talking" to the old General Instrument speech chip.



It works better if you plug it in.

Re: need help with OPEN COM
« Reply #14 on: April 08, 2019, 02:21:57 AM »
From the wiki, in OPEN COM:
'Currently, QB64 only supports OPEN FOR RANDOM access using the GET/PUT commands in BIN mode'