Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telnet.canReadLine()) #3

Open
sirias52 opened this issue May 28, 2024 · 9 comments
Open

telnet.canReadLine()) #3

sirias52 opened this issue May 28, 2024 · 9 comments

Comments

@sirias52
Copy link

in serial comunication is posible this while (m_serialPort.canReadLine()), is posible in this program?

@silderan
Copy link
Owner

Nop.
This is an "event driven" concept. That is: "Do your staff and I (QTelnet API) will call your code when data is available".
Of course, you can do it by your own. But, not sure if it worth.

What you must do is to read and store incomming data (in your slot connected to newData signal) and when a new line is present (or whatever you want) call your functions.

Your approach is fine when there are two threads: your code and socked code. So you need to call "canReadLine" on socked code. Not needed in Qt framework.

Tell me if you need help to implement this properly.

@sirias52
Copy link
Author

Hello, greetings from Nicaragua. thanks for your answer. I am not an expert programmer, but I am implementing a telnet (your library) communication between hardware and software. The program is based on sending and receiving data. in serial communication there is no problem.
1- Sending data

void frmMain::sendCommand(QString command, int tableIndex, bool showInConsole)
{
// if (!m_serialPort.isOpen() || !m_resetCompleted) return;

if (!telnet.isConnected() || !m_resetCompleted) return;

                  /*some code*/...

qDebug() << "sendata:" << command;

//m_serialPort.write((command + "\r").toLatin1());

telnet.sendData((command + "\r").toLatin1().trimmed());
telnet.sendData("\n");
}
this part of the code works fine. I think so.

2- Recieving data

//connect(&m_serialPort, SIGNAL(readyRead()), this, SLOT(onSerialPortReadyRead()), Qt::AutoConnection);
//connect(&m_serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialPortError(QSerialPort::SerialPortError)));

connect( &telnet, SIGNAL(newData(const char*,int)), this, SLOT(onTelnetPortReadyRead(const char*,int)) );

//void frmMain::onSerialPortReadyRead()
//{
void frmMain::onTelnetPortReadyRead(const char *msg, int count)
{
// while (m_serialPort.canReadLine()) {
if(count>1) { ---------------------------------> (This way works well, but not if it is the best way.)

  QByteArray data(msg, count);
  data = data.trimmed();
  QString::fromStdString(data.toStdString().c_str());  -------> My problem is the way it receives the data.

// QString data = m_serialPort.readLine().trimmed();

  qDebug() << "Recieveddata:" << data ;

   /*some code*/......
   /*some code*/......
   /*some code*/......

}

In serial communication I receive formatted data: It's the right way to operate.

Recieveddata: "ok"
Recieveddata: "[G54:0.000,0.000,-16.500]"
Recieveddata: "[G55:0.000,0.000,0.000]"
Recieveddata: "[G56:0.000,0.000,0.000]"
Recieveddata: "[G57:0.000,0.000,0.000]"
Recieveddata: "[G58:0.000,0.000,0.000]"
Recieveddata: "[G59:0.000,0.000,0.000]"
Recieveddata: "[G28:0.000,0.000,0.000]"
Recieveddata: "[G30:0.000,0.000,0.000]"
Recieveddata: "[G92:0.000,0.000,16.500]"
Recieveddata: "[TLO:0.000]"
Recieveddata: "[PRB:0.000,0.000,0.000:0]"
Recieveddata: "ok"
Received offsets: "[G54:0.000,0.000,-16.500]; [G55:0.000,0.000,0.000]; [G56:0.000,0.000,0.000]; [G57:0.000,0.000,0.000]; [G58:0.000,0.000,0.000]; [G59:0.000,0.000,0.000]; [G28:0.000,0.000,0.000]; [G30:0.000,0.000,0.000]; [G92:0.000,0.000,16.500]; [TLO:0.000]; [PRB:0.000,0.000,0.000:0]; ok"

In telnet communication I receive this data:

Recieveddata: "ok"

Recieveddata: "[G54:-83.285,23.397,4.872,-40.000,0.000,0.000]\n[G55:0.000,0.000,0.000,0.000,0.000,0.000]\n[G56:0.000,0.000,0.000,0.000,0.000,0.000]\n[G57:0.000,0.000,0.000,0.000,0.000,0.000]\n[G58:0.000,0.000,0.000,0.000,0.000,0.000]\n[G59:0.000,0.000,0.000,0.000,0.000,0.000]\n[G28:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G30:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G92:35.158,16.739,0.000,0.000,0.000,0.000]\n[TLO:0.000]\nok"
Received offsets: "[G54:-83.285,23.397,4.872,-40.000,0.000,0.000]\n[G55:0.000,0.000,0.000,0.000,0.000,0.000]\n[G56:0.000,0.000,0.000,0.000,0.000,0.000]\n[G57:0.000,0.000,0.000,0.000,0.000,0.000]\n[G58:0.000,0.000,0.000,0.000,0.000,0.000]\n[G59:0.000,0.000,0.000,0.000,0.000,0.000]\n[G28:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G30:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G92:35.158,16.739,0.000,0.000,0.000,0.000]\n[TLO:0.000]\nok"

this generates some bugs for me.

I don't know if it's something in this part of the code

void QTelnet::onReadyRead()
{
qint64 readed;
qint64 processed;

while( (readed = read(m_buffIncoming, IncommingBufferSize)) != 0 )
{
	switch( readed )
	{
	case -1:
		disconnectFromHost();
		break;
	default:
		processed = doTelnetInProtocol(readed);
		if( processed > 0 )
			Q_EMIT(newData(m_buffProcessed, processed));

		break;
	}
}

}

or I'm the problem in this part.

QByteArray data(msg, count);
data = data.trimmed();
QString::fromStdString(data.toStdString().c_str());

thank you very much for your help.

@silderan
Copy link
Owner

silderan commented Jun 1, 2024 via email

@sirias52
Copy link
Author

sirias52 commented Jun 2, 2024

Hola, gracias por su respuesta y amabilidad, si hablo español jeje. La verdad estoy tratando de hacer funcionar este proyecto https://github.com/Denvi/Candle, pero con comunicion telnet. No se, si usted ha escuchado de grbl. Simplemente es un proyecto para menejo de equipos CNC de codigo abierto. Pero hasta el momento solo hay una opción por telnet o websocket, pero no es de mi agrado. Así que decidí trabajar este proyecto que para mí es muy comodo y agradable visualmente. Entonces encontré su librería para telnet y la implementé en el candle. Funciona bien, pero mi problema es a la hora de recibir datos, no me interpreta las "\n" como su debido siguiente línea.

cuando la aplicación se conecta por telnet (con lo que he modificado el codigo) recibo exactamente la respuesta a como debería estar en serial.

grbl reset
Recieveddata: "GRBL 3.7 [CNC v3.7.12 (wifi) '$' for help]"
reseting filter: "GRBL 3.7 [CNC v3.7.12 (wifi) '$' for help]"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0|WCO:-83.285,23.397,4.872,-40.000,0.000,0.000>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0|Ov:100,100,100>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
sendata: "$G"
Recieveddata: "[GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0]"
Recieveddata: "ok"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
sendata: "$G"
Recieveddata: "[GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0]"
Recieveddata: "ok"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0>"
Recieveddata: "<Idle|MPos:416.267,13.398,-6.000,0.000,0.000,0.000|FS:0,0|WCO:-83.285,23.397,4.872,-40.000,0.000,0.000>"
sendata: "$G"

y asi esta repitiendo esta secuencia una vez se envia "$G". hasta por aca todo espectacular, el problema ocurré si yo envio un seteo de posición.

sendata: "G92X0Y0"
sendata: "$#"
Recieveddata: "ok"
Recieveddata: "[G54:-83.285,23.397,4.872,-40.000,0.000,0.000]\n[G55:0.000,0.000,0.000,0.000,0.000,0.000]\n[G56:0.000,0.000,0.000,0.000,0.000,0.000]\n[G57:0.000,0.000,0.000,0.000,0.000,0.000]\n[G58:0.000,0.000,0.000,0.000,0.000,0.000]\n[G59:0.000,0.000,0.000,0.000,0.000,0.000]\n[G28:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G30:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G92:499.552,-10.000,0.000,0.000,0.000,0.000]\n[TLO:0.000]\nok"
Received offsets: "[G54:-83.285,23.397,4.872,-40.000,0.000,0.000]\n[G55:0.000,0.000,0.000,0.000,0.000,0.000]\n[G56:0.000,0.000,0.000,0.000,0.000,0.000]\n[G57:0.000,0.000,0.000,0.000,0.000,0.000]\n[G58:0.000,0.000,0.000,0.000,0.000,0.000]\n[G59:0.000,0.000,0.000,0.000,0.000,0.000]\n[G28:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G30:-334.875,52.796,0.000,0.000,-137.182,-137.182]\n[G92:499.552,-10.000,0.000,0.000,0.000,0.000]\n[TLO:0.000]\nok"

el envio de dato esta a como debería ser, pero al recibir el dato recibo el mensaje sin su debido espacio, como que no interpreta el \n. y esto genera que no se setee o que la tarjeta al comparar estos valores no lo haga adecuadamente. Pero si yo envio un archivo para que lo procese, se procesa muy bien, aunque a veces recibe ese \n y crashea.

Ayer estuve probando sus lineas.

QByteArray data(msg, count);

foreach( const QByteArray &lineaByes : data.split('\n') )
{
QString lineaString = data.toStdString();
qDebug() << "Receiveddata: " << lineaString << "\n";
}

En esta parte el compildor me da un error, no sabe quien es &lineaByes.

QByteArray data(msg, count);

static QByteArray bytesLinea;

for( int i = 0; i < count; ++i )
{
if( ((msg[i] == '\n') || (msg[i] == '\r')) && !bytesLinea.isEmpty() )
{
qDebug() << "Receiveddata: " << bytesLinea.toStdString() << "\n" ;
// DO your stuff here.
bytesLinea.clear();
}
}

en este otro funciona, pero lo recibe sin comillas y a la hora de poner el resto de codigo en // DO your stuff here// no funciona.

Con respecto a esto: "PEEEERO debe ser if( count > 0 ) Y NO if( count >1 )", si pongo cero no funciona. Es extraño, pero no funciona.

Aclaro, no creo sea un problema con su librería, mas bien parece un problema de que no he podido procesar bien lo que recibo.

@silderan
Copy link
Owner

silderan commented Jun 3, 2024 via email

@sirias52
Copy link
Author

sirias52 commented Jun 3, 2024

Si , con el harware es de evidente ayuda. Si estoy modificando el candle, y a como le dije funciona bien, solo con los inconvenientes mencionados. De hecho ya está agregada su libreria al proyecto... Asi es, con el reset esta funcionando bien, pero a la hora de dar algún comando en el que el grbl retorna posiciones , es ahi los problemas de los \n, de ahi con otros comando que no retorne posiciones no hay problema. Voy a revisar para crear el fichero. De todas formas el hardaware es un ESP32 (https://www.ebay.es/itm/364306536290?itmmeta=01HZDT4RR5GKXP67YXDRX6KZ9R&hash=item54d25c9762:g:2~YAAOSw1ZVkjWEQ&itmprp=enc%3AAQAJAAAA4JFj9dxzyXUzYlC0rrtMedok4%2BebVZMv6G9rZSUlx%2BK3kCW6SIJ4eAOEv%2FA1P6OPYz3eT%2BRbj0rAhNzwqpC5f--L56xnPUrJFU%2B1DfNqt5pJs08N3auznox615DSj7emcKzCu%2BM3gnBW%2F9%2BvCZm%2FBjUb2UVRqrKsYmZ9cZ4jbgIRXpumm9DNMdQXxYbXK6TrrQuG8RFQyib72Iz0uBRcmkPvl4WGGkxD3Uk4BIsbmZUvnF4aO1Kerx6VNrEgAyLUyioFNCeVO78q0yTuAWqodcpAIM8hB3WyUCLUbsaDu5Si%7Ctkp%3ABk9SR5aMk7r7Yw) con el firmware Fluid cnc https://github.com/bdring/FluidNC

Por otro lado, con su mismo programa Qtelnettester se puede estar probando, ya que el debug retorna los mismo errores. El debug debe estar sin <<\n, ya que deberia recibir los datos ya con su respectivo "enter".

WhatsApp Image 2024-06-02 at 19 13 00

@sirias52
Copy link
Author

sirias52 commented Jun 3, 2024

Muchas gracias. Funcionaaa... for(const QByteArray &lineaByes : data.split('\n') ), ayudo mucho, ya no tiene ningun bug, ni con altas velocidades. Su librería esta genial y ahora con su ayuda ya se tiene un controlador CNC por telnet. Asi que su libreria andará gestionando placas CNC por telnet. Igual seguiré depurando, pero muchas gracias.

Ahora solo necesito probarla con otro firmware y estaría completo.

@silderan
Copy link
Owner

silderan commented Jun 3, 2024 via email

@sirias52
Copy link
Author

sirias52 commented Jun 3, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants