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 : Qt Serial Port communication

I am writing a Qt application to communicate with another computer over a serial port. I have 2 real issues.

1. I can send and receive data fine, but sometimes the serial port "eats" part of my input. For example if I send:

cd /application/bin

sometimes (not always) it will only receive:

cd /applica

(Since it's a terminal it echoes the input back. Also my prompt tells me I'm clearly in the wrong spot.)

2. Also, sometimes the Qt slot which fires when there is data available doesn't fire even though I know that there's data I can receive. If I send another \r\n down the port the slot will fire. For example sometimes I'll ls something, and the command name will be read back from the port, but the contents of the folder sit there in limbo until I hit return again. Then I get the listing of the directory and two prompts.

Here's my code:

void Logic::onReadyRead(){        
        QByteArray incomingData;  
        incomingData = port->readAll();
        QString s(incomingData);
        emit dataAvailable(s);// this is a Qt slot if you don't know what it is.
        qDebug() << "in:"<< s.toLatin1();     
}

void Logic::writeToTerminal(QString string )
{
    string.append( "\r\n");
    port->write((char*)string.data(), string.length());
    if ( port->bytesToWrite() > 0){
        port->flush();
    }
    qDebug() << "out:" << string.toLatin1();
}

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 (1 solutions) : QT Serial Port Reading

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

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

Similar Question 9 (2 solutions) : Qt serial port does not read the string well

cc