why i cant write to serial ? | Arduino Uno , QT 5.9.9 , C++ Question
Unsolved
General and Desktop
-
My code can read from arduino uno fine, but it cannot write to it. I thought about it because I was trying to write while reading data in the same time , so i commented the part that read data, but it still doesn't work. Here is the call for the code
uno.write_to_arduino("1"); //! testing
here is the arduino init (it work fine beside the writing part)
#include <QDebug> #include "arduino.h" Arduino::Arduino() { data = ""; arduino_port_name = ""; arduino_is_available = false; serial = new QSerialPort; } QString Arduino::getarduino_port_name() { return arduino_port_name; } QSerialPort *Arduino::getserial() { return serial; } int Arduino::connect_arduino() { // recherche du port sur lequel la carte arduino identifée par arduino_uno_vendor_id // est connectée foreach (const QSerialPortInfo &serial_port_info, QSerialPortInfo::availablePorts()) { if (serial_port_info.hasVendorIdentifier() && serial_port_info.hasProductIdentifier()) { if (serial_port_info.vendorIdentifier() == arduino_uno_vendor_id && serial_port_info.productIdentifier() == arduino_uno_producy_id) { arduino_is_available = true; arduino_port_name = serial_port_info.portName(); } } } qDebug() << "arduino_port_name is :" << arduino_port_name; if (arduino_is_available) { // configuration de la communication ( débit...) serial->setPortName(arduino_port_name); if (serial->open(QSerialPort::ReadWrite)) { serial->setBaudRate(QSerialPort::Baud9600); // débit : 9600 bits/s serial->setDataBits(QSerialPort::Data8); // Longueur des données : 8 bits, serial->setParity(QSerialPort::NoParity); // 1 bit de parité optionnel serial->setStopBits(QSerialPort::OneStop); // Nombre de bits de stop : 1 serial->setFlowControl(QSerialPort::NoFlowControl); return 0; } return 1; } return -1; } int Arduino::close_arduino() { if (serial->isOpen()) { serial->close(); return 0; } return 1; } QByteArray Arduino::read_from_arduino() { if (serial->isReadable()) { data = serial->readAll(); // récupérer les données reçues return data; } } int Arduino::write_to_arduino(QByteArray d) { if (serial->isWritable()) { serial->write(d); // envoyer des donnés vers Arduino qDebug() << "Data sent to Arduino: " << d; } else { qDebug() << "Couldn't write to serial!"; } }
i get "Couldn't write to serial!" is there at least a way to debug the issue more ?
-
@Lime1 said in why i cant write to serial ? | Arduino Uno , QT 5.9.9 , C++ Question:
Also, switching the USB port did not work.
What should this do? :)
Maybe you need some terminating
\r\n
(CR LF)
Try to send it at the end of your data.What does
QSerialPort::error()
return?