Cov txheej txheem:

Arduino Serial Communication: 5 Kauj Ruam
Arduino Serial Communication: 5 Kauj Ruam

Video: Arduino Serial Communication: 5 Kauj Ruam

Video: Arduino Serial Communication: 5 Kauj Ruam
Video: Serial Communication between Raspberry Pi and Arduino [1H Complete Tutorial] 2024, Hlis ntuj nqeg
Anonim
Arduino Kev Sib Txuas Lus
Arduino Kev Sib Txuas Lus

Ntau txoj haujlwm Arduino vam khom xa cov ntaub ntawv ntawm ntau Arduinos.

Txawm hais tias koj yog tus nyiam ua haujlwm uas tab tom tsim lub tsheb RC, lub dav hlau RC, lossis tsim lub chaw nres tsheb huab cua nrog cov chaw taws teeb pom kev, koj yuav tsum paub yuav ua li cas ntseeg tau cov ntaub ntawv hloov pauv ntawm ib qho Arduino mus rau lwm qhov. Hmoov tsis zoo, nws nyuaj rau cov neeg nyiam ua haujlwm kom tau txais cov ntaub ntawv sib txuas lus ua haujlwm hauv lawv tus kheej cov haujlwm no. Qhov no yog vim cov ntaub ntawv xa tawm xa mus raws li kwj ntawm bytes.

Tsis muaj ib yam ntawm cov ntsiab lus hauv kwj ntawm bytes, nws yuav luag tsis tuaj yeem txhais cov ntaub ntawv. Tsis muaj peev xwm txhais cov ntaub ntawv, koj Arduinos yuav tsis muaj peev xwm sib txuas lus tau zoo. Qhov tseem ceeb yog ntxiv cov ntaub ntawv ntsiab lus no rau hauv byte kwj los ntawm kev siv tus qauv txheej txheem pob ntawv tsim.

Kev tsim cov pob ntawv ntim khoom, pob ntawv ntim khoom, thiab pob ntawv cais yog qhov nyuaj thiab nyuaj rau ua tiav. Hmoov zoo rau Arduino cov neeg siv, muaj cov tsev qiv ntawv muaj uas tuaj yeem ua txhua yam ntawm cov laj thawj nyuaj tom qab qhov xwm txheej yog li koj tuaj yeem tsom mus rau kom koj txoj haujlwm ua haujlwm yam tsis muaj lub taub hau ntxiv. Cov Lus Qhia no yuav siv lub tsev qiv ntawv SerialTransfer.h rau kev ua cov pob ntawv ntim.

Hauv qhov luv: Cov Lus Qhia no yuav dhau mus li cas koj tuaj yeem siv cov ntaub ntawv muaj zog ruaj khov yooj yim hauv ib qhov haujlwm siv lub tsev qiv ntawv SerialTransfer.h. Yog tias koj xav kawm paub ntau ntxiv txog kev tshawb fawb qib qis ntawm kev sib txuas lus ruaj khov, saib cov ntawv qhia no.

Khoom siv

  • 2 Arduinos

    Nws raug txhawb siab tias koj siv Arduinos uas muaj ntau yam khoom siv UARTs (piv txwv li Arduino Mega)

  • Hookup hlau
  • Nruab SerialTransfer.h

    Muaj los ntawm Arduino IDE Tus Thawj Saib Xyuas Tsev Qiv Ntawv

Kauj Ruam 1: Kev Sib Txuas Lub Cev

Kev Sib Txuas Lub Cev
Kev Sib Txuas Lub Cev

Thaum siv kev sib txuas lus hauv xov tooj cua, ob peb lub ntsiab lus thaiv kab mob yuav tsum tau khaws cia hauv siab:

  • Nco ntsoov tias txhua thaj chaw txuas nrog!
  • Arduino TX (Transmit) tus pin xav tau txuas nrog rau lwm tus Arduino's RX (Txais) tus pin

Kauj Ruam 2: Yuav Siv Lub Tsev Qiv Li Cas

Yuav Siv Lub Tsev Qiv Li Cas
Yuav Siv Lub Tsev Qiv Li Cas

SerialTransfer.h tso cai rau koj xa cov ntaub ntawv ntau ntau yooj yim siv cov txheej txheem txheej txheem txheej txheem. Hauv qab no yog cov lus piav qhia ntawm txhua lub tsev qiv ntawv cov yam ntxwv - ntau yam uas peb yuav siv tom qab hauv qhov kev qhia no:

SerialTransfer.txBuff

Nov yog qhov byte array qhov twg txhua cov ntaub ntawv them nqi xa mus rau ntau dua yog tsis ua ntej kis. Koj tuaj yeem ntim cov khoom no nrog bytes ntawm cov ntaub ntawv xa mus rau lwm Arduino.

SerialTransfer.rxBuff

Nov yog qhov byte array uas txhua cov ntaub ntawv them nyiaj tau txais los ntawm lwm qhov Arduino tsis nyob.

SerialTransfer.bytesRead

Tus lej them nyiaj bytes tau txais los ntawm lwm Arduino thiab khaws cia hauv SerialTransfer.rxBuff

SerialTransfer.begin (Kwj & _port)

Pib ua piv txwv ntawm chav qiv ntawv. Koj tuaj yeem hla ib qho "Serial" chav kawm khoom raws li qhov ntsuas - txawm tias "SoftwareSerial" cov khoom hauv chav kawm!

SerialTransfer.sendData (const uint16_t & messageLen)

Qhov no ua rau koj Arduino xa "messageLen" tus lej ntawm bytes hauv qhov kis tsis mus rau lwm Arduino. Piv txwv li, yog tias "messageLen" yog 4, thawj 4 bytes ntawm SerialTransfer.txBuff yuav xa los ntawm cov lej mus rau lwm Arduino.

SerialTransfer.available ()

Qhov no ua rau koj Arduino txheeb xyuas cov ntaub ntawv tau txais los ntawm lwm qhov Arduino. Yog tias txoj haujlwm no rov qab boolean "tseeb", nws txhais tau tias pob ntawv tshiab tau ua tiav kev txheeb xyuas thiab cov pob ntawv tau txais tshiab cov ntaub ntawv khaws cia/muaj nyob hauv SerialTransfer.rxBuff.

SerialTransfer.txObj (const T & val, const uint16_t & len, const uint16_t & index = 0)

Cov khoom "len" tus lej ntawm bytes ntawm qhov khoom xav tau (byte, int, ntab, ob npaug, qauv, thiab lwm yam …) rau hauv qhov kis tsis kis pib ntawm qhov ntsuas raws li tau teev tseg los ntawm kev sib cav "ntsuas".

SerialTransfer.rxObj (const T & val, const uint16_t & len, const uint16_t & index = 0)

Nyeem "len" tus lej bytes los ntawm qhov tsis tau txais (rxBuff) pib ntawm qhov ntsuas raws li tau teev tseg los ntawm kev sib cav "ntsuas" mus rau qhov khoom xav tau (byte, int, ntab, ob npaug, qauv, thiab lwm yam …).

NCO TSEG:

Txoj hauv kev yooj yim tshaj plaws rau xa cov ntaub ntawv yog ua ntej txhais cov qauv uas muaj txhua cov ntaub ntawv koj xav xa. Arduino ntawm qhov kawg tau txais yuav tsum muaj cov qauv zoo ib yam.

Kauj Ruam 3: Xa Cov Ntaub Ntawv Yooj Yim

Cov duab kos hauv qab no xa ob qho tib si ADC tus nqi ntawm analogRead (0) thiab tus nqi ntawm analogRead (0) hloov pauv mus rau qhov hluav taws xob rau Arduino #2.

Tshaj tawm cov duab kos hauv qab no rau Arduino #1:

#suav nrog "SerialTransfer.h"

SerialTransfer myTransfer; cov txheej txheem STRUCT {uint16_t adcVal; ntab voltage; } cov ntaub ntawv; teeb tsa tsis muaj dab tsi () {Serial.begin (115200); Serial1.begin (115200); myTransfer.begin (Serial1); } tsis muaj dab tsi voj () {data.adcVal = analogRead (0); data.voltage = (data.adcVal * 5.0) / 1023.0; myTransfer.txObj (data, sizeof (data)); myTransfer.sendData (sizeof (cov ntaub ntawv)); ncua (100); }

Kauj Ruam 4: Tau Txais Cov Ntaub Ntawv Tseem Ceeb

Cov cai hauv qab no luam tawm ADC thiab qhov muaj txiaj ntsig tau txais los ntawm Arduino #1.

Tshaj tawm cov cai hauv qab no rau Arduino #2:

#suav nrog "SerialTransfer.h"

SerialTransfer myTransfer; struct QHUAS {uint16_t adcVal; ntab voltage; } cov ntaub ntawv; teeb tsa tsis muaj dab tsi () {Serial.begin (115200); Serial1.begin (115200); myTransfer.begin (Serial1); } void loop () {yog (myTransfer.available ()) {myTransfer.rxObj (data, sizeof (data))); Serial.print (data.adcVal); Serial.print (''); Serial.println (data.voltage); Serial.println (); } lwm tus yog (myTransfer.status <0) {Serial.print ("ERROR:"); yog (myTransfer.status == -1) Serial.println (F ("CRC_ERROR")); lwm tus yog (myTransfer.status == -2) Serial.println (F ("PAYLOAD_ERROR")); lwm tus yog (myTransfer.status == -3) Serial.println (F ("STOP_BYTE_ERROR")); }}

Kauj ruam 5: Xeem

Thaum ob daim duab kos tau muab tso rau lawv qhov Arduinos, koj tuaj yeem siv Serial Monitor ntawm Arduino #2 txhawm rau txheeb xyuas koj tau txais cov ntaub ntawv los ntawm Arduino #1!

Pom zoo: