#include "StdAfx.h" #include ".\h7serial.h" CH7Serial::CH7Serial(void) : CSlipSerial() { } CH7Serial::CH7Serial(char* port, int bitrate) : CSlipSerial(port, bitrate) { } CH7Serial::~CH7Serial(void) { } short CH7Serial::readTarget(unsigned char target) { serialLock.Lock(); short retval; unsigned char frame[1]; unsigned char hi_target = (target & 0x0F) << 4; frame[0] = hi_target | 0x00; writeFrame(frame,1); Sleep(50); //be nice and wait a little std::vector answer = readFrame(); if (answer.size() == 0) throw std::exception("No reply"); if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK throw std::exception("Request not acknowledged"); if ( (answer[0] & 0xF0) != hi_target) throw std::exception("Incorrect reply"); if (answer.size() == 2) retval = answer[1]; else if (answer.size() == 3) retval = (answer[1]<<8) | answer[2]; else retval = -1; Sleep(5); //forced delay before unlock our sync.object serialLock.Unlock(); return retval; } void CH7Serial::writeTarget(unsigned char target, unsigned char data) { serialLock.Lock(); unsigned char frame[2]; unsigned char hi_target = (target & 0x0F) << 4; frame[0] = hi_target | 0x01; frame[1] = data; writeFrame(frame,2); Sleep(50); //be nice and wait a little std::vector answer = readFrame(); if (answer.size() == 0) throw std::exception("No reply"); if ( (answer[0] & 0x0F) != 0x02) // tjek for ACK throw std::exception("Request not acknowledged"); if ( (answer[0] & 0xF0) != hi_target) throw std::exception("Incorrect reply"); serialLock.Unlock(); }