how to get input from COM1 and how to parse it?

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
PipeRifle
Coder
Posts: 13
Joined: Tue Dec 13, 2005 12:51 am
Contact:

how to get input from COM1 and how to parse it?

Post by PipeRifle »

I have a measuring device which uses a COM1 port as output to PC. It sends data at a particular format which i know, and being used, via windows telnet.

I want to use QBASIC to make simple data acquisition program, but I can't really use QBASIC for such things despite my interest in it for several years back.

May I ask you my dear friends, how can I use QBASIC for such task?
I follw the examples in QBASIC help, but it didn't help much.

Here is the detail of the program should be:
The program will acquire the data from COM1 and store it in a variable.
I need to "process" the data stored in that variable before storing it in file.
The data being fed is in format: blabla123bla and I need to extract the "123" part, ignoring the rest of the data, then store it in integer variable.

I humbly apologize for any inconveniences I may created, and many thanks for any help all of you can give.
One defeated only when one stopped wanting to win
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

This is the sort of thing I used BASIC for, for years!

Tell me more about the "blablabla" part. Specifically, is it always the same number of characters? Is it always non-numeric, and you want to save the numbers? Do the numbers you want always fall in the same columns?

Could you possibly post a sample of the data you collect?

I KNOW I can help you with this.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
PipeRifle
Coder
Posts: 13
Joined: Tue Dec 13, 2005 12:51 am
Contact:

Post by PipeRifle »

Thank You very much, this is the details of the data being sent:

The data is being sent on a 16 digit format, namely d15 to d0

d0
the last digit is the end word.

d1-d4
the Upper display reading, with d1=Least significant digit,and d4=Most significant digit

d5-d14
Not required...

d15
start word

d1-d4 are numbers, but being stored as string variable, unfortunately I don't know how to convert them to integer.

Generally, the d5-d14 are boolean data mentioning the status of the device, but always constant once the data measurement begins... so I don't need them.

That's fairly all that the manual told about connecting the device to PC.
And because of that, i had to spend some time figuring out the data format (baud rate, parity, such things like that..)

Sadly enough, because of various trivial reasons, I can't give you the example of the data being fed. (mainly because of the screw-ups recently happened on my PC)

Note: I have wire tricked the COM1 port by shorting the pin 1,4,6 so that the COM1 port always ready to receive the data.

I'm looking forward for your reply.
One defeated only when one stopped wanting to win
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

You cant if the QBASIC program is being run on a windows computer. End of story.
I have left this dump.
PipeRifle
Coder
Posts: 13
Joined: Tue Dec 13, 2005 12:51 am
Contact:

Post by PipeRifle »

Ahh... indeed, QBASIC based software will suffer greatly in windows environment.

But worry not, because I am not working in Windows :D

In fact... because of the massive usage of processing power and the need of zero interruption in my program... working in Windows is a disaster.

Sincere thanks for warning me. :wink:
One defeated only when one stopped wanting to win
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

Well, I can't really tell you how to get this thing hooked up and talking to QBasic. The only thing along those lines that I've ever done is read data from a serial device. The manufacturer suggested some type of modem/terminal software, which worked, but I preferred to write my own and I did.

Once you get the data in an ASCII file, then I can tell you what to do with it, but then, so could most everyone here, probably.

The right$(), left$(), and mid$() functions will help you take strings apart into "sub-strings" and the val() function will convert a text string into a numeric value.

As for how to get the com port working, you're already over my head with that one; sorry.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
RyanKelly
Coder
Posts: 48
Joined: Sun Jan 22, 2006 6:40 pm
Contact:

Post by RyanKelly »

Z!re wrote:You cant if the QBASIC program is being run on a windows computer. End of story.
Not true. Most issues with QB and a COM port can be solved by turning off the FIFO buffer.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

RyanKelly wrote:
Z!re wrote:You cant if the QBASIC program is being run on a windows computer. End of story.
Not true. Most issues with QB and a COM port can be solved by turning off the FIFO buffer.
Not true, again. There is no direct hardware access in Windows (Counting from versions after win98)
So accessing such things from inside a DOS shell is, impossible.
I have left this dump.
RyanKelly
Coder
Posts: 48
Joined: Sun Jan 22, 2006 6:40 pm
Contact:

Post by RyanKelly »

Z!re wrote:
RyanKelly wrote:
Z!re wrote:You cant if the QBASIC program is being run on a windows computer. End of story.
Not true. Most issues with QB and a COM port can be solved by turning off the FIFO buffer.
Not true, again. There is no direct hardware access in Windows (Counting from versions after win98)
So accessing such things from inside a DOS shell is, impossible.

Qbasic uses the BIOS services for accessing serial ports, and these are software interrupts. The INT instruction generates a fault, which NTVDM catches, and I can't speak for all XP distributions everywhere, but mine seems to emulate it just fine, along with quite a number of direct IO faults as well.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

RyanKelly wrote:
Z!re wrote:
RyanKelly wrote: Not true. Most issues with QB and a COM port can be solved by turning off the FIFO buffer.
Not true, again. There is no direct hardware access in Windows (Counting from versions after win98)
So accessing such things from inside a DOS shell is, impossible.

Qbasic uses the BIOS services for accessing serial ports, and these are software interrupts. The INT instruction generates a fault, which NTVDM catches, and I can't speak for all XP distributions everywhere, but mine seems to emulate it just fine, along with quite a number of direct IO faults as well.
Well, thats special.
I have left this dump.
PipeRifle
Coder
Posts: 13
Joined: Tue Dec 13, 2005 12:51 am
Contact:

Post by PipeRifle »

Thanks for the help zim. It relly helps.

By the way, i got this message: "Device Timeout" when I try to run the program I made. Whoa.... what have I done wrong?

:(

Help needed.
One defeated only when one stopped wanting to win
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

You are welcome. Once you get output into a text file, let me see it and we'll go from there.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
PipeRifle
Coder
Posts: 13
Joined: Tue Dec 13, 2005 12:51 am
Contact:

Post by PipeRifle »

Ahem, well that's the problem...

I could use HyperTerminal to extract the data (it works quite easily there) and get the small parsing code in the QB to make all the data came out the way I want it to be.

Alas, my QB skill is quite lacking. Although I have checked the Help and Examples, my data acquisition program can't run properly.

It says: "Bad File Mode"

Here's where the program stopped:

OPEN "COM1:9600,N,8,1" FOR INPUT AS #2
GET #2, 1, adcrec$

Did I doing it wrong?
One defeated only when one stopped wanting to win
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

Now, mind you, I'm used to doing things the old (qb v3) way, but I think you have to use the Field statement before you use Get. But then you need to open the file as Random, rather than for Input. Maybe if you just use INPUT # instead of GET #. Also, with QB 4 and higher it's easier to open the file as BINARY, then read as many bytes as you need. I don't have the QB ref manual in front of me, but check some of those keywords (also SEEK) and examples and try again. You're on the right track!
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
Post Reply