I need to transmit data in 8-bit format via rs232. I am getting errors . please kindly help me.
-
void SerialConnect::write(const QString data) { QBitArray dataArray1(8); dataArray1[1]=0;dataArray1[2]=0;dataArray1[3]=1;dataArray1[4]=0;dataArray1[5]=0;dataArray1[6]=1;dataArray1[7]=1;dataArray1[8]=0; qint64 bytesWritten = serial->write(dataArray1); if (bytesWritten == (-1)) { qDebug() << "Error writing data to serial port: " << serial->errorString(); } qDebug() << "SerialConnect::write(QString) Text sent as QByteArray: " << dataArray1; qDebug() << "Number of bytes written: " << bytesWritten; }
-
Hi, arrays start with [0] not [1]. Also I don't think Qt's serialport class supports writing a QBitArray, try using a QByteArray :-)
@hskoglund
thanks for the reply , But i need to transmit data as 8-bit . Is it possible to send in bytes. By converting bits as hexa decimals or decimals -
As you see in the docs http://doc.qt.io/qt-5/qserialport.html#writeData you can send
char
buffer through serial port.
Probably you want to have something like://0-indexed bit position void setBit(uint bit, bool value, char& buffer) { if(bit < 8) buffer |= (value << bit); }
Since
char
is one byte you can imagine how this function works:char data = 0b00000000; setBit(3, true, data); //now data is 0b00001000;
Now to the sending part:
//1st option QByteArray byteToSend(1, data); //data from previous part serial->write(byteToSend); serial->waitForBytesWritten(1000); //2nd option char byteToSend[1] = {data}; serial->write(byteToSend, 1); serial->waitForByteWritten(1000);
Of course you can define byte you wanna write in the beginning assuming your example you can have something like:
char dataArray = 0b00100110;
and send this one somehow.
For more bitwise operators you can check here: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=163 or just google it :) -
thank you very much mr michelson . Its is working , i am able to transmit the bit data. And could please tell me how to send the data on after another, Because rs232 serialport can not transmit all at a time. Could you please write me a sample code.
thanks in advance. -
You can just pass larger
QByteArray
orchar*
with all the data you need.
For user controlled flow you can try sending new data as above after eg. button click (so somewhere in apropriate slot probably).
Or justQByteArray data = ...//fill serial->write(data); serial->waitForBytesWritten(1000); data = ...//again fill serial->write(data); serial->waitForBytesWritten(1000);
and so on (mayby in loop?)