A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
io/MTSSerialFlowControl.cpp
- Committer:
- kranjan
- Date:
- 2014-01-04
- Revision:
- 141:571e0ef6c8dc
- Parent:
- 140:bf0c8288eb78
- Child:
- 142:3b40320f3f73
File content as of revision 141:571e0ef6c8dc:
/* Universal Socket Modem Interface Library * Copyright (c) 2013 Multi-Tech Systems * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "MTSSerialFlowControl.h" using namespace mts; MTSSerialFlowControl::MTSSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize) : MTSSerial(TXD, RXD, txBufSize, rxBufSize) , rxReadyFlag(false) , rts(RTS) , cts(CTS) { notifyStartSending(); highThreshold = MAX(rxBufSize - 10, rxBufSize * 0.85); lowThreshold = rxBufSize * 0.3; rxBuffer.attach(this, &MTSSerialFlowControl::notifyStartSending, lowThreshold, Vars::LESS); } MTSSerialFlowControl::~MTSSerialFlowControl() { } void MTSSerialFlowControl::rxClear() { MTSBufferedIO::rxClear(); notifyStartSending(); } void MTSSerialFlowControl::notifyStartSending() { if(!rxReadyFlag) { rts.write(0); rxReadyFlag = true; //printf("RTS LOW: READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); } } void MTSSerialFlowControl::notifyStopSending() { if(rxReadyFlag) { rts.write(1); rxReadyFlag = false; //printf("RTS HIGH: NOT-READY - RX[%d/%d]\r\n", rxBuffer.size(), rxBuffer.capacity()); } } void MTSSerialFlowControl::handleRead() { while (serial.readable()) { char byte = serial.getc(); if(rxBuffer.write(byte) != 1) { rts.write(1); rxReadyFlag = false; printf("[ERROR] Serial Rx Byte Dropped [%c][0x%02X]\r\n", byte, byte); if(byte == 0xFF) { serial.attach(NULL, Serial::RxIrq); serial.attach(this, &MTSSerialFlowControl::handleRead, Serial::RxIrq); printf("[INFO] Reset the RxIrq.\n\r"); } return; } if (rxBuffer.size() > highThreshold) { notifyStopSending(); } } } void MTSSerialFlowControl::handleWrite() { while(txBuffer.size() != 0) { if (serial.writeable() && cts.read() == 0) { char byte; if(txBuffer.read(byte) == 1) { serial.putc(byte); } } else { return; } } }