Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1175

Warning: session_start(): Cannot start session when headers already sent in /home/styllloz/public_html/qa-include/app/users.php on line 162

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/users.php on line 1267

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/page.php on line 356
Write data to Tcp socket from another thread? - | Q&A

Write data to Tcp socket from another thread?

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
On the single computer work stream 2A. First, Tcp server betrays the data from the target PC to UART. The second checks the buffer for data (polling). UART is very slow so in a single thread to run all fails. It is necessary for the arrival of data at the UART to send that data through Tcp Server on the target PC but when calling the method write to a port, the server crashes on write ( socket->write ( data ); ) or even does not write to port ( socket->write ( "data" ); ). Where can I find documentation or example for this problem?

Protocol UART'and the error happens after calling the parser method of recording myserver.write();

void UART_Protokol::ParseCommand() { MyServer myserver; if(sp_dataString == "Start") { TransmitData = "ready"; sp_Send(TransmitData); qDebug()<<"Self"; } if(sp_dataString == "ready") qDebug()<<"Self"; if (sp_dataString =="MG1") myserver.write("MG1"); //for Example here if (sp_dataString =="MG0") myserver.write("MG0"); if (sp_dataString =="MG") qDebug()<<The "calibration..."; }

The part of Tcp server here is a method of recording MyServer::write at the end:

#include "myserver.h" MyServer::MyServer(){} //the constructor MyServer::~MyServer(){}//destructor void MyServer::startserver() { if (this->listen(QHostAddress::Any,8981))qDebug()<<"Server is running"; else{ qDebug()<<"Error while trying start server";} } void MyServer::incomingConnection(int socketDescriptor ) // the connection handler the name of the method must be incomingConnection { socket = new QTcpSocket this(this); socket->setSocketDescriptor( socketDescriptor);// prasowanie unique port number connect(socket,SIGNAL(readyRead()),this,SLOT(read())); // connect the interrupt port with a read method connect(socket,SIGNAL(disconnected()),this,SLOT(disconect())); //connect interrupt break soedineniya method qDebug()<write("Connection"); } void MyServer::read() { UART_Protokol UART; UART.StartProtokol( false ); arr = socket->readAll(); data += arr; if ( data == "wu" )UART.sp_Send("wu"); if ( data == "su" )UART.sp_Send("su"); if ( data == "au" )UART.sp_Send("au"); if ( data == "du" )UART.sp_Send("du"); if ( data == "qu" )UART.sp_Send("qu"); if ( data == "eu" )UART.sp_Send("eu"); if ( data == "wd" )UART.sp_Send("wd"); if ( data == "sd" )UART.sp_Send("sd"); if ( data == "ad" )UART.sp_Send("ad"); if ( data == "dd" )UART.sp_Send("dd"); if ( data == "qd" )UART.sp_Send("qd"); if ( data == "ed" )UART.sp_Send("ed"); if (data == "SD1")UART.sp_Send("SD1"); if (data == "SD0")UART.sp_Send("SD0"); if (data == "cl")UART.sp_Send("cl"); if( arr[0] == Mask[0]&& arr[1] == Mask[1]) { int length = 0; QByteArray dt = "tr"; trust[0] =0; trust[1] =0; trust[2] =0; length = arr[2]; trust[0] =arr[3]; trust[1] =arr[4]; trust[2] =arr[5]; truster = convert(trust, length - 48); dt[2] = truster; UART.sp_Send_integer(dt); } arr.clear(); //In case the error appears long due to improper cleaning of the array. data = "; } void MyServer::disconect() { socket->deleteLater(); } void MyServer::write (QByteArray dat) { //Here the error occurs qDebug()<write(dat); // So doesn't work either socket->write ("MG1"); }

Starting threads:
#include #include #include #include #include int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); UART_Protokol Serial; Serial.StartProtokol( false ); std::thread serial (&UART_Protokol::Event, Serial); MyServer Server; Server.startserver(); return a.exec(); }
by | 95 views

1 Answer

0 like 0 dislike
Transfer the data to the server thread in any way (queue, messages, etc.) and the server he's sending them.
110,608 questions
257,186 answers
35,181 users