From 568f00b65bbacba8f735911967632cebcb66de33 Mon Sep 17 00:00:00 2001 From: Oleg Mazurov Date: Fri, 24 Jun 2011 11:49:19 -0600 Subject: [PATCH] PL2303 Xbee terminal added --- examples/acm/acm_terminal/acm_terminal.pde | 8 +- .../pl2303_xbee_terminal.pde | 120 ++++++++++++++++++ 2 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde diff --git a/examples/acm/acm_terminal/acm_terminal.pde b/examples/acm/acm_terminal/acm_terminal.pde index 8ed56fed..92141c78 100644 --- a/examples/acm/acm_terminal/acm_terminal.pde +++ b/examples/acm/acm_terminal/acm_terminal.pde @@ -84,9 +84,11 @@ void loop() delay(50); /* reading the phone */ - /* buffer size must be equal to max.packet size */ - uint8_t buf[32]; - uint16_t rcvd = 32; + /* buffer size must be greater or equal to max.packet size */ + /* it it set to 64 (largest possible max.packet size) here, can be tuned down + for particular endpoint */ + uint8_t buf[64]; + uint16_t rcvd = 64; rcode = Acm.RcvData(&rcvd, buf); if (rcode && rcode != hrNAK) ErrorMessage(PSTR("Ret"), rcode); diff --git a/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde b/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde new file mode 100644 index 00000000..89c15420 --- /dev/null +++ b/examples/pl2303/pl2303_xbee_terminal/pl2303_xbee_terminal.pde @@ -0,0 +1,120 @@ +/* Arduino terminal for PL2303 USB to serial converter and XBee radio. */ +/* Inserts linefeed after carriage return in data sent to and received from Xbee */ +/* USB support */ +#include +#include +#include +#include +#include +#include +#include +#include +/* CDC support */ +#include +#include +/* Debug support */ +#include +#include +#include +#include + +class PLAsyncOper : public CDCAsyncOper +{ +public: + virtual uint8_t OnInit(ACM *pacm); +}; + +uint8_t PLAsyncOper::OnInit(ACM *pacm) +{ + uint8_t rcode; + + // Set DTR = 1 + rcode = pacm->SetControlLineState(1); + + if (rcode) + { + ErrorMessage(PSTR("SetControlLineState"), rcode); + return rcode; + } + + LINE_CODING lc; + lc.dwDTERate = 115200; + lc.bCharFormat = 0; + lc.bParityType = 0; + lc.bDataBits = 8; + + rcode = pacm->SetLineCoding(&lc); + + if (rcode) + ErrorMessage(PSTR("SetLineCoding"), rcode); + + return rcode; +} +USB Usb; +//USBHub Hub(&Usb); +PLAsyncOper AsyncOper; +PL Pl(&Usb, &AsyncOper); + +void setup() +{ + Serial.begin( 115200 ); + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSCOKIRQ failed to assert"); + + delay( 200 ); +} + +void loop() +{ + Usb.Task(); + + if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) + { + uint8_t rcode; + + /* reading the keyboard */ + if(Serial.available()) { + uint8_t data= Serial.read(); + + if ( data == '\r' ) { + Serial.print("\r\n"); //insert linefeed + } + else { + Serial.print( data ); //echo back to the screen + } + + /* sending to the phone */ + rcode = Pl.SndData(1, &data); + if (rcode) + ErrorMessage(PSTR("SndData"), rcode); + }//if(Serial.available()... + + delay(50); + + /* reading the converter */ + /* buffer size must be greater or equal to max.packet size */ + /* it it set to 64 (largest possible max.packet size) here, can be tuned down + for particular endpoint */ + uint8_t buf[64]; + uint16_t rcvd = 64; + rcode = Pl.RcvData(&rcvd, buf); + if (rcode && rcode != hrNAK) + ErrorMessage(PSTR("Ret"), rcode); + + if( rcvd ) { //more than zero bytes received + for(uint16_t i=0; i < rcvd; i++ ) { + if( buf[i] =='\r' ) { + Serial.print("\r\n"); //insert linefeed + } + else { + Serial.print(buf[i]); //printing on the screen + } + } + } + delay(10); + }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING.. +} + +