modbus-rtu qt4-serialport1------ xp as host

sourceforge有一个qt下的modbus开源项目
http://sourceforge.net/projects/qmodbus/
http://sourceforge.net/search/?q=qmodbus




qt4串口编程基本见
http://www.yafeilinux.com/?p=820
使用第3方的类,
windows添加以下4个文件
qextserialbase.cpp
qextserialbase.h
win_qextserialport.cpp
win_qextserialport.h

linux下添加以下4个文件
qextserialbase.cpp
qextserialbase.h
posix_qextserialport.cpp
posix_qextserialport.h



1.使用事件驱动QextSerialBase::EventDriven,当串口有数据时Win_QextSerialPort自动发送readyRead()信号,执行readMyCom()槽函数
    QString portName="com3";
    myCom = new Win_QextSerialPort(portName,QextSerialBase::EventDriven);
    //定义串口对象,并传递参数,在构造函数里对其进行初始化
    if(myCom->open(QIODevice::ReadWrite)){
        QMessageBox::information(this, tr("打开成功"), tr("已成功打开串口 ") + portName, QMessageBox::Ok);
    }else{
        QMessageBox::critical(this, tr("打开失败"), tr("未能打开串口 ") + portName + tr("\n该串口设备不存在或已被占用"), QMessageBox::Ok);
        return;
    }
    //以可读写方式打开串口
    myCom->setBaudRate(BAUD9600);
    myCom->setDataBits(DATA_8);
    myCom->setParity(PAR_EVEN);
    myCom->setStopBits(STOP_1);
    myCom->setFlowControl(FLOW_OFF); //设置数据流控制,我们使用无数据流控制的默认设置
    myCom->setTimeout(500); //设置延时
   connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
2.在readMyCom()里面没有立即将其显示到 textBrowser,而是在一帧接收完毕之后再显示。判断一帧结束的方法是使用定时器记录收到readyRead()信号间隔的时间。当readyRead()信号过了足够长时间仍然没有收到下一个readyRead()信号,说明一帧结束。
3.发送的时候以byte方式
     uint16 crcData;
     //crcData = crc16(sendBuf,byteCount);
     //ui->sendMsgLineEdit->text().toAscii();
     sendBuf[0]=(quint8) 0x01;
     sendBuf[1]=(quint8) 0x01;
     sendBuf[2]=(quint8) 0x00;
     sendBuf[3]=(quint8) 0x00;
     sendBuf[4]=(quint8) 0x00;
     sendBuf[5]=(quint8) 0x07;

    // crcData = crc16(sendBuf,6);
     crcData = crc16((uint8*)sendBuf,6);
     DEBUG_BYSONG<<crcData;
     sendBuf[6] = crcData >> 8;
     sendBuf[7] = crcData & 0xff;

     myCom->write(sendBuf,8);
     DEBUG_BYSONG << "bytesToWrite:"<<myCom->bytesToWrite()<<"bytes";
4.发送时加上crc校验,查表方式。接收的时候没判断。
5.是一个成功与下位机modbus通信上的skeleton。需要完善modbus各个功能号。
6.qtcreator 2.3.1,qt-lib-4.7.4,xp

http://download.csdn.net/detail/songqqnew/3852094
http://download.csdn.net/detail/songqqnew/3852120

refer to
http://www.yafeilinux.com/?p=820
http://www.yafeilinux.com/?p=616
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值