| 34 | }; |
| 35 | |
| 36 | void SerialCommunicationApp::setup() |
| 37 | { |
| 38 | mCounter = 0; |
| 39 | mLastRead = 0; |
| 40 | mLastUpdate = 0; |
| 41 | mSendSerialMessage = false; |
| 42 | |
| 43 | // print the devices |
| 44 | for( const auto &dev : Serial::getDevices() ) |
| 45 | console() << "Device: " << dev.getName() << endl; |
| 46 | |
| 47 | try { |
| 48 | Serial::Device dev = Serial::findDeviceByNameContains( "tty.usbserial" ); |
| 49 | mSerial = Serial::create( dev, 9600 ); |
| 50 | } |
| 51 | catch( SerialExc &exc ) { |
| 52 | CI_LOG_EXCEPTION( "coult not initialize the serial device", exc ); |
| 53 | exit( -1 ); |
| 54 | } |
| 55 | |
| 56 | // wait for * as a sign for first contact |
| 57 | char contact = 0; |
| 58 | while( contact != '*' ) { |
| 59 | contact = (char)mSerial->readByte(); |
| 60 | } |
| 61 | |
| 62 | // request actual data |
| 63 | mSerial->writeByte( mCounter ); |
| 64 | |
| 65 | // clear accumulated contact messages in buffer |
| 66 | char b = '*'; |
| 67 | while( mSerial->getNumBytesAvailable() > -1 ) { |
| 68 | b = mSerial->readByte(); |
| 69 | console() << b << "_"; |
| 70 | } |
| 71 | |
| 72 | mSerial->flush(); |
| 73 | } |
| 74 | |
| 75 | void SerialCommunicationApp::mouseDown( MouseEvent event ) |
| 76 | { |