Can't read Com port with QB64

817 views Asked by At

I am using Arduino as a slave just to read the voltage of a battery. The program in Arduino is is C. The computer program is written in QB64 It is connected to a USB com port#3. The slave program and QB64 work fine for making the Pin 13 Blink. But when I ask for a voltage all I get is 10 empty spaces no data. The following QB64 code is my attempt to read. The comments show some of the results:

CLS
ctr = 1 '                           Counts number times you press continue

again:
INPUT "Press any key to continue"; d$ ' Run program to receive voltage 
OPEN "Com3:9600,n,8,1,ds0,cs0,rs" FOR RANDOM AS #1
_DELAY .5 '                                      delay to let com settle
FIELD #1, 35 AS m$

'            ****  Form Message to send to External Device ****
Command = 2: pin = 4: argument = 0 '   Code to Get analog voltage reading
msg$ = STR$(Command) + "," + STR$(pin) + "," + STR$(argument) + "*"
PRINT "msg$ Sent= "; msg$, CHR$(13) '                     Show Message
LSET m$ = msg$
PUT #1, , m$ '                  Send message to the external device
_DELAY 1


'*****  C code in external device  *****
'External device responds to message & prints to Com3 buffer.
' if ( cmd == 2)//getanalog
'   {
'        int sensorValue = analogRead(pin);//
'        delay(10);
'        float voltage= sensorValue*(5.0/1023.0);
'        Serial.print("Voltage =");
'        Serial.print(voltage);
'        Serial.print(",");
'       Serial.print("*");
'      }

CLOSE #1

m$ = "": msg$ = "" '                            Wipe out old messages

OPEN "Com3:9600,n,8,1,ds0,cs0,rs" FOR RANDOM AS #1
_DELAY .5 '                                    Delay to let Com Port settle
FIELD #1, 50 AS m$

t1 = TIMER '                                     Start time for getdata

getdata:
bytes% = LOC(1) '                             Check receive buffer for data
IF bytes% = 0 THEN GOTO getdata 'After thousands of cycles it continues
PRINT "Bytes="; bytes '                           Bytes remain at 0
DO UNTIL EOF(1)
    GET #1, , m$ '                         First GET #1 returns EOF=-1
    PRINT "LEN(m$)= "; LEN(m$) '               Length of m$ = 10
    IF INSTR(m$, "*") > 0 THEN GOTO duh '    Never finds End of Message (*)
LOOP

IF bytes% = 0 THEN GOTO getdata
t2 = TIMER '                                         End time, Have data
DeltaT = t2 - t1 '                                   How long did it take?
GOTO stepover '               If arrived here w/o End of Message signal

duh:
PRINT "DUH instr= "; INSTR(m$, "*") '               Never hits this line
stepover:

tmp$ = "DeltaT= #.### Seconds Until LOC(1) > 0" '   Various times 
PRINT USING tmp$; DeltaT
PRINT "m$ Received= "; m$ + "&" '                 Prints 10 spaces No Data
PRINT "endofdata= "; endofdata '                endofdata=0, Never got "*"
PRINT "ctr= "; ctr, CHR$(13) '    Show number of times you pressed continue
CLOSE #1
ctr = ctr + 1
GOTO again

Also included in the comments is the Slave program that writes to the com port. Some lines of QB64 code are not necessary but were included as debugging attempt

0

There are 0 answers