You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

pl2303_xbee_terminal.ino 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Arduino terminal for PL2303 USB to serial converter and XBee radio. */
  2. /* Inserts linefeed after carriage return in data sent to and received from Xbee */
  3. /* USB support */
  4. #include <usbhub.h>
  5. /* CDC support */
  6. #include <cdcacm.h>
  7. #include <cdcprolific.h>
  8. // Satisfy the IDE, which needs to see the include statment in the ino too.
  9. #ifdef dobogusinclude
  10. #include <spi4teensy3.h>
  11. #include <SPI.h>
  12. #endif
  13. class PLAsyncOper : public CDCAsyncOper
  14. {
  15. public:
  16. uint8_t OnInit(ACM *pacm);
  17. };
  18. uint8_t PLAsyncOper::OnInit(ACM *pacm)
  19. {
  20. uint8_t rcode;
  21. // Set DTR = 1
  22. rcode = pacm->SetControlLineState(1);
  23. if (rcode)
  24. {
  25. ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
  26. return rcode;
  27. }
  28. LINE_CODING lc;
  29. lc.dwDTERate = 115200;
  30. lc.bCharFormat = 0;
  31. lc.bParityType = 0;
  32. lc.bDataBits = 8;
  33. rcode = pacm->SetLineCoding(&lc);
  34. if (rcode)
  35. ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
  36. return rcode;
  37. }
  38. USB Usb;
  39. //USBHub Hub(&Usb);
  40. PLAsyncOper AsyncOper;
  41. PL2303 Pl(&Usb, &AsyncOper);
  42. void setup()
  43. {
  44. Serial.begin( 115200 );
  45. #if !defined(__MIPSEL__)
  46. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  47. #endif
  48. Serial.println("Start");
  49. if (Usb.Init() == -1)
  50. Serial.println("OSCOKIRQ failed to assert");
  51. delay( 200 );
  52. }
  53. void loop()
  54. {
  55. Usb.Task();
  56. if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
  57. {
  58. uint8_t rcode;
  59. /* reading the keyboard */
  60. if(Serial.available()) {
  61. uint8_t data= Serial.read();
  62. if ( data == '\r' ) {
  63. Serial.print("\r\n"); //insert linefeed
  64. }
  65. else {
  66. Serial.print( data ); //echo back to the screen
  67. }
  68. /* sending to the phone */
  69. rcode = Pl.SndData(1, &data);
  70. if (rcode)
  71. ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
  72. }//if(Serial.available()...
  73. delay(50);
  74. /* reading the converter */
  75. /* buffer size must be greater or equal to max.packet size */
  76. /* it it set to 64 (largest possible max.packet size) here, can be tuned down
  77. for particular endpoint */
  78. uint8_t buf[64];
  79. uint16_t rcvd = 64;
  80. rcode = Pl.RcvData(&rcvd, buf);
  81. if (rcode && rcode != hrNAK)
  82. ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
  83. if( rcvd ) { //more than zero bytes received
  84. for(uint16_t i=0; i < rcvd; i++ ) {
  85. if( buf[i] =='\r' ) {
  86. Serial.print("\r\n"); //insert linefeed
  87. }
  88. else {
  89. Serial.print((char)buf[i]); //printing on the screen
  90. }
  91. }
  92. }
  93. delay(10);
  94. }//if( Usb.getUsbTaskState() == USB_STATE_RUNNING..
  95. }