我使用 Arduino 作为奴隶只是为了读取电池的电压。Arduino中的程序是C。计算机程序是用QB64编写的,它连接到USB com端口#3。从程序和 QB64 可以很好地使引脚 13 闪烁。但是当我要求电压时,我得到的只是 10 个空格,没有数据。以下 QB64 代码是我尝试阅读的。评论显示了一些结果:
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
注释中还包括写入 com 端口的 Slave 程序。一些 QB64 代码行不是必需的,但作为调试尝试包含在内