Last Updated
Viewed 14 Times

I have many devices that communicate through COM port(tty/s0), i need to implement collision avoidance to make collision impossible. Please give me some algorithm or code. Using: smart6818(Friendly arm), Ubuntu 16.04.5 LTS

I am performing an app with QT for communicate my PC with an 8 bits microcontroller through rs-232. I am using the QtSerialPort library and the communication is working fine, but each time I write something from the PC to the micro and I receive the response, I have to close and open the serial port or I can't continue communicating.. My configuration is: 10500 bps, 8 bits, 1 stop, no parity, no flow control.

The code used for the configuration and the lecture/writting of the port is the next:

bool DriverS::configure(int port, int baudRate)
{
    if(port!=22)
        return false;

     serialPort->setPortName("COM22");

 if (serialPort->open(QIODevice::ReadWrite)==true){

        if (!serialPort->setBaudRate(baudRate)) {
            return false ;
        }

        if (!serialPort->setDataBits(QSerialPort::Data8)) {
            return false ;
        }

        if (!serialPort->setParity(QSerialPort::NoParity)) {
            return false;
        }

        if (!serialPort->setStopBits(QSerialPort::OneStop)) {
            return false;
        }

        if (!serialPort->setFlowControl(QSerialPort::NoFlowControl)){
            return false;
            }
    };

    return true;
}



bool DriverS::read(QByteArray & rxData, int * size)
{

    Sleep(200); 
    *size = 0;
    if (serialPort->waitForReadyRead(TIMEOUT_SERIAL)) {
        rxData = serialPort->readAll();
        *size = rxData.size() ;

    if (!this->checkCRC(rxData))
        {
            qDebug()<< "Rx Checksum Error";
            return false;
        }

    return true;
    }
    qDebug()<< "Rx Timeout";
    return false;
}

bool DriverS::write(QByteArray txData)
{

    unsigned int chk = 0;
    int ret ;

    for(int i = 0;i<txData.size();i++)
    {
        chk+=txData.at(i);
    }

    txData.append(chk);
    ret = serialPort->write(txData);
    return (txData.size()==ret);

}

Similar Question 2 : Connecting to Serial port in QT

I'd like to connect to a microcontroller using QSerialPort. I've added the line serial port to my .pro file, included QSerialPort in my source file and ran qmake. My code is below:

    serial.setPortName("COM3");
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    serial.open(QIODevice::ReadWrite);
    serial.write("ok*");

When I run the code I get a message saying the device is not open though I've confirmed it's open with TeraTerm. What am I missing? The error message is below:

QIODevice::write: device not open

Similar Question 3 : QT serial port not working

I'm trying to write \ read from a serial device using a usb / rs 232 cable. I'm pretty sure that my code writes " #002s " (this is a control code) to the serial device, because

a) the serial port is open

b) the serial port is writable

c) the code successfully navigates "wait For Bytes Written (-1)"

d) when using a serial port sniffer the data is successfully written.

The issue I have is that I don't receive any data, and no data is being emitted from the other device. When using qt terminal writing the same " #002s " produces the correct response.

Any ideas?

many thanks.

    #include "test_serial.h"
#include "ui_test_serial.h"
#include <QtSerialPort/QtSerialPort>
#include <QDebug>

QSerialPort *serial;
Test_Serial::Test_Serial(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Test_Serial)
{
    ui->setupUi(this);

  serial = new QSerialPort(this);
  connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
  serial->setPortName("COM1");
  serial->setBaudRate(QSerialPort::Baud9600);
  serial->setDataBits(QSerialPort::Data8);
  serial->setParity(QSerialPort::NoParity);
  serial->setStopBits(QSerialPort::OneStop);
  serial->setFlowControl(QSerialPort::NoFlowControl);
  serial->open(QIODevice::ReadWrite); 

  if (serial->isOpen() && serial->isWritable())
 {
  QByteArray input;

  input = "#";
  serial->write(input);
  serial->waitForBytesWritten(-1);
  input = "0";
  serial->write(input);
  serial->waitForBytesWritten(-1);
  input = "0";
  serial->write(input);
  serial->waitForBytesWritten(-1);
  input = "2";
  serial->write(input);
  serial->waitForBytesWritten(-1);
  input = "s";
  serial->write(input);
  serial->waitForBytesWritten(-1);
  input = "\r";
  serial->write(input);
  serial->waitForBytesWritten(-1);
  serial->flush();

  serial->waitForReadyRead(100);
  QByteArray output = serial->readAll();
  ui->label_2->setText(output);

}}


Test_Serial::~Test_Serial()
{
    delete ui;
    serial->close();
}

void Test_Serial::serialReceived()
{
    QByteArray output;
    output = serial->readAll();
    ui->label->setText("output");
}

Similar Question 4 (2 solutions) : Qt Serial Port communication

Similar Question 6 (2 solutions) : Qt - serial port name in ubuntu

Similar Question 7 (2 solutions) : Qt - circles for collision detection

Similar Question 9 (1 solutions) : Qt serial port: write and read data

cc