基于表具协议的报文生成工具——插件

cmdcreater

cmdcreater.pro

1 SOURCES += \
2     cmdcreator.cpp \
3     main.cpp
4 
5 HEADERS += \
6     cmdcreator.h \
7     myplugininterface.h

cmdcreater.h

 1 #ifndef CMDCREATOR_H
 2 #define CMDCREATOR_H
 3 
 4 #include <QtGui/QWidget>
 5 #include "myplugininterface.h"
 6 
 7 class QComboBox;
 8 class QLineEdit;
 9 class QPushButton;
10 class QLabel;
11 
12 class CmdCreator : public QWidget
13 {
14     Q_OBJECT
15 
16 public:
17     CmdCreator();
18     ~CmdCreator(){}
19 
20 private slots:
21     void printCmd();
22     void setCurrentPlugin(QString strPluginName);
23 
24 private:
25     void creatGUI();
26     bool loadPlugin(QString pluginName);
27     void pluginContext(QString strPluginName);
28     void addCmdType();
29 
30     QComboBox *m_ptrCmdTypeComboBox;
31     QLineEdit *m_ptrNoLineEdit;
32     QLineEdit *m_ptrStartReg;
33     QLineEdit *m_ptrRegCounts;
34     QLineEdit *m_ptrDILineEdit;
35     QLineEdit *m_ptrPrintOutLineEdit;
36     QTextBrowser *m_ptrNoteTextBrowser;
37     QPushButton *m_ptrOkPushButton;
38     MyPluginInterface * m_ptrMyPluginInterface;
39 };
40 
41 #endif // CMDCREATOR_H

myplugininterface.h

 1 #ifndef MYPLUGIN_H
 2 #define MYPLUGIN_H
 3 #include <QtCore>
 4 #include <QtGui/QLineEdit>
 5 #include <QtGui/QLabel>
 6 #include <QTextBrowser>
 7 class MyPluginInterface
 8 {
 9 public:
10 //  MyPluginInterface(){}
11     virtual ~MyPluginInterface(){}
12     virtual void setNoteTextBrowser(QTextBrowser *ptrNoteTextBrowser) = 0;
13     virtual void setLineEdit(QLineEdit *ptrNoLineEdit, QLineEdit *ptrStartReg, QLineEdit *ptrRegCounts, QLineEdit *ptrDILineEdit) = 0;
14     virtual QString creatCmd(QString strNum, QString strStartReg, QString strRegCounts, QString strDI) = 0;
15 };
16 
17 Q_DECLARE_INTERFACE(MyPluginInterface,"com.reatgreen.MyPligin.MyPluginInterface/1.0");
18 #endif

cmdcreator.cpp

  1 #include <QtGui/QComboBox>
  2 #include <QtGui/QLineEdit>
  3 #include <QtGui/QPushButton>
  4 #include <QtGui/QLabel>
  5 #include <QtGui/QGridLayout>
  6 #include <QtGui/QMessageBox>
  7 #include <QDebug>
  8 #include <QStringList>
  9 
 10 #include "cmdcreator.h"
 11 
 12 CmdCreator::CmdCreator()
 13 {
 14     creatGUI();
 15 
 16     setCurrentPlugin(m_ptrCmdTypeComboBox -> currentText());
 17 
 18     connect(m_ptrCmdTypeComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(setCurrentPlugin(QString)));
 19     connect(m_ptrOkPushButton, SIGNAL(clicked()), this, SLOT(printCmd()));
 20 
 21 }
 22 
 23 void CmdCreator::printCmd()
 24 {
 25     QString strNum = m_ptrNoLineEdit->text();
 26     QString strStartReg = m_ptrStartReg->text();
 27     QString strRegCounts = m_ptrRegCounts->text();
 28     QString strDI = m_ptrDILineEdit->text();
 29     QString text = m_ptrMyPluginInterface->creatCmd(strNum, strStartReg, strRegCounts, strDI);
 30     m_ptrPrintOutLineEdit -> setText(text);
 31     qDebug() << text;
 32 }
 33 
 34 void CmdCreator::setCurrentPlugin(QString strPluginName)
 35 {
 36     pluginContext(strPluginName);
 37 }
 38 
 39 void CmdCreator::creatGUI()
 40 {
 41     m_ptrCmdTypeComboBox = new QComboBox;
 42     addCmdType();
 43 
 44     m_ptrNoLineEdit = new QLineEdit;
 45     m_ptrStartReg = new QLineEdit;
 46     m_ptrRegCounts = new QLineEdit;
 47     m_ptrDILineEdit = new QLineEdit;
 48     m_ptrPrintOutLineEdit = new QLineEdit;
 49     m_ptrPrintOutLineEdit -> setReadOnly(true);
 50     m_ptrNoteTextBrowser = new QTextBrowser;
 51     m_ptrNoteTextBrowser -> setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
 52     m_ptrOkPushButton = new QPushButton(tr("确定"));
 53 
 54     QGridLayout *ptrMainLayout = new QGridLayout;
 55     ptrMainLayout -> addWidget(new QLabel(tr("命令类型:")), 0, 0);
 56     ptrMainLayout -> addWidget(m_ptrCmdTypeComboBox, 0, 1);
 57     ptrMainLayout -> addWidget(new QLabel(tr("表    号:")), 1, 0);
 58     ptrMainLayout -> addWidget(m_ptrNoLineEdit, 1, 1);
 59     ptrMainLayout -> addWidget(new QLabel(tr("寄存器起始地址:")), 2, 0);
 60     ptrMainLayout -> addWidget(m_ptrStartReg, 2, 1);
 61     ptrMainLayout -> addWidget(new QLabel(tr("寄存器个数:")), 3, 0);
 62     ptrMainLayout -> addWidget(m_ptrRegCounts, 3, 1);
 63     ptrMainLayout -> addWidget(new QLabel(tr("数据标识:")), 4, 0);
 64     ptrMainLayout -> addWidget(m_ptrDILineEdit, 4, 1);
 65     ptrMainLayout -> addWidget(m_ptrOkPushButton, 5, 1, Qt::AlignRight);
 66     ptrMainLayout -> addWidget(new QLabel(tr("输    出:")), 6, 0);
 67     ptrMainLayout -> addWidget(m_ptrPrintOutLineEdit, 7, 0, 1, 2);
 68     ptrMainLayout -> addWidget(new QLabel(tr("提    示:")), 8, 0);
 69     ptrMainLayout -> addWidget(m_ptrNoteTextBrowser, 9, 0, 1, 2);
 70 
 71     setLayout(ptrMainLayout);
 72     setFixedSize(QSize(500,500));
 73     setWindowTitle(tr("命令生成器"));
 74 }
 75 
 76 bool CmdCreator::loadPlugin(QString pluginName)
 77 {
 78     QDir pluginsDir(qApp->applicationDirPath());
 79     pluginsDir.cd("plugins");
 80     QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(pluginName));
 81     QObject *plugin = pluginLoader.instance();
 82     if (plugin)
 83     {
 84         m_ptrMyPluginInterface = qobject_cast<MyPluginInterface *>(plugin);
 85         if (m_ptrMyPluginInterface)
 86         {
 87             m_ptrMyPluginInterface->setLineEdit(m_ptrNoLineEdit, m_ptrStartReg, m_ptrRegCounts, m_ptrDILineEdit);
 88             m_ptrMyPluginInterface->setNoteTextBrowser(m_ptrNoteTextBrowser);
 89             return true;
 90         }
 91     }
 92     return false;
 93 }
 94 void CmdCreator::pluginContext(QString strPluginName)
 95 {
 96     if (!loadPlugin(strPluginName.append(".dll")))
 97     {
 98         QMessageBox::information(this, tr("错误"), tr("无法载入该插件"));
 99         m_ptrNoLineEdit->setEnabled(false);
100         m_ptrOkPushButton->setEnabled(false);
101     }
102 }
103 
104 void CmdCreator::addCmdType()
105 {
106     QStringList strlItems, strlNameFilters;
107 
108     QDir pluginsDir(qApp->applicationDirPath());
109     pluginsDir.cd("plugins");
110     strlNameFilters << "*.dll";
111     pluginsDir.setNameFilters(strlNameFilters);
112     strlItems = pluginsDir.entryList(pluginsDir.nameFilters(), QDir::Files);
113     for(int i = 0; i < strlItems.size(); ++i)
114         strlItems[i].remove(".dll");
115     m_ptrCmdTypeComboBox -> addItems(strlItems);
116 }

main.cpp

 1 #include <QApplication>
 2 #include "cmdcreator.h"
 3 #include <Qtcore/QTextCodec>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication app(argc, argv);
 8     QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
 9     CmdCreator *w = new CmdCreator;
10     w -> show();
11     app.exec();
12 }

DLT645_07_plugin

DLT645_07_plugin.pro

 1 TEMPLATE = lib
 2 TARGET = $$qtLibraryTarget(DLT645规约_2007版协议读表插件)
 3 HEADERS += \
 4     DLT645_07_plugin.h \
 5     myplugininterface.h \
 6     utils.h
 7 
 8 SOURCES += \
 9     DLT645_07_plugin.cpp \
10     utils.cpp

DLT645_07_plugin.h

 1 #ifndef DLT645_07_PLUGIN_H
 2 #define DLT645_07_PLUGIN_H
 3 #include "myplugininterface.h"
 4 
 5 class DLT645_07_Plugin : public QObject, MyPluginInterface
 6 {
 7     Q_OBJECT
 8     Q_INTERFACES(MyPluginInterface)
 9 
10 public:
11    DLT645_07_Plugin(){}
12     void setNoteTextBrowser(QTextBrowser *ptrNoteTextBrowser);
13     void setLineEdit(QLineEdit *ptrNoLineEdit, QLineEdit *ptrStartRegLineEdit,
14                      QLineEdit *ptrRegCountsLineEdit, QLineEdit *ptrDILineEdit);
15     QString creatCmd(QString strNum, QString strStartReg,
16                      QString strRegCounts, QString strDI);
17 };
18 #endif // DLT645_07_PLUGIN_H

myplugininterface.h

 1 #ifndef MYPLUGININTERFACE_H
 2 #define MYPLUGININTERFACE_H
 3 #include <QtCore>
 4 #include <QtGui/QLineEdit>
 5 #include <QtGui/QLabel>
 6 #include <QTextBrowser>
 7 class MyPluginInterface
 8 {
 9 public:
10 //        MyPluginInterface(){}
11     virtual ~MyPluginInterface(){}
12     virtual void setNoteTextBrowser(QTextBrowser *ptrNoteTextBrowser) = 0;
13     virtual void setLineEdit(QLineEdit *ptrNoLineEdit, QLineEdit *ptrStartRegLineEdit, QLineEdit *ptrRegCountsLineEdit, QLineEdit *ptrDILineEdit) = 0;
14     virtual QString creatCmd(QString strNum, QString strStartReg, QString strRegCounts, QString strDI) = 0;
15 };
16 
17 Q_DECLARE_INTERFACE(MyPluginInterface,"com.reatgreen.MyPligin.MyPluginInterface/1.0");
18 #endif // MYPLUGININTERFACE_H

utils.h

1 #ifndef UTILS_H
2 #define UTILS_H
3 #include <QtCore>
4 
5 extern quint16 getCS(quint8 *buffer, int buffer_length);
6 extern bool convertStr2BCD(QString strSrc, quint8 * arrBcd, int nLen);
7 extern bool reverseArray(quint8 * arrData, int nLen);
8 #endif // UTILS_H

DLT645_07_plugin.cpp

  1 #include "DLT645_07_plugin.h"
  2 #include "utils.h"
  3 #include <QtCore/QString>
  4 #include <QRegExpValidator>
  5 void DLT645_07_Plugin::setLineEdit(QLineEdit *ptrNoLineEdit, QLineEdit *ptrStartRegLineEdit,
  6                                QLineEdit *ptrRegCountsLineEdit, QLineEdit *ptrDILineEdit)
  7 {
  8     ptrNoLineEdit->setEnabled(true);
  9     ptrStartRegLineEdit->setEnabled(false);
 10     ptrRegCountsLineEdit->setEnabled(false);
 11     ptrDILineEdit->setEnabled(true);
 12     QRegExp regExpNo("[0-9]{0,12}|[a-aA-A]{12}");
 13     ptrNoLineEdit -> setValidator(new QRegExpValidator(regExpNo,this));
 14     QRegExp regExpDI("[0-9a-fA-F]{8}");
 15     ptrDILineEdit -> setValidator(new QRegExpValidator(regExpDI,this));
 16 }
 17 
 18 void DLT645_07_Plugin::setNoteTextBrowser(QTextBrowser *ptrNoteTextBrowser)
 19 {
 20     QString text = QString(tr("  这是DLT645规约_2007版协议的读表插件,需要分别输入表号、数据标识。\n"));
 21     text.append(tr("\n  表号:参考表具铭牌上的数字。请输入0~12位0~9的数字,或12位‘a’或12位‘A’,默认为12位'0'。示例:12345\n"));
 22     text.append(tr("\n  数据标识DI3~DI0:需要填写8位0~9或a~f或A~F的数字,根据表具使用手册来填写。示例:A相电压:02010100\n"));
 23     text.append(tr("\n  数据标识参考信息:\n \t正相有功总电能:\t00010000\n \tA相正相有功总电能:\t00150000\n \tB相正相有功总电能:\t00290000\n \tC相正相有功总电能:\t003D0000\n"));
 24     text.append(tr("\tA相电压:\t\t02010100\n \tB相电压:\t\t02010200\n \tC相电压:\t\t02010300\n \t电压数据块:\t\t0201FF00\n"));
 25     text.append(tr("\tA相电流:\t\t02020100\n \tB相电流:\t\t02020200\n \tC相电流:\t\t02020300\n \t电流数据块:\t\t0202FF00\n"));
 26     text.append(tr("\t总功率因数:\t\t02060000\n \tA相功率因数:\t\t02060100\n \tB相功率因数:\t\t02060200\n \tC相功率因数:\t\t02060300\n \t功率因数数据块:\t0206FF00\n"));
 27 
 28     ptrNoteTextBrowser -> setText(text);
 29 }
 30 
 31 QString DLT645_07_Plugin::creatCmd(QString strNum, QString strStartReg,
 32                                QString strRegCounts, QString strDI)
 33 {
 34     Q_UNUSED(strStartReg);
 35     Q_UNUSED(strRegCounts);
 36 
 37     quint8 arrBuff[16];
 38     bool ok;
 39     arrBuff[0] = 0x68;
 40 
 41     for(int i = 1;i <= 6;i++)
 42     {
 43         arrBuff[i]=0x00;
 44     }
 45     quint8 arrBcdNum[6];
 46     ok = convertStr2BCD(strNum,arrBcdNum,6);
 47     if (ok == true)
 48     {
 49         bool bok = reverseArray(arrBcdNum,6);
 50         if (bok == true)
 51         {
 52             for(int j = 1;j <= 6;j++)
 53             {
 54                 arrBuff[j]=arrBcdNum[j-1];
 55             }
 56         }
 57     }
 58     arrBuff[7] = 0x68;
 59     arrBuff[8] = 0x11;
 60     arrBuff[9] = 0x04;
 61 
 62     for(int i = 10;i <= 13;i++)
 63     {
 64         arrBuff[i]=0x33;
 65     }
 66 
 67     quint8 arrBcdDI[4];
 68     if(strDI.count()!=8)
 69     {
 70         return " illegal data identification ";
 71     }
 72     ok = convertStr2BCD(strDI,arrBcdDI,4);
 73     if (ok == true)
 74     {
 75         bool bok = reverseArray(arrBcdDI,4);
 76         if (bok == true)
 77         {
 78             static int a = 0;
 79             for(int j = 10;j <= 13;j++)
 80             {
 81 
 82                 arrBuff[j]=arrBcdDI[a]+0x33;
 83                 ++a;
 84             }
 85         }
 86     }
 87 
 88     arrBuff[14] = getCS(arrBuff, 14);
 89     arrBuff[15] = 0x16;
 90 
 91     QString strCmd;
 92     for(int i = 0; i < 16; ++i)
 93     {
 94         strCmd += QString("%1").arg(arrBuff[i], 2, 16, QChar('0')).toUpper() + " ";
 95     }
 96 
 97     return strCmd;
 98 
 99 }
100 
101 Q_EXPORT_PLUGIN2(DLT645_07, DLT645_07_Plugin);

utils.cpp

 1 #include "utils.h"
 2 quint16 getCS(quint8 *buffer, int buffer_length)
 3 {
 4         int sum=0;
 5         while (buffer_length--)
 6         {
 7             sum += *buffer++;
 8             if(sum>=256)
 9             {
10                  sum=sum-256;
11             }/* calculate the CS  */
12         }
13         bool ok;
14         quint16 CS = (QString::number(sum, 16).toUpper()).toUInt(&ok, 16);
15         return (CS);
16 }
17 
18 bool convertStr2BCD(QString strSrc, quint8 * arrBcd, int nLen)
19 {
20         int nIndex = strSrc.length()-2;
21         int nTmpLen = 2;
22         bool bOk = false;
23         nLen--;
24 
25         do
26         {
27                 if(-1==nIndex)
28                 {
29                         nIndex = 0;
30                         nTmpLen = 1;
31                 }else if(-1>nIndex)
32                 {
33                         return false;
34                 }
35 
36                 *(arrBcd+nLen) = strSrc.mid(nIndex, nTmpLen).toUInt(&bOk, 16);
37 
38                 nIndex -= 2;
39                 nLen--;
40 
41         }while(-2!=nIndex);
42 
43         if(nLen>=0)
44         {
45                 memset(arrBcd, 0, nLen+1);
46         }
47 
48         return true;
49 }
50 
51 bool reverseArray(quint8 * arrData, int nLen)
52 {
53         for(int i=0; i<nLen/2; i++)
54         {
55                 quint8 nTemp = arrData[i];
56                 arrData[i] = arrData[nLen-i-1];
57                 arrData[nLen-i-1] = nTemp;
58         }
59 
60         return true;
61 }
posted @ 2012-04-25 13:13  雨中枫叶  阅读(713)  评论(0编辑  收藏  举报