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

0 Members and 1 Guest are viewing this topic.

Offline joe_cool61

  • Newbie
  • Posts: 3
    • View Profile
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

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
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: QB64: [Select]
  1. OPEN "COM1:9600,N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
  2. B1 = 197 'Command byte
  3. B2 = 21 'Motor control byte
  4. B12$ = CHR$(B1) + CHR$(B2)
  5. PUT #1, , B12$
  6.  PUT #1, , B12$
  7.  _DELAY .03
  8.  GET #1, , B3~%%
  9.  GET #1, , B4~%%
  10.  GET #1, , B5~%%
  11.  LOCATE 1, 1: PRINT B3~%%
  12.  PRINT B4~%%; B5~%%
  13.  

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

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
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.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/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


 
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
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: QB64: [Select]
  1. OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
  2. LINE INPUT #11, Result$    'clears the echo response from meter
  3.  
You are opening for input as #12 then trying to read from #11
Oh look - a sig file :-)

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
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: QB64: [Select]
  1. DIM bytestr AS STRING * 1 'one byte transfers
  2.  
  3. ' port$ is set earlier so that I can use various comm ports
  4. ' comms can be set to 9600, 19200 or 28800 in my program
  5. ' outputs to a CSV file opened as #2
  6.  
  7. OPEN "COM" + port$ + ":" + comms$ + ",N,8,1,BIN,CS0,DS0" FOR RANDOM AS #1
  8.  
  9. DO 'main loop
  10.  
  11.     'receive data in buffer when LOC > 0
  12.     IF LOC(1) THEN
  13.         GET #1, , bytestr
  14.  
  15.         IF (ASC(bytestr) = 13 AND (style = 1 OR style = 5)) THEN bytestr = ","
  16.         IF (ASC(bytestr) = 10 AND style = 1) THEN bytestr = ""
  17.  
  18.         IF (bytestr <> "") THEN PRINT #2, bytestr;
  19.         IF (bytestr <> "") THEN ptime = TIMER
  20.         ck$ = ck$ + bytestr
  21.  
  22.         IF LEN(ck$) > 11 THEN ck$ = RIGHT$(ck$, 11)
  23.  
  24.     END IF
  25.  
  26.     ptime2 = TIMER
  27.     IF (ptime2 >= (ptime + 8)) THEN ck$ = "end of data"
  28.  
  29.  
  30. LOOP UNTIL LCASE$(ck$) = "end of data"
  31.  
  32.  
Oh look - a sig file :-)

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
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: QB64: [Select]
  1. MeterSetup:
  2. OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
  3. LINE INPUT #12, Result$    'clears the echo response from meter
  4. Prompt$ = INPUT$(5, #12)   'reads the prompt returned by the meter
  5.  

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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
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

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
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!

Offline joe_cool61

  • Newbie
  • Posts: 3
    • View Profile
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

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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.

Offline Mad Axeman

  • Newbie
  • Posts: 74
    • View Profile
    • My web site - Harrythetrout
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: QB64: [Select]
  1. MeterSetup:
  2. OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR OUTPUT AS #11 'open path to Fluke 45
  3. Cmd$ = "REMS;VDC;RANGE 3;RATE M;FORMAT 1"
  4. GOSUB ProgramFluke
  5. CLOSE #11
  6. OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR INPUT AS #12 'open path to Fluke 45
  7. LINE INPUT #12, Result$    'clears the echo response from meter
  8. INPUT #12, prompt$ 'reads the prompt returned by the meter
  9.  
  10. ' rest of prog goes here to stop ProgramFluke ending with a return without gosub error
  11.  
  12. ProgramFluke:
  13. PRINT #11, Cmd$
  14.  
Oh look - a sig file :-)

Offline joe_cool61

  • Newbie
  • Posts: 3
    • View Profile
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.


Offline moises1953

  • Newbie
  • Posts: 55
    • View Profile
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: QB64: [Select]
  1. SUB PrintCOM (envio$) STATIC
  2.   DIM car AS STRING * 1
  3.   FOR i = 1 TO LEN(envio$)
  4.     car = MID$(envio$, i, 1)
  5.     PUT #1, , car
  6.   NEXT i
  7.   car = CHR$(13)
  8.   PUT #1, , car
  9.  

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
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.

Offline moises1953

  • Newbie
  • Posts: 55
    • View Profile
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'