Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Serial Communication: BASIC to Python (http://www.programmingforums.org/showthread.php?t=10788)

titaniumdecoy Jul 20th, 2006 4:44 PM

Serial Communication: BASIC to Python
 
This thread is essentially a continuation of this one; however, my problem now is translating BASIC code to Python.

I got ahold of a small piece of BASIC code someone was using to communicate with the device I described which connected to the serial port:

:

INPUT ; "Enter filename to save data- ", outfile$
OPEN outfile$ FOR OUTPUT AS #1

OPEN "COM1:9600,O,7,1" FOR RANDOM AS #2
PRINT #2, "01,TEMP?"
INPUT #2, A$, B$
INPUT #2, C$, D$, E$, F$

PRINT #1, TIME$, C$, D$, E$, F$

If it's possible to do this in just a few lines of BASIC code, it doesn't make sense to me that it should require an entire module (eg, PySerial) to do the same thing in Python. Is there a simpler way to translate this? Thanks for any input.

DaWei Jul 20th, 2006 5:07 PM

Have you tried that BASIC program on your XP machine?? http://www.daweidesigns.com/images/eyebrow.gif

Wizard1988 Jul 20th, 2006 5:12 PM

Windows XP will not be happy about that program, it will most likeley crash.

Arevos Jul 20th, 2006 5:15 PM

Quote:

Originally Posted by titaniumdecoy
If it's possible to do this in just a few lines of BASIC code, it doesn't make sense to me that it should require an entire module (eg, PySerial) to do the same thing in Python.

The functionality is obviously built into that version of BASIC, both because BASIC was a language that had a lot of built in functionality, and because serial ports were a commonly used interface when the language was developed.

Python came along much later, when it became fashionable to have libraries and modules, rather than inbuilt functionality, and when serial ports had become more or less obsolete.

Just because functionality is inbuilt into a language, doesn't mean it doesn't have overhead. At least when something is an external module you have a choice whether to use it or not.

Game_Ender Jul 20th, 2006 5:16 PM

What platform does that code run under? This is closets I could find to explaining that function call, it is for m the help manual for liberty basic. Which is windows only. Both python modules are portable on windows,linux and mac.

Using USPP I think the code translates (roughly) to:
:

from uspp import *
from time import strftime

tty=SerialPort("COM1", 1000, 9600)
tty.write("01,TEMP?") 
a = tty.read()
b = tty.read()
c = tty.read()
d = tty.read()
e = tty.read()
f = tty.read()

print '%s %d %d %d %d' % (strftime(''%H:%M:%S')c,d,e,f)


Because I can't get the refernce material online, I am not sure I am using the read method properly.

DaWei Jul 20th, 2006 5:40 PM

The reason I asked if that BASIC runs is that the code appears to date back to real-mode days, when one could access ports directly. That is no longer the case. For that to run, the interpreter would have to drop out of protected mode, just as driver or kernel code has to.

titaniumdecoy Jul 20th, 2006 5:46 PM

This is all very interesting. It sounds like PySerial/USPP is the way to go.

And Dawei, I don't know if it runs because I haven't had a chance to connect the device to my computer yet (It's very large).

DaWei Jul 20th, 2006 6:09 PM

You would have to have a BASIC interpreter (with or without a BASCOM) to use it, anyway. That code indicates that the equipment is rather straightforward, anyway. I have a link here somewhere to serial access via C/C++, but I'm not finding it. I'd probably use Python, though, if it were me.


All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC