Using QBasic to serial send to Picaxe

Texy

Senior Member
I,ve decided to ditch using op-amps to condition the signal for my altimeter and just use a max187 12-bit adc to supply the voltage to the 18x picaxe.
Before, when using the internal 10-bit ADC, I was able to 'manually' program the lookup table of a 24lc256 eeprom by sending 256 bytes from internal eeprom space over i2c. As there were 1024 words to write, it took 8 versions of the program with different data and addresses to completely program the table. Now that I have 4 times that amount of data to program, I,d like a easier approach. My idea was to use quick basic to supply the data from a file, sending it over via a serial port, and the picaxe then sending over i2c to the relevant address of the i2c eeprom. However, my program seems to stall after the 1st byte sent and I don't understand why. Here is the qbasic code, only to get the principle working, ie not complete :

Code:
OPEN "COM2:2400,N,8,1,RS,CS,DS,CD" FOR RANDOM AS #1
OPEN "table.txt" FOR INPUT AS #2
start:
INPUT #2, abc$
PRINT abc$
PRINT #1, abc$; CHR$(13); CHR$(10)
FOR j = 0 TO 50: NEXT j
INPUT #1, def$
PRINT "Received : "; def$
GOTO start
..and the picaxe code, again, only to prove the principle :

Code:
symbol LED=0
serout 5,N2400,("Hi",cr,lf)
;goto main
start:
serin 0,N2400,b1,b0,b3,b2
high LED
pause 2000
serout 5,N2400,(b1,b0,b3,b2,cr,lf)
low LED
pause 2000
goto start
Note that I,m using port 0 for IN, and port 5 for OUT of the picaxe.

When the QBasic code is run (after the picaxe is already running) the following is seen typically:

E796
Received : E796
E753

...and then it stops. Note that the LED comes on, then goes out, so I am assuming that the picaxe has stopped at the 'start:' label, ie waiting for more serial input. If I hit the reset button on the picaxe, 'Hi' is sent and received by qbasic, then the next number in the talbe is successfully sent and received, ie :

E796
Received : E796
E753
Received : Hi
E710
Received : E710
E6CD

..and stops again.
Any idea's why only 1 packet is sent and received? Incidently I,d be quite happy to move over to VB.net code, as I,ve followed a couple of the Doc's Instructable's, if its possible.
Cheers,
Texy
 

moxhamj

New Member
A few possible problems. First, in QB, if you open a file you need to close it as well.

Second, the delays are confusing, and if you are doing formal two way comms, you need handshaking both ways. Delays work, but only on that particular computer at that clock speed.

Third, I believe any serin on a picaxe really needs a header with at least a couple of bytes eg "ABC". And it helps to put a checksum at the end of a packet as well.

Fourth, and this might actually be the problem, when you say "input #2,abc$", what is abc$? Is this from a file, and if so, how many bytes is it. I'm presuming it is two bytes for the program to work properly, because you then go and add two more bytes with the chr13 and chr10, and the serin on the picaxe is expecting 4 bytes.
 

Texy

Senior Member
A few possible problems. First, in QB, if you open a file you need to close it as well.

Second, the delays are confusing, and if you are doing formal two way comms, you need handshaking both ways. Delays work, but only on that particular computer at that clock speed.

Third, I believe any serin on a picaxe really needs a header with at least a couple of bytes eg "ABC". And it helps to put a checksum at the end of a packet as well.

Fourth, and this might actually be the problem, when you say "input #2,abc$", what is abc$? Is this from a file, and if so, how many bytes is it. I'm presuming it is two bytes for the program to work properly, because you then go and add two more bytes with the chr13 and chr10, and the serin on the picaxe is expecting 4 bytes.
Thanks Doc,
I,m aware of needing to close the files by the end of the program, its just not in the code I listed.
WRT speed, well I got delays in and I,ve experimented with them. Its a one off really as once the table is written to the eeprom, I,ll not need to do it again. However, I may need to experiment some more......
I,ve also only just taken out the ABC header before posting my code, again to see if I got it wrong in either program, and it doesn't seem to make much difference, however, I will put it back in again.
Channel #2 is from a txt file, each data line is a 4 digit hex number (with CRLF at the end), eg

E796
E753
E710
E6CD
E68A
E647
E605
I,m pretty happy with the reading from the file part - abc$ is printed correctly immediately after reading from the file. I,m reading from the file, and consequently sending via serial as 4 digit (ie byte) ascii string, rather than 2 byte data as I thought raw data might cause ill effects.

I,m open to offers on alternate ways of doing this. I think its possible via my picstart plus and mplab? But I,d much rather do it with the picaxe to help my learning curve:D

Texy
 

moxhamj

New Member
Maybe that is the problem "Channel #2 is from a txt file, each data line is a 4 digit hex number (with CRLF at the end)"

So it is actually a 6 digit number if you add the CRLF. And the picaxe serin is only reading in 4?

Need to break down the code into chunks that work. Then build it back up again. CR and LF are not really needed for instance in the serial data stream. If you want a display to look neater, add them to the display code in qbasic.

You might find this helpful too http://www.serial-port-monitor.com/free-serial-port-monitor-downloads.html

A software serial port monitor so you can see what is going back and forth.

VB.net has a timeout on the serial port which helps a lot - you send a byte, you expect one back but you timeout after a second if nothing comes back. And you can clear the input buffer before you read it - sometimes the input buffer on the PC gets full.

If you have XP and a broadband connection, have a look at vb.net - it is a free download and the program used to sell for hundreds of dollars. Grab it while it is free! http://www.instructables.com/id/Control-real-world-devices-with-your-PC/
 
Last edited:

Texy

Senior Member
Hmmmmm, but surely the abc string does not store the CRLF? Otherwise, when you, for example
PRINT abc$;abc$;abc$
you'd get

1234
1234
1234

When actually you get

123412341234


I,ve already downloaded vb.net and tried a couple of your instructables, I,ve just not had a play 'on my own' yet, only your examples.

Texy
 

hippy

Ex-Staff (retired)
Not sure where to start, but this is how I'd do it. Untested because I don't use QB ...

Code:
Open "COM2:2400,N,8,1,RS,CS,DS,CD" For Random As #1
Open "table.txt" For Input As #2
Print#1, "START";
Input#1, reply$
Print reply$
addrToSend = 0
While Not Eof(2)
  Input #2, dataToSend$
  dataToSend$ = Ltrim$(Str$(Val("&h"+dataToSend$)))
  Print "Send",Hex$(addrToSend),Hex$(Val(dataToSend$)),"";
  Print #1, dataToSend$+Chr$(&h0D);
  Input #1, addrRxd$
  Input #1, dataRxd$
  Print "Received",Hex$(Val(addrRxd$)),Hex$(Val(dataRxd$))
  addrToSend = addrToSend+1
Wend
Close#1
Close#2
End
Code:
Symbol addrRxd = w0
Symbol dataRxd = w1
Symbol LED     = 0

addrRxd = 0
SerIn 0,N2400,("START")
SerOut 5,N2400,("Started",CR,LF)
Do
  SerIn 0,N2400,#dataRxd
  High LED
  ' Store the dataRxd here
  Pause 1000
  Low LED
  SerOut 5,N2400,(#addrRxd,CR,LF)
  SerOut 5,N2400,(#dataRxd,CR,LF)
Loop
 
Last edited:

Texy

Senior Member
Thanks for the code. Well I seem to be getting the same results - see screendump attached. I wonder if its my circuit or the PC I,m using (at work:eek:). I will give it another go tonight on my laptop and will let you know
how I get on. Thanks for the help so far guys.

Oh and for some additional info, I,m using the usb download cable, swapping between the download socket to program, and another socket on the breadboard. The usb serial port is configured as comm port 2 as qbasic only supports ports 1 and 2 in its native form without tweeking. I,m thinking I mite make a (proper) serial cable as its a pain to keep swapping.
Texy
 

Attachments

Last edited:

hippy

Ex-Staff (retired)
Hmm ... could be a timing issue, the QB sending before the PICAXE is ready.

There's also a problem with sending what QB considers negative numbers, you could try changing

dataToSend$ = Ltrim$(Str$(Val("&h"+dataToSend$)))

to

n = Val("&h"+dataToSend$)
If n < 0 Then
n = 65536 + n
End If
dataToSend$ = Ltrim$(Str$(n))

You might have to fudge something so n is a 32-bit integer. I Don't know enough about QB to help there.
 
Top