Реализовано чтение данных из порта (в моем случае СОМ4) в потоке.
Данные выводятся в виде вертикального линейного графика.
Выход из программы - клавиша ESC.
Для ознакомительных целей
- Код: Выделить всё
#COMPILE EXE
#DIM ALL
#INCLUDE "Win32Api.inc"
%WINDOW_MAX = 600& ' ширина/высота окна для вывода
GLOBAL nComm AS LONG
GLOBAL fUpdating AS LONG
GLOBAL fCloseThread AS LONG
GLOBAL tBuff AS STRING
GLOBAL tWin AS DWORD
GLOBAL vBuff, fInitGraph, fBufReady AS LONG
'
'
FUNCTION map(BYVAL x AS LONG, BYVAL in_min AS LONG, BYVAL in_max AS LONG,BYVAL out_min AS LONG, BYVAL out_max AS LONG) AS LONG
FUNCTION = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
END FUNCTION
'
'
FUNCTION ArduinoInit () AS LONG
LOCAL dummy AS STRING
COMM OPEN "COM4" AS #nComm
IF ERRCLEAR THEN EXIT FUNCTION
COMM SET #nComm, BAUD = 9600
COMM SET #nComm, BYTE = 8
COMM SET #nComm, PARITY = %False
COMM SET #nComm, STOP = 1
COMM SET #nComm, TXBUFFER = 4096
COMM SET #nComm, RXBUFFER = 4096
COMM PRINT #nComm, $NUL
SLEEP 500 '2000
IF COMM(#nComm, RXQUE) THEN
COMM RECV #nComm, COMM(#nComm, RXQUE), dummy
END IF
FUNCTION = %TRUE
END FUNCTION
'
'
SUB ArduinoEnd ()
LOCAL dummy AS STRING
SLEEP 1000
IF COMM(#nComm, RXQUE) THEN
COMM RECV #nComm, COMM(#nComm, RXQUE), dummy
END IF
COMM CLOSE #nComm
END SUB
'
'
THREAD FUNCTION ReceiveData (BYVAL tWin AS DWORD) AS LONG
LOCAL sBigBuffer, sBuffer, sTxt AS STRING, ncBytesInBuffer AS LONG
WHILE ISFALSE fCloseThread
ncBytesInBuffer = COMM(#nComm, RXQUE)
IF ISFALSE ncBytesInBuffer OR fUpdating THEN
stxt = ""
'SLEEP 100
ITERATE LOOP
END IF
'-Read incoming characters.
COMM RECV #nComm, ncBytesInBuffer, sBuffer
sBigBuffer = sBigBuffer & sBuffer
fBufReady = %FALSE
IF INSTR(sBigBuffer, $CR) THEN
vBuff = VAL(sBigBuffer)
fBufReady = %TRUE
sBigBuffer = ""
END IF
WEND
FUNCTION = %TRUE
END FUNCTION
FUNCTION GraphInit() AS LONG
LOCAL i,x,n AS LONG
GRAPHIC LINE (1,15)-(%WINDOW_MAX + 1,15), %BLACK
FOR i = 1 TO 11
x = 1+ (%WINDOW_MAX / 10) * (i-1)
IF i MOD 2 =0 THEN
GRAPHIC LINE (x,10)-(x,15), %BLACK
ELSE
GRAPHIC LINE (x,1)-(x, %WINDOW_MAX), %BLACK
GRAPHIC SET POS (x+2, 1)
GRAPHIC PRINT STR$(n)
n+=1
END IF
NEXT i
END FUNCTION
FUNCTION GraphDisplay(BYVAL tVal AS LONG) AS LONG
LOCAL aLog AS LONG
STATIC yPos, Xold, YOld AS LONG
IF fInitGraph = %TRUE THEN
yPos = 20
xOld = 1
Yold = 20
fInitGraph = %FALSE
END IF
' АЦП в Ардуино 10 разрядный, поэтому максимальное значение 1024
'
alog=map(tVal,0,1024,1, %WINDOW_MAX + 20)
GRAPHIC LINE( xOld,Ypos)-(alog, Ypos+1), %BLUE
IF YPos+1 >= %WINDOW_MAX THEN
Ypos=20
GRAPHIC CLEAR
CALL GraphInit
ELSE
YPos +=1
Xold = alog
END IF
SLEEP(10)
END FUNCTION
FUNCTION PBMAIN () AS LONG
LOCAL hThread, lRet, hF AS LONG
hF = FREEFILE
' OPEN "data" FOR OUTPUT AS #hf
GRAPHIC WINDOW NEW "Arduino Grapher",100,100, %WINDOW_MAX+20, %WINDOW_MAX+20 TO tWin
GRAPHIC ATTACH tWin, 0
IF ISFALSE ArduinoInit THEN
MSGBOX "Arduino connection failed!",,""
EXIT FUNCTION
END IF
THREAD CREATE ReceiveData(tWin) TO hThread
' ---
fInitGraph = %TRUE
CALL GraphInit
DO
IF fBufReady = %TRUE THEN GraphDisplay(vBuff)
IF GRAPHIC$(INKEY$)=CHR$(27) THEN EXIT DO
LOOP
fCloseThread = %TRUE ' - close "listen" thread
DO
THREAD CLOSE hThread TO lRet
SLEEP 0
LOOP UNTIL ISTRUE lRet
CALL ArduinoEnd() '
GRAPHIC WINDOW END tWin
' CLOSE #hf
END FUNCTION
Код контроллера.
Надо скомпилировать и загрузить в Ардуино.
Читаем аналоговый сигнал с ножки А0 и отправляем результат в порт.
- Код: Выделить всё
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println (analogRead(A0)) ;
delay(10);
}
Скоро запостирую програмку для нахождения порта Ардуино.