Serial Port Input

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
vanwa
Newbie
Posts: 2
Joined: Thu Dec 06, 2007 8:12 pm

Serial Port Input

Post by vanwa »

Hi,
I am new to this board and I am not a very experienced Basic programmer.
I am trying to read and count a pulse train from the serial port. The code looks like this:
INPUT "ENTER PULSE COUNT"; P%
PIN = &H3F8: REM COM1 PORT ADDRESS
DO
IN1=-((INP(PIN + 6) AND 128) = 128): REM READ INPUT PIN 1
IF IN1 = 1 THEN NR =1 ELSE NR=0
NR=NR+1
LOOP UNTIL NR = P%
********************************
It does not work. Is this because the pulse train is to fast? The pulses have a period of 0.15ms which is 5kHz and and are 12V, or is the programming wrong?
Any suggestins will be greatly appreciated.
Thanks.
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss »

The NR variable will never reach more then two becuase this line:
IF IN1 = 1 THEN NR = 1 ELSE NR = 0
Sets NR to 0 or 1. So the maximal value of NR at the end of the loop is two (1 from the IF statement and one because you added one after that with NR = NR + 1)

What always helps is looking at your program and write what it actually does. Like this:
IF IN1 = 1 THEN NR = 1 ELSE NR = 0
Sets NR to one if IN1 is one, else NR is set to zero.
NR = NR + 1
Increases NR by one.
It's also a good tip to check what values you expect from variables at certain moments and what values they actually have at that moment(try making a breakpoint with F9 when you're in QBasic). When variables have the value you expect at one moment, but a value that is too high or too low, then the bug is probably between this two lines of code.

BTW, it's still possible that there are more small bugs, but I don't know much about that ports etc. I wonder what are you trying to do?

EDIT: I see that you use different types of variables also. Try to make the most variables of one type. I prefer integers, they can only hold integer numbers but are very fast.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

COM PORTS

Post by burger2227 »

COM ports read data, not usually pulses. Do you have an OPEN COM statement? QB can open only COM 1 or 2. Did you find the address from Windows Devices first? A COM port is capable of speeds up to 11,500 bytes per second on the data transmit and receive lines.

You seem to be using the Modem Status Register &H03FE or base address + 6. The pin you are sending the pulse to is CD or carrier detect. The bit is 7 so AND 128 should indicate if it is high or low. So you are close!

Code: Select all

DEFINT A-Z    'use integers only
DO
    IF (INP(&H3FE) AND 128) THEN    'brackets required 
       count = count + 1
       DO: LOOP UNTIL (INP(&H3FE) AND 128) = 0
    END IF
LOOP UNTIL count = P
I have not tested this. I hope it helps. The COM port may have to be opened first. Be careful as devices and voltages can destroy ports. You will also need a common ground to your pulse circuit from the port. A 9 pin connector uses pin 5 and a 25 pin uses pin 1 for ground.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
vanwa
Newbie
Posts: 2
Joined: Thu Dec 06, 2007 8:12 pm

It works!!

Post by vanwa »

Thanks to Burger2227 and Codemss. Your suggestions were essential.
I re-wrote the code and it works:

DO
IF -((INP(&3HFE) AND 128) = 128 THEN
CT=CT+1
DO
LOOP UNTIL -((INP(&H3FE) AND 128) = 0
END IF
LOOP UNTIL CT = L

My project is a wire cutting machine. A DC motor with an optical encoder output measures the wire and a cutter device cuts the wire. I am entering the wire length and wire count. There is only one input (encoder) and two outputs, for motor run/stop and a cutting solenoid. The encoder output is 5000 pulses a second, pretty fast.
Post Reply