Modbus TCP Client write holding registers
-
Look..
I wold like callwr_ready
when QbyteArray (m_holdRegArray
) was changed, so I created code like below:void ClientMachine1::run() { ///my Modbus TCP Client code ///........ how connect wr_redy with signal holdRegArrayChanged } QByteArray ClientMachine1::holdRegArray() const { return m_holdRegArray; } void ClientMachine1::setHoldRegArray(const QByteArray &newHoldRegArray) { if (m_holdRegArray == newHoldRegArray) return; m_holdRegArray = newHoldRegArray; emit holdRegArrayChanged(); }
How connect
holdRegArrayChanged
withwr_ready
insiderun()
thread ?@Damian7546 I still don't understand the problem. If wr_ready is a slot and holdRegArrayChanged a signal then simply connect them like any other signal/slot. Can you explain what exactly is not clear?
-
Because
wr_ready
is not visible in the constructor:ClientMachine1::ClientMachine1(QObject *parent) : QThread{parent} { connect(this, ClientMachine1::holdRegArrayChanged, this, ClientMachine1::wr_ready); }
In othe hand, the
holdRegArrayChanged
I can not connect insiderun()
because I have errors:Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()' No member named 'wr_redy' in 'ClientMachine1'
-
Because
wr_ready
is not visible in the constructor:ClientMachine1::ClientMachine1(QObject *parent) : QThread{parent} { connect(this, ClientMachine1::holdRegArrayChanged, this, ClientMachine1::wr_ready); }
In othe hand, the
holdRegArrayChanged
I can not connect insiderun()
because I have errors:Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available) insert '()' No member named 'wr_redy' in 'ClientMachine1'
@Damian7546 said in Modbus TCP Client write holding registers:
Because wr_ready is not visible in the constructor
Of course it's not, because you defined it inside run().
Simply define it as normal member of ClientMachine1... -
Can you tell me how create definition in *.h file to my
wr_redy
lambda contruction andauto
type ? -
Can you tell me how create definition in *.h file to my
wr_redy
lambda contruction andauto
type ?@Damian7546 said in Modbus TCP Client write holding registers:
Can you tell me how create definition in *.h file to my wr_redy lambda contruction and auto type ?
Why does it have to be a lambda?
-
Can you tell me how create definition in *.h file to my
wr_redy
lambda contruction andauto
type ?@Damian7546 I'm also wondering why you use threads at all?
If you need to do something periodically then simply use QTimer. -
@Damian7546 said in Modbus TCP Client write holding registers:
Can you tell me how create definition in *.h file to my wr_redy lambda contruction and auto type ?
Why does it have to be a lambda?
@jsulm said in Modbus TCP Client write holding registers:
@Damian7546 said in Modbus TCP Client write holding registers:
Can you tell me how create definition in *.h file to my wr_redy lambda contruction and auto type ?
Why does it have to be a lambda?
I just want to know how to do it - for my own knowledge
-
@Damian7546 I'm also wondering why you use threads at all?
If you need to do something periodically then simply use QTimer.@jsulm said in Modbus TCP Client write holding registers:
@Damian7546 I'm also wondering why you use threads at all?
If you need to do something periodically then simply use QTimer.I would like to run Client on thread and test with several clients(threads) ... how will it be with stability and read/write speed,
So how definewr_redy
in *.h file ? -
@jsulm said in Modbus TCP Client write holding registers:
@Damian7546 I'm also wondering why you use threads at all?
If you need to do something periodically then simply use QTimer.I would like to run Client on thread and test with several clients(threads) ... how will it be with stability and read/write speed,
So how definewr_redy
in *.h file ?@Damian7546 said in Modbus TCP Client write holding registers:
So how define wr_redy in *.h file ?
Don't define it as lambda, but a normal method.
-
@Damian7546 said in Modbus TCP Client write holding registers:
So how define wr_redy in *.h file ?
Don't define it as lambda, but a normal method.
@jsulm How do I do that?
When I declare my function in *.h file :
void wr_redy();
And I will create definition this function outside my run() function , I do not have acces to variables defined in run()...
-
@jsulm How do I do that?
When I declare my function in *.h file :
void wr_redy();
And I will create definition this function outside my run() function , I do not have acces to variables defined in run()...
@Damian7546 If you want to connect a lambda to a signal then do it properly:
connect(this, ClientMachine1::holdRegArrayChanged, this, wr_redy);
wr_redy is NOT a member of your class, so simply do not prefix it with your class name.
-
@Damian7546 If you want to connect a lambda to a signal then do it properly:
connect(this, ClientMachine1::holdRegArrayChanged, this, wr_redy);
wr_redy is NOT a member of your class, so simply do not prefix it with your class name.
@jsulm said in Modbus TCP Client write holding registers:
@Damian7546 If you want to connect a lambda to a signal then do it properly:
connect(this, ClientMachine1::holdRegArrayChanged, this, wr_redy);
wr_redy is NOT a member of your class, so simply do not prefix it with your class name.
Right
-
@jsulm Unfortunetly my write function still doesn't work:/
auto wr_redy = [&](){ for(int i=0; i < m_holdRegArray.count(); i++){ qDebug() << "count: " << i; wr.setValue(i, m_holdRegArray[i]); } };
The application was crash when it
wr.setValue(i, m_holdRegArray[i]);
is called... -
@jsulm Unfortunetly my write function still doesn't work:/
auto wr_redy = [&](){ for(int i=0; i < m_holdRegArray.count(); i++){ qDebug() << "count: " << i; wr.setValue(i, m_holdRegArray[i]); } };
The application was crash when it
wr.setValue(i, m_holdRegArray[i]);
is called...@Damian7546 said in Modbus TCP Client write holding registers:
The application was crash when
So, did you use debugger to see why it crashes?
What is wr? -
@jsulm When I have status: "ModbusTCPClient is disconnected" my app crash when I try write registers..
-
@jsulm When I have status: "ModbusTCPClient is disconnected" my app crash when I try write registers..
@Damian7546 Then don't write registers if there is no connection...
-
@Damian7546 Then don't write registers if there is no connection...
@jsulm I was wrong, sometimes app was crash when I try write value. I get message like below :
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QObject: Cannot create children for a parent that is in a different thread. (Parent is QModbusTcpClient(0x504f8fc), parent's thread is ClientTicketMach(0x1215a30), current thread is QThread(0x11c0b28) QObject::startTimer: Timers cannot be started from another thread Error Modbus TCP CLient read: QModbusDevice::TimeoutError QObject::killTimer: Timers cannot be stopped from another thread
-
@jsulm I was wrong, sometimes app was crash when I try write value. I get message like below :
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QObject: Cannot create children for a parent that is in a different thread. (Parent is QModbusTcpClient(0x504f8fc), parent's thread is ClientTicketMach(0x1215a30), current thread is QThread(0x11c0b28) QObject::startTimer: Timers cannot be started from another thread Error Modbus TCP CLient read: QModbusDevice::TimeoutError QObject::killTimer: Timers cannot be stopped from another thread
@Damian7546 As all these errors tell you: do the threading properly!
-
This post is deleted!
-
This post is deleted!