diff --git a/vincecontrol.cpp b/vincecontrol.cpp index 1d4af62..39ee4ab 100644 --- a/vincecontrol.cpp +++ b/vincecontrol.cpp @@ -1,16 +1,77 @@ #include "vincecontrol.h" - -VinceControl::VinceControl() +#include"QSerialPortInfo" +VinceControl::VinceControl(ProTools proto) { - + protools = proto; + IsMotorInit = false; + RS485ID = "0"; } VinceControl::~VinceControl() { - + serial->close(); + IsMotorInit = false; } -void VinceControl::connect(QString comname, QString bandrate) +bool VinceControl::serialconnect(QString comname, QString bandrate) { + QSerialPortInfo info; + QList infos = QSerialPortInfo::availablePorts(); + int i = 0; + foreach(info, infos) { + if (info.portName() == comname) break; + i++; + } + if (i != infos.size()) + { + serial->close(); + serial->setPort(info); + bool aa = serial->open(QIODevice::ReadWrite); + qint32 b = bandrate.toInt(); + serial->setBaudRate(b); + IsMotorInit = true; + } + else + { + serial->close(); + IsMotorInit = false; + } +} + +void VinceControl::SetRS485ID(QString id) +{ + RS485ID = id; +} + +void VinceControl::EnableMotro() +{ + QString str = "ena\n"; + SendCommandtoSerial(str); + + } + +void VinceControl::DisableMotro() +{ + QString str = "off\n"; + SendCommandtoSerial(str); +} + +void VinceControl::SendCommandtoSerial(QString str) +{ + if (protools == RS232) + { + QByteArray buf; + buf.append(str); + serial->write(buf); + } + else if(protools==RS485) + { + QString str2 = RS485ID + " " + str; + QByteArray buf; + buf.append(str2); + serial->write(buf); + + } +} diff --git a/vincecontrol.h b/vincecontrol.h index e676ff5..a0bffd1 100644 --- a/vincecontrol.h +++ b/vincecontrol.h @@ -1,17 +1,120 @@ +/****************************************************** +* 文件名 : vincecontrol.h +* 类名 : +* 作用 :马达控制程序 +* 作者 : xin +* 邮箱 : renlixin@iris-rs.cn +* 日期 : 2020-8-5 +******************************************************** +* * +* _ooOoo_ * +* o8888888o * +* 88" . "88 * +* (| -_- |) * +* O\ = /O * +* ____/`---'\____ * +* .' \\| |// `. * +* / \\||| : |||// \ * +* / _||||| -:- |||||- \ * +* | | \\\ - /// | | * +* | \_| ''\---/'' | | * +* \ .-\__ `-` ___/-. / * +* ___`. .' /--.--\ `. . __ * +* ."" '< `.___\_<|>_/___.' >'"". * +* | | : `- \`.;`\ _ /`;.`/ - ` : | | * +* \ \ `-. \_ __\ /__ _/ .-` / / * +* ======`-.____`-.___\_____/___.-`____.-'====== * +* `=---=' * +* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * +* 佛祖保佑 长发永存 * +* * +********************************************************/ + + + #ifndef VINCECONTROL_H #define VINCECONTROL_H #include "vincecontrol_global.h" #include "QString" - +#include +enum ProTools +{ + RS232 = 0, +RS485 = 1 +}; +/******************************************************************** +* 描述:马达控制程序 +* 使用步骤 +* 1)初始话 需要告诉通讯协议(Vince规定的): +* 2)如果是485 请设置id 默认为0 +* 3) serialconnect 建立链接 +* 4)使用 +* 2020-8-5:立新 +*******************************************************************/ class VINCECONTROL_EXPORT VinceControl { public: - VinceControl(); + //************************************ + // Method: VinceControl + // FullName: VinceControl::VinceControl + // Access: public + // Returns: + // Qualifier: + // Parameter: ProTools 通讯协议 RS485 或者 RS232 + //************************************ + VinceControl(ProTools proto); ~VinceControl(); - void connect(QString comname,QString bandrate); - + //************************************ + // Method: serialconnect + // FullName: VinceControl::serialconnect + // Access: public + // Returns: bool + // Qualifier: + // Parameter: QString comname 串口名称 + // Parameter: QString bandrate 串口波特率 + //************************************ + bool serialconnect(QString comname,QString bandrate); + //************************************ + // Method: SetRS485ID + // FullName: VinceControl::SetRS485ID + // Access: public + // Returns: void + // Qualifier: + // Parameter: QString id RS485 id + //************************************ + void SetRS485ID(QString id); + //************************************ + // Method: 电机使能 + // FullName: VinceControl::EnableMotro + // Access: public + // Returns: void + // Qualifier: + //************************************ + void EnableMotro(); + //************************************ + // Method: 取消使能 + // FullName: VinceControl::DisableMotro + // Access: public + // Returns: void + // Qualifier: + //************************************ + void DisableMotro(); + //************************************ + // Method: SendCommandtoSerial + // FullName: VinceControl::SendCommandtoSerial + // Access: public + // Returns: void + // Qualifier: + // Parameter: QString str 命令 不包含id + //************************************ + void SendCommandtoSerial(QString str); private: + QSerialPort *serial; + + bool IsMotorInit; + ProTools protools; + QString RS485ID; };