[SLOVED]how to save \0 in my array coming from serial
-
//----------------------------------------------------SIGNAL SLOT CONNECTION------------------------------------------//
connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)), SLOT(onBaudRateChanged(int)));
connect(ui->parityBox, SIGNAL(currentIndexChanged(int)), SLOT(onParityChanged(int)));
connect(ui->dataBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onDataBitsChanged(int)));
connect(ui->stopBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onStopBitsChanged(int)));
connect(ui->queryModeBox, SIGNAL(currentIndexChanged(int)), SLOT(onQueryModeChanged(int)));
connect(ui->timeoutBox, SIGNAL(valueChanged(int)), SLOT(onTimeoutChanged(int)));
connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
connect(port, SIGNAL(readyRead()), SLOT(onReadyRead()));
connect(timer1,SIGNAL(timeout()),this,SLOT(MySlot()));
connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
//----------------------------------------------------SIGNAL SLOT CONNECTION------------------------------------------//
//---------------------------------------------------PORT OPENING-----------------------------------------------------//
port->setPortName(ui->portBox->currentText());
port->open(QIODevice::ReadWrite);
//If using polling mode, we need a QTimer
if (port->isOpen() && port->queryMode() == QextSerialPort::Polling)
timer->start();
else
timer->stop();
//update led's status
ui->led->turnOn(port->isOpen());
//---------------------------------------------------PORT OPENING-----------------------------------------------------//
}
Dialog::~Dialog()
{
delete ui;
delete port;
}
void Dialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Dialog::onBaudRateChanged(int idx)
{
port->setBaudRate((BaudRateType)ui->baudRateBox->itemData(idx).toInt());
}void Dialog::onParityChanged(int idx)
{
port->setParity((ParityType)ui->parityBox->itemData(idx).toInt());
}void Dialog::onDataBitsChanged(int idx)
{
port->setDataBits((DataBitsType)ui->dataBitsBox->itemData(idx).toInt());
}void Dialog::onStopBitsChanged(int idx)
{
port->setStopBits((StopBitsType)ui->stopBitsBox->itemData(idx).toInt());
}void Dialog::onQueryModeChanged(int idx)
{
port->setQueryMode((QextSerialPort::QueryMode)ui->queryModeBox->itemData(idx).toInt());
}void Dialog::onTimeoutChanged(int val)
{
port->setTimeout(val);
} -
Hi,
Please enclose your code with coding tags (one @ at the beginning and another at the end of your code)
Otherwise it's pretty much unreadable
-
@ //———————————————————-INITIALIZATION———————————————————————————//
char String11={0×05,0×04,0×01,0×01,0×00,0×0A,0×71,0×89,0×00,’\0’};
char StringOne10={“AB CBBB—”};
char test10;
char *str;
int i=0,a=5;
char DATA50={0×05,0×04,0×00,0×00,0×00,0×30,0×71,0×89,0×00,’\0’};
QString buffer;
Dialog::Dialog(QWidget *parent) :
QDialog(parent), ui(new Ui::Dialog)
{ ui->setupUi(this); ui->stringOne->setText(String); ui->stringTwo->setText(StringOne); //————————————————————PORTSETTING—————————————————————————-//
ui->portBox->addItem(“ttyUSB0”); ui->portBox->addItem(“ttyUSB1”); //make sure user can input their own port name! ui->portBox->setEditable(true);/———————————————————DATA RECEIVE SECTION————————————————————//
void Dialog::onReadyRead()
{ if (port->bytesAvailable()) { int avail=port->bytesAvailable(); ui->recvEdit->moveCursor(QTextCursor::End); buffer=QString::fromLatin1(port->readAll()); ui->recvEdit->insertPlainText(buffer); QByteArray ba; ba=buffer.toLatin1(); str=ba.data(); for(int i=0;i<avail;i++){test[i]=str[i]+48;} ui->stringOne->setText(test); }
}
//———————————————————DATA RECEIVE SECTION————————————————————////———————————————————DATA TRANSMIT SECTION-(MY SLOT)——————————————————————-//
void Dialog::MySlot()
{ if (port->isOpen()){port->write(String);}
}
//——————————————————DATA TRANSMIT SECTION-(MY SLOT)———————————————————————//@ -
@
//——————————————————————————SIGNAL SLOT CONNECTION—————————————————————// connect(ui->baudRateBox, SIGNAL), SLOT)); connect(ui->parityBox, SIGNAL), SLOT)); connect(ui->dataBitsBox, SIGNAL), SLOT)); connect(ui->stopBitsBox, SIGNAL), SLOT)); connect(ui->queryModeBox, SIGNAL), SLOT)); connect(ui->timeoutBox, SIGNAL), SLOT)); connect(timer, SIGNAL), SLOT)); connect(port, SIGNAL), SLOT)); connect(timer1,SIGNAL),this,SLOT)); connect(enumerator, SIGNAL), SLOT)); connect(enumerator, SIGNAL), SLOT));
//——————————————————————————SIGNAL SLOT CONNECTION—————————————————————//
//—————————————————————————-PORT OPENING——————————————————————————-// port->setPortName(ui->portBox->currentText()); port->open(QIODevice::ReadWrite); //If using polling mode, we need a QTimer if (port->isOpen() && port->queryMode() == QextSerialPort::Polling) timer->start(); else timer->stop(); //update led’s status ui->led->turnOn(port->isOpen());
//—————————————————————————-PORT OPENING——————————————————————————-//
}
Dialog::~Dialog()
{ delete ui; delete port;
}
void Dialog::changeEvent(QEvent *e)
{ QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }
}
void Dialog::onBaudRateChanged(int idx)
{ port->setBaudRate((BaudRateType)ui->baudRateBox->itemData(idx).toInt());
}void Dialog::onParityChanged(int idx)
{ port->setParity((ParityType)ui->parityBox->itemData(idx).toInt());
}void Dialog::onDataBitsChanged(int idx)
{ port->setDataBits((DataBitsType)ui->dataBitsBox->itemData(idx).toInt());
}void Dialog::onStopBitsChanged(int idx)
{ port->setStopBits((StopBitsType)ui->stopBitsBox->itemData(idx).toInt());
}void Dialog::onQueryModeChanged(int idx)
{ port->setQueryMode((QextSerialPort::QueryMode)ui->queryModeBox->itemData(idx).toInt());
}void Dialog::onTimeoutChanged(int val)
{ port->setTimeout(val);
}
@ -
can any one provide some help , how to make '\0' non termination for serial communication so that i can able to retrive my complete set of data at receiver or slave end when i am transmitting from Master End.
-
You can't use strings, because 0x00 == '\0' is a termination character. Use a raw QByteArray.
-
when My String is :
@
char String[11]={0x05,0x04,0x01,0x01,0x01,0x0A,0x71,0x89,'\0'};
@
is this then I i can able to see on label defined and value of avail=8 and and data i can seen in my Temp_Buf;but when My string is :
@
char String[11]={0x05,0x04,0x00,0x00,0x00,0x0A,0x71,0x89,0x00,'\0'};
@then the value of avail=2; and it is get terminated by the function.
@void Dialog::onReadyRead()
{
int avail = port->readData(Temp_Buf,256);for(int i=0;i<avail;i++) {test[i]=Temp_Buf[i]+48;} ui->stringOne->setText(test); ui->stringTwo->setNum(avail);
}
@When I tried diffrently :
@
void Dialog::onReadyRead()
{
if (port->bytesAvailable())
{
int avail=port->bytesAvailable();
QByteArray data = port->readAll();
ui->recvEdit->insel);rtPlainText(data);
for(int i=0;i<8;i++)
{
test[i]=data[i]+48;
}
ui->stringOne->setText(test);
ui->stringTwo->setNum(avail);
}
}
@
@
Same thing happens fuction read all when it encounter 0 it get terminated is there any fuction which not terminate Zero or i have to modify the fuction
@
bytesAvailable();
@
because when i am reading bytes available in case of array contating zero it terminates but in case of array contating non-zero show complete value.Thank you
-
As said by JKSH the 0x00 is equivalent to '\0' which terminates a string (it's not QString specific)
If you only want to show the values from the byte array you could consider the hexadecimal representation. Otherwise you have to parse your byte array and translate it to something that can be shown in a string.
-
Sir ,
Actully From MODBUS protocol i will be receving same pattern
as an array named String ;
As I am in problem i know if it getting 0 it will terminate@
char String[11]={0x05,0x04,0x00,0x00,0x00,0x0A,0x71,0x89,0x00,'\0'};int avail =port->bytesAvailable(); // is also providing the value upto it
///My Updated Code is Code is :
void Dialog::onReadyRead() { if (port->bytesAvailable()) { int avail=port->bytesAvailable(); QByteArray buf = port->readAll(); for(int i=0;i<avail;i++) { test[i]=buf[i]+48; } ui->stringOne->setText(test); ui->stringTwo->setNum(avail); } }
@
/======/code for transmitting------//
@
void Dialog::MySlot()
{
if (port->isOpen()){port->write(String);
}
// I am transmitting it from RS 232 and making Rx-Tx short for testing -
@
Now i am not dealing with Qstring.
Just Handling QByteArray buf array to represent the data I received and and make a conversion to make it visible.if you help in some coding pls do so...need help
And if --- > Raw QByteArray is solution then pls provide some example I will be very thankfull to you..
till now i have tried with readData , readall() ,readLine in all cases the problem is same.Thankyou.
-
If you are working with hexadecimal values you can use QByteArray::toHex() to show the byte array content.
Don't use write(const char *), as the documentation states, it's for null terminated strings. Use write(const char *, int), and give it the size of your String variable.
-
it working fine by using
@
QByteArray data = QByteArray::fromRawData(port->readAll(),sizeof(port->readAll()));
@