// Description: Sets the data rate in bits per second for serial data transmission. This constructor should be used if you are using a non-standard crystal. // Syntax: MultiSerialInstance.begin(speed, crystalHz) // Parameter: speed - communications speed in bits per second // Parameter: crystalHz - Speed of the crystal on the board, in Hz // Returns: nothing void MultiSerial::begin(unsigned long baud, unsigned long crystalHz) { // join i2c bus (no address as we want to be the master) Wire.begin(); // if this is the first time the shield is used, reset the UART so // that we can start off in a known state // FIXME: will this work for >1 shield though? if(needsReset==1) { msWriteRegister(IOControl, 0x8); needsReset=0; } delay(25); // switch to the special register bank msWriteRegister(LCR, 128); // set the baud rate unsigned short baudDivisor; baudDivisor = (crystalHz + baud * 8L) / (baud * 16L); msWriteRegister(DLL, baudDivisor & 0xFF); msWriteRegister(DLH, baudDivisor >> 8); // switch back to the normal register bank msWriteRegister(LCR, 0); // set the word size to 8 bits setWordSize(8); // enable the TX/RX fifos msWriteRegister(FCR, 7); }
SerialPort::SerialPort(const string device) : m_fd(0), m_baudRate(0), m_wordSize(0), m_asyncTimeout(DEFAULT_ASYNC_TIMEOUT) { if((m_fd = ::open(device.c_str(), O_RDWR | O_NOCTTY)) < 0) throw SysException(); ::bzero(&m_term, sizeof(m_term)); ::tcgetattr(m_fd, &m_termOld); setBaudRate(DEFAULT_BAUD_RATE); setWordSize(DEFAULT_WORD_SIZE); setParity(DEFAULT_PARITY); m_term.c_cflag |= CLOCAL | CREAD; m_term.c_iflag = IGNPAR; m_term.c_oflag = OPOST; m_term.c_lflag = 0; m_term.c_cc[VTIME] = 0; m_term.c_cc[VMIN] = 1; setAttr(); if(::fcntl(m_fd, F_SETOWN, getpid()) == -1) throw SysException(); }
bool GisBinFile::readCompressdRow(int row, char* charValues) { char nBytes; if(file_.get(nBytes)) { if((int) nBytes == 4 || (int) nBytes == 8) { setWordSize((int) nBytes); int totBytes = nCols() * dataSize_; gotoPos(row * nBytes + 1L); int rowSize; if((int) nBytes == 4) { int rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr - 1; } else { off_t rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr; } char compressFlag; file_.get(compressFlag); // if ( rowSize < totBytes ) if((compressFlag == 0x01) && ((rowSize - 1) < totBytes)) { unsigned int i = 0; while (i < totBytes) { char charCount; file_.get(charCount); char charValue; file_.get(charValue); unsigned int j = 0; for(j = i; j < i + (unsigned char) charCount; j++) charValues[j] = charValue; i += (unsigned char) charCount; } } else this->readNChar(charValues, totBytes); return true; } } return false; }
bool GisBinFile::readCompressdRow(int row, float* floatValues) { char nBytes; if(file_.get(nBytes)) { if((int) nBytes == 4 || (int) nBytes == 8) { setWordSize((int) nBytes); int totBytes = nCols() * dataSize_; unsigned char* expandedValues = new unsigned char[totBytes]; gotoPos(row * nBytes + 1L); int rowSize; if((int) nBytes == 4) { int rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr - 1; } else { off_t rowPtr, nextRowPtr; read(&rowPtr); read(&nextRowPtr); gotoPos(rowPtr); rowSize = nextRowPtr - rowPtr - 1; } char compressFlag; file_.get(compressFlag); if(compressFlag == 0x31) { unsigned char* charValues = new unsigned char[rowSize]; this->readNChar((char *) charValues, rowSize); if(!GisUncompress(rowSize, charValues, totBytes, expandedValues)) { delete[] charValues; return false; } delete[] charValues; } else this->readNChar((char *) expandedValues, totBytes); int i = 0; int j = 0; while (i < totBytes) { char fvalchar[4]; for(int k = 0; k < 4; k++) { if(swappMode_) fvalchar[3 - k] = expandedValues[i++]; else fvalchar[k] = expandedValues[i++]; } floatValues[j++] = *((float*) &fvalchar[0]); } delete[] expandedValues; return true; } } return false; }