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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MTSSerial.cpp Source File

MTSSerial.cpp

00001 /* Universal Socket Modem Interface Library
00002 * Copyright (c) 2013 Multi-Tech Systems
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 */
00016 
00017 #include "MTSSerial.h"
00018 
00019 using namespace mts;
00020 
00021 MTSSerial::MTSSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize)
00022     : MTSBufferedIO(txBufferSize, rxBufferSize)
00023     , serial(TXD,RXD)
00024 {
00025     serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
00026     //serial.attach(this, &MTSSerial::handleWrite, Serial::TxIrq);
00027 }
00028 
00029 MTSSerial::~MTSSerial()
00030 {
00031 
00032 }
00033 
00034 void MTSSerial::baud(int baudrate)
00035 {
00036     serial.baud(baudrate);
00037 }
00038 
00039 void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits)
00040 {
00041     serial.format(bits, parity, stop_bits);
00042 }
00043 
00044 void MTSSerial::handleRead()
00045 {
00046     char byte = serial.getc();
00047     if(rxBuffer.write(byte) != 1) {
00048         printf("[ERROR] Serial Rx Byte Dropped [%c][0x%02X]\r\n", byte, byte);
00049     }
00050 }
00051 
00052 // Currently uses Non-Irq based blocking write calls
00053 void MTSSerial::handleWrite()
00054 {
00055     while(txBuffer.size() != 0) {
00056         if (serial.writeable()) {
00057             char byte;
00058             if(txBuffer.read(byte) == 1) {
00059                 serial.attach(NULL, Serial::RxIrq);
00060                 serial.putc(byte);
00061                 serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
00062             }
00063         } else {
00064             return;
00065         }
00066     }
00067 }
00068 
00069