-
UID:145258
-
- 注册时间2013-08-10
- 最后登录2014-05-13
- 在线时间20小时
-
- 发帖13
- 搜Ta的帖子
- 精华0
- 金钱140
- 威望23
- 贡献值0
- 好评度13
-
访问TA的空间加好友用道具
|
window.open('http://www.qtcn.org/bbs/attachment/Mon_1402/18_145258_3786eae11825d0f.jpg?212');" style="max-width:700px;max-height:700px;" onload="if(is_ie6&&this.offsetWidth>700)this.width=700;" >
功能是实现数据的透传,例如将串口输入的数据通过WIFI或者3g上传到服务器202.116.65.245。 从串口到3G的功能是正常的,但是从串口到wifi就不行,每次都在命令行看到有数据进来,但服务器上却没有数据更新,直到点击“断开连接”,则会看到服务器上面的数据有更新。所以求各位大神指点迷津,是哪里出了问题。 另外,不管是串口到3G或者是串口到wifi,都存在一个奇怪的现象: 假如有ABCDEFGHIJKL这么多组数据,我们点击“开始连接”,假设当传进来ABCD的时候我们点击“断开连接”,然后点击“开始连接”,则在命令行可以看到后面进来的数据是这样子:EEFFGG。如果这时候再点击“断开连接”,再“开始连接”,则接下来的数据会是HHHIIIJJJKKKLLL.以此类推,每次断开连接,再重新连接那么进来的数据重复,并且重复次数会递增。相关的代码如下:mainwindow.cpp- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- init();// 初始化相关配置
- serialToTcp = new SerialToTcp();
- wifiToTcp = new WiFiToTcp();
- ethernetToTcp = new EthernetToTcp();
- gprsToTcp = new GprsToTcp();
- }
- //构造函数
- MainWindow::~MainWindow()
- {
- delete ui;
- delete inputButtonGroup;
- delete outputButtonGroup;
- }
- // 初始化相关配置
- void MainWindow::init() {
- // 将不同通信接口按钮加入到输入输出按钮组中
- inputButtonGroup = new QButtonGroup;
- //下面调用了QButtonGroup的void addButton ( QAbstractButton * button, int id )方法
- inputButtonGroup->addButton(ui->serialInputRadioButton, 1);
- inputButtonGroup->addButton(ui->wifiInputRadioButton, 2);
- inputButtonGroup->addButton(ui->ethernetInputRadioButton, 3);
- inputButtonGroup->addButton(ui->gprsInputRadioButton, 4);
- outputButtonGroup = new QButtonGroup;
- outputButtonGroup->addButton(ui->serialOutputRadioButton, 1);
- outputButtonGroup->addButton(ui->wifiOutputRadioButton, 2);
- outputButtonGroup->addButton(ui->ethernetOutputRadioButton, 3);
- outputButtonGroup->addButton(ui->gprsOutputRadioButton, 4);
- connect( inputButtonGroup, SIGNAL(buttonClicked (int)), this, SLOT(inputButtonJudge(int)) );
- connect( outputButtonGroup, SIGNAL(buttonClicked (int)), this, SLOT(outputButtonJudge(int)) );
- // 限制网络地址和端口格式
- QRegExp portExp("[0-9]{1,5}");
- //[0-9]表示从0到9,{1,5}表示出现1到5次???也就是输入一个数字不超过5位数???
- QRegExpValidator *validator = new QRegExpValidator();
- validator->setRegExp(portExp);
- ui->portInputLineEdit->setValidator(validator);
- ui->portOutputLineEdit->setValidator(validator);
- QRegExp regExp("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
- //意思大概是输入格式为***.***.***.***的ip地址
- QRegExpValidator *ipvalidator = new QRegExpValidator();//ip验证器
- ipvalidator->setRegExp(regExp);
- ui->addressInputLineEdit->setValidator(ipvalidator);
- ui->addressOutputLineEdit->setValidator(ipvalidator);
- }
- // 程序退出操作
- void MainWindow::on_exitButton_clicked()
- {
- int ret = QMessageBox::question(this, tr("退出信息"), tr("确定退出该程序?"), QMessageBox::Yes, QMessageBox::No);
- if(ret == QMessageBox::Yes) {
- close();
- }
- }
- // 使串口输入配置或者网络配置可用
- void MainWindow::inputButtonJudge(int buttonId) {
- if(buttonId == 1) {
- ui->serialInputFrame->setEnabled(true);// 设置串口输入配置可用
- ui->downloadFrame->setEnabled(false);// 设置网络配置不可用
- } else {
- ui->serialInputFrame->setEnabled(false);// 设置串口输入配置不可用
- ui->downloadFrame->setEnabled(true);// 设置网络配置可用
- }
- }
- // 使串口输出配置或者网络配置可用
- void MainWindow::outputButtonJudge(int buttonId) {
- if(buttonId == 1) {
- ui->serialOutputFrame->setEnabled(true);// 设置串口输出配置可用
- ui->uploadFrame->setEnabled(false);// 设置网络配置不可用
- } else {
- ui->serialOutputFrame->setEnabled(false);// 设置串口输出配置不可用
- ui->uploadFrame->setEnabled(true);// 设置网络配置可用
- }
- }
- // 开始连接配置
- void MainWindow::on_connectButton_clicked()
- {
- if(inputButtonGroup->checkedId() == -1) {
- QMessageBox::information(this, tr("提示信息"), tr("请选择通信接口输入方式!"), QMessageBox::Ok);
- return;
- }
- if(outputButtonGroup->checkedId() == -1) {
- QMessageBox::information(this, tr("提示信息"), tr("请选择通信接口输出方式!"), QMessageBox::Ok);
- return;
- }
- if(inputButtonGroup->checkedId() == outputButtonGroup->checkedId()) {
- QMessageBox::information(this, tr("提示信息"), tr("请选择两种不同通信接口输入输出方式!"), QMessageBox::Ok);
- return;
- }
- //上面调用了int QButtonGroup::checkedId () const方法,没有检查到button则返回-1,检查到button就返回相应的buttonId
- ui->connectButton->setEnabled(false);
- ui->disconnectButton->setEnabled(true);
- ui->mainFrame->setEnabled(false);
- QString server_ip = ui->addressInputLineEdit->text();
- QString client_ip = ui->addressOutputLineEdit->text();
- bool ok;
- int server_port = ui->portInputLineEdit->text().toInt(&ok, 10);
- int client_port = ui->portOutputLineEdit->text().toInt(&ok, 10);
- if(inputButtonGroup->checkedId() == 1) {
- qDebug()<<outputButtonGroup->checkedId();
- serialToTcp->serialToTcp(client_ip,
- client_port,
- ui->speedInputComboBox->currentIndex(),
- ui->databitInputComboBox->currentIndex(),
- ui->parityInputcomboBox->currentIndex(),
- ui->stopbitInputComboBox->currentIndex(),
- outputButtonGroup->checkedId());
- }
- else if(inputButtonGroup->checkedId() == 2){
- if(outputButtonGroup->checkedId() == 1) {
- wifiToTcp->wifiToSerial(server_ip,
- server_port,
- ui->speedOutputComboBox->currentIndex(),
- ui->databitOutputComboBox->currentIndex(),
- ui->parityOutputComboBox->currentIndex(),
- ui->stopbitOutputComboBox->currentIndex());
- }
- else {
- wifiToTcp->wifiToTcp(server_ip,
- server_port,
- client_ip,
- client_port,
- outputButtonGroup->checkedId());
- }
- }
- else if(inputButtonGroup->checkedId() == 3){
- if(outputButtonGroup->checkedId() == 1) {
- ethernetToTcp->ethernetToSerial(server_ip,
- server_port,
- ui->speedOutputComboBox->currentIndex(),
- ui->databitOutputComboBox->currentIndex(),
- ui->parityOutputComboBox->currentIndex(),
- ui->stopbitOutputComboBox->currentIndex());
- }
- else {
- ethernetToTcp->ethernetToTcp(server_ip, server_port, client_ip, client_port,
- outputButtonGroup->checkedId());
- }
- }
- else if(inputButtonGroup->checkedId() == 4){
- if(outputButtonGroup->checkedId() == 1) {
- gprsToTcp->gprsToSerial(server_ip, server_port,
- ui->speedOutputComboBox->currentIndex(),
- ui->databitOutputComboBox->currentIndex(),
- ui->parityOutputComboBox->currentIndex(),
- ui->stopbitOutputComboBox->currentIndex());
- } else {
- gprsToTcp->gprsToTcp(server_ip, server_port, client_ip, client_port,
- outputButtonGroup->checkedId());
- }
- }
- }
- // 关闭连接配置
- void MainWindow::on_disconnectButton_clicked()
- {
- ui->connectButton->setEnabled(true);
- ui->disconnectButton->setEnabled(false);
- ui->mainFrame->setEnabled(true);
- if(inputButtonGroup->checkedId() == 1) {
- serialToTcp->closeSerialToTcp();
- }
- else if(inputButtonGroup->checkedId() == 2) {
- wifiToTcp->stopTransfer();
- }
- else if(inputButtonGroup->checkedId() == 3){
- ethernetToTcp->stopTransfer();
- }
- else if(inputButtonGroup->checkedId() == 4){
- gprsToTcp->stopTransfer();
- }
- }
serialconfig.cpp- #include "serialconfig.h"
- SerialConfig::SerialConfig(QObject *parent) :
- QObject(parent)
- {
- timer = new QTimer(this);
- comm = new Posix_QextSerialPort("/dev/ttySAC0", QextSerialBase::Polling);// 打开串口设备
- connect(timer, SIGNAL(timeout()), this, SLOT(readReady()));
- }
- // 初始化串口配置
- void SerialConfig::init(int baud, int databits, int parity, int stopBits)
- {
- comm->open(QIODevice::ReadWrite);
- switch(baud) {
- case 0:
- comm->setBaudRate(BAUD115200);
- break;
- case 1:
- comm->setBaudRate(BAUD57600);
- break;
- case 2:
- comm->setBaudRate(BAUD38400);
- break;
- case 3:
- comm->setBaudRate(BAUD19200);
- break;
- case 4:
- comm->setBaudRate(BAUD14400);
- break;
- case 5:
- comm->setBaudRate(BAUD9600);
- break;
- case 6:
- comm->setBaudRate(BAUD4800);
- break;
- }
- switch (parity) {
- case 0:
- comm->setParity(PAR_NONE);
- break;
- case 1:
- comm->setParity(PAR_ODD);
- break;
- case 2:
- comm->setParity(PAR_EVEN);
- break;
- }
- switch (databits) {
- case 0:
- comm->setDataBits(DATA_8);
- break;
- case 1:
- comm->setDataBits(DATA_7);
- break;
- case 2:
- comm->setDataBits(DATA_6);
- break;
- case 3:
- comm->setDataBits(DATA_5);
- break;
- }
- switch (stopBits) {
- case 0:
- comm->setStopBits(STOP_1);
- break;
- case 1:
- comm->setStopBits(STOP_2);
- break;
- }
- comm->setFlowControl(FLOW_XONXOFF);
- comm->setTimeout(10);
- }
- // 读取串口数据
- void SerialConfig::readData(int baud, int databits, int parity, int stopBits)
- {
- init(baud, databits, parity, stopBits);
- timer->start(100);
- }
- // 通过串口发送数据
- void SerialConfig::sendData(QByteArray buff)
- {
- if(buff.length() > 0) {
- int sendBytes = comm->write(buff, buff.length());
- emit updataSendDataBytes(sendBytes);
- }
- }
- // 关闭串口
- void SerialConfig::closeComm()
- {
- timer->stop();
- comm->close();
- }
- // 当串口接收到数据时,调用该方法
- void SerialConfig::readReady()
- {
- while(comm->bytesAvailable() > 0) {
- QByteArray receivedData = comm->readAll();
- emit updataReceivedData(receivedData);
- }
- }
serialtotcp.cpp- #include "serialtotcp.h"
- SerialToTcp::SerialToTcp(QObject *parent) :
- QObject(parent)
- {
- serialConfig = new SerialConfig();
- serverAddress = new QHostAddress();
- tcpSocket = new QTcpSocket(this);
- networkManager = new QNetworkAccessManager(this);
- }
- void SerialToTcp::serialToTcp(QString server_ip, int server_port, int baud, int databits, int parity, int stopbits, int type) {
- this->server_ip = server_ip;
- this->server_port = server_port;
- this->type = type;
- if(type == 3) {
- // 串口到以太网数据传输
- system("route add default dev eth0");// 将以太网接口作为默认网关接口
- } else if(type == 2) {
- // 串口到WiFi数据传输
- system("route add default dev wlan0");// 将wifi接口作为默认网关接口
- } else if(type == 4) {
- // 串口到3G数据传输
- system("route add default dev ppp0");// 将3G接口作为默认网关接口
- }
- connect(serialConfig, SIGNAL(updataReceivedData(QByteArray)), this, SLOT(slotDataFromSerial(QByteArray)));
- serialConfig->readData(baud, databits, parity, stopbits);
- }
- // 通过串口获取数据后,将数据通过其他接口发送数据
- void SerialToTcp::slotDataFromSerial(QByteArray msg)
- {
- this->msg = msg;
- qDebug()<<msg;
- if(server_port == 80) {
- QString url = "http://" + server_ip;
- url += "/sysu/zigbeeconnect";
- QNetworkRequest request;
- request.setUrl(url);
- request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html");// 设置请求内容为文本内容
- request.setHeader(QNetworkRequest::ContentLengthHeader, msg.length());
- networkManager->post(request, msg);
- } else {
- // 使用其他端口时使用socket发送数据
- serverAddress->setAddress(server_ip);
- connect(tcpSocket, SIGNAL(connected()), this, SLOT(slotConnectedByServer()));
- tcpSocket->connectToHost(*serverAddress, server_port);
- }
- }
- // 使用socket发送数据
- void SerialToTcp::slotConnectedByServer()
- {
- tcpSocket->write(msg);
- }
- // 关闭相关资源
- void SerialToTcp::closeSerialToTcp() {
- serialConfig->closeComm();
- if(type == 2) {
- system("route del default dev wlan0");// 删除默认以太网网关接口
- } else if(type == 3) {
- system("route del default dev eth0");// 删除默认wifi网关接口
- } else if(type == 4) {
- system("route del default dev ppp0");// 删除默认3G网关接口
- }
- }
|