here are some portion of my code:
//MaiWindow.cpp
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer_for_read
=new QTimer(this);
//this timer is used for calling continuous_monitor() QObject::connect(timer_for_read,
SIGNAL(timeout
()),
this,
SLOT(continuous_monitor
()));
QObject::connect(timer_for_read,
SIGNAL(timeout
()),timer_for_read,
SLOT(start
()));
//didnt know ways to restart timer again. start_check[0]=0;
}
void MainWindow::on_Start1_clicked()
{
if(!modbus_master.port_init)
modbus_master.init_port();
modbus_master.data_buffer.Reset();//these are for data
modbus_master.data_buffer.Append(50);
while (modbus_master.CheckBusy());
modbus_master.WriteOutputReg(10,1000);
start_check[0]=1;
qDebug()<<timer_for_read->isActive();
if(!timer_for_read->isActive())
timer_for_read->start(5000);
}
void MainWindow::continuous_monitor()
{
if(start_check[0]){
while(modbus_master.CheckBusy());// this checks whether the code is listening to response, returns 1 when listening is done
modbus_master.ReadInputReg(10,1000,4);// reads input register// this writes a message to serial port and listens to response
}
if(start_check[1]){
while(modbus_master.CheckBusy());
modbus_master.ReadInputReg(10,1000,4);
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer_for_read=new QTimer(this); //this timer is used for calling continuous_monitor()
QObject::connect(timer_for_read,SIGNAL(timeout()),this,SLOT(continuous_monitor()));
QObject::connect(timer_for_read,SIGNAL(timeout()),timer_for_read,SLOT(start()));//didnt know ways to restart timer again.
start_check[0]=0;
}
void MainWindow::on_Start1_clicked()
{
if(!modbus_master.port_init)
modbus_master.init_port();
modbus_master.data_buffer.Reset();//these are for data
modbus_master.data_buffer.Append(50);
while (modbus_master.CheckBusy());
modbus_master.WriteOutputReg(10,1000);
start_check[0]=1;
qDebug()<<timer_for_read->isActive();
if(!timer_for_read->isActive())
timer_for_read->start(5000);
}
void MainWindow::continuous_monitor()
{
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);//dint know where to do this
if(start_check[0]){
while(modbus_master.CheckBusy());// this checks whether the code is listening to response, returns 1 when listening is done
modbus_master.ReadInputReg(10,1000,4);// reads input register// this writes a message to serial port and listens to response
}
if(start_check[1]){
while(modbus_master.CheckBusy());
modbus_master.ReadInputReg(10,1000,4);
}
}
To copy to clipboard, switch view to plain text mode
//ModbuMaster.cpp
ModbusMaster::ModbusMaster()
{
QObject::connect(timer,
SIGNAL(timeout
()),
this,
SLOT(onTimeOut
()));
// onTimeout asserts that the response is invalid QObject::connect(this,
SIGNAL(WriteEmit
(char*)),
this,
SLOT(writeData
(char*)));
port=new QextSerialPort();
QObject::connect(port,
SIGNAL(readyRead
()),
this,
SLOT(readData
()));
}
void ModbusMaster::readData()
{
qDebug("read data");
port->flush();
// char buff[1024];
// qDebug("read data");
for(int i=0;i<response.length();i++)
{
ReadCharacterCB((uint8_t)response[i]); //this function has intrepretation of data
}
}
void ModbusMaster::writeData(char* msg)
{
//qDebug("Writing data");
port->write(msg,1);
port->flush();
}
ModbusMaster::ModbusMaster()
{
timer=new QTimer(this);
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(onTimeOut()));// onTimeout asserts that the response is invalid
QObject::connect(this,SIGNAL(WriteEmit(char*)),this,SLOT(writeData(char*)));
port=new QextSerialPort();
QObject::connect(port,SIGNAL(readyRead()),this,SLOT(readData()));
}
void ModbusMaster::readData()
{
qDebug("read data");
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
port->flush();
// char buff[1024];
// qDebug("read data");
QByteArray response=port->readAll();
for(int i=0;i<response.length();i++)
{
ReadCharacterCB((uint8_t)response[i]); //this function has intrepretation of data
}
}
void ModbusMaster::writeData(char* msg)
{
//qDebug("Writing data");
port->write(msg,1);
port->flush();
}
To copy to clipboard, switch view to plain text mode
Bookmarks