ucomport.cpp
88 lines
| 1.5 KiB
| text/x-c
|
CppLexer
/ src / ucomport.cpp
|
r2 | #include "ucomport.h" | ||
UComPort::UComPort() | ||||
{ | ||||
this->p_portMutex = new QMutex(); | ||||
this->p_opened = false; | ||||
} | ||||
QString UComPort::portName() | ||||
{ | ||||
QMutexLocker locker(this->p_portMutex); | ||||
return p_PortName; | ||||
} | ||||
int UComPort::speed() | ||||
{ | ||||
QMutexLocker locker(this->p_portMutex); | ||||
return p_speed; | ||||
} | ||||
void UComPort::setPortName(const QString &name) | ||||
{ | ||||
QMutexLocker locker(this->p_portMutex); | ||||
this->p_PortName = name; | ||||
} | ||||
void UComPort::setSpeed(int speed) | ||||
{ | ||||
QMutexLocker locker(this->p_portMutex); | ||||
this->p_speed = speed; | ||||
} | ||||
bool UComPort::open() | ||||
{ | ||||
QMutexLocker locker(this->p_portMutex); | ||||
if(p_opened) | ||||
{ | ||||
rs232close(this->p_port); | ||||
this->p_opened = false; | ||||
} | ||||
this->p_port = rs232open((char*)p_PortName.toStdString().c_str()); | ||||
if(this->p_port!=badPortValue) | ||||
{ | ||||
rs232setup(this->p_port,8,this->p_speed,rs232parityNo,rs232OneStop); | ||||
p_opened = true; | ||||
} | ||||
|
r3 | return p_opened; | ||
} | ||||
bool UComPort::close() | ||||
{ | ||||
if(p_opened) | ||||
{ | ||||
p_opened = rs232close(this->p_port); | ||||
} | ||||
return !p_opened; | ||||
} | ||||
bool UComPort::opened() | ||||
{ | ||||
return p_opened; | ||||
|
r2 | } | ||
bool UComPort::open(const QString &name, int speed) | ||||
{ | ||||
setPortName(name); | ||||
setSpeed(speed); | ||||
return open(); | ||||
} | ||||
|
r3 | |||
int UComPort::availableBytes() | ||||
{ | ||||
if(p_opened) | ||||
{ | ||||
return rs232availablebytes(this->p_port); | ||||
} | ||||
return 0; | ||||
} | ||||
int UComPort::readBytes(char *data, int count) | ||||
{ | ||||
return rs232read(this->p_port,data,count); | ||||
} | ||||
int UComPort::writeBytes(char *data, int count) | ||||
{ | ||||
return rs232write(this->p_port,data,count); | ||||
} | ||||