/******************************************************************** * write_byte * Writes one byte to the COM * * Inputs: serdevice - opened file handle * byte - byte to write * * Returns: nothing * ********************************************************************/ int write_byte(WEATHERSTATION ws, int byte) { int status; int i; char str[20]; sprintf(str,"Writing byte %i",byte); print_log(3,str); for (i = 0; i < 8; i++) { write_bit(ws, byte & 0x80); byte <<= 1; byte &= 0xff; } set_RTS(ws,0); nanodelay(); status = get_CTS(ws); //TODO: checking value of status, error routine nanodelay(); set_DTR(ws,0); nanodelay(); set_DTR(ws,1); nanodelay(); if (status) return 1; else return 0; }
/** * Write one byte. * @param value Value to write. * @param verify Whether the write has to be verified (default: true). * @return Whether the write was successful. */ bool SerialInterface::write_byte(byte value, bool verify) { //clog(trace) << "Send byte 0x" << std::hex << value << std::endl; for (size_t i = 0; i < 8; i++) { write_bit((value & 0x80) > 0); value <<= 1; } set_RTS(false); nanodelay(); bool status = true; if (verify) { status = get_CTS(); //TODO: checking value of status, error routine // TODO: exceptions? nanodelay(); set_DTR(false); nanodelay(); set_DTR(true); nanodelay(); } return status; }
/** * Read one bit. * @return Bit read. */ byte SerialInterface::read_bit() { //clog(trace) << "Read bit ..." << std::endl; set_DTR(false); nanodelay(); bool status = get_CTS(); nanodelay(); set_DTR(true); nanodelay(); //clog(trace) << "Bit = " << (status ? "0" : "1") << std::endl; return (byte)(status ? 0 : 1); }
int read_bit(WEATHERSTATION ws) { int bit_value; char str[20]; set_DTR(ws,0); nanodelay(); bit_value = get_CTS(ws); nanodelay(); set_DTR(ws,1); nanodelay(); sprintf(str,"Read bit %i",!bit_value); print_log(4,str); return !bit_value; }
/** * Write an arbitrary amount of data. * @param location Location to write to. * @param data Data to write. */ bool SerialInterface::write_data(address location, const std::vector<byte> &data) { start_sequence(); if (!request(location)) return false; for (size_t i = 0; i < data.size(); i++) if (!write_byte(data[i])) return false; end_command(); start_sequence(); for (size_t i = 0; i < 3; i++) send_command(0xA0, false); set_DTR(false); nanodelay(); bool result = get_CTS(); set_DTR(true); nanodelay(); return result; }