PDA

View Full Version : qext-serial-port write problem.



rex
1st March 2011, 14:21
hello every one.. i have a small problem here. I am communicating with a hardware on rs232 serial terminal.. I am able to send a start of frame and receive back a acknowledge-ment from the hardware device. i am sending a fixed string i.e 0xAC,0xCA,0x1F as the start of the frame which the hardware device sud check for and give a ackn. which is working fine . this is what i am doing i am writing the string AC CA 1F to the port this is done successfully.i get a acknowledgment from the hardware also.

static const char mydata[] = { 0xAC,0xCA,0x1F};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
char i = port->write(mydata,sizeof(mydata));

Now the problem is when i try to send some configured hexa format data which i have
in QByteArray hexadecimaldata; i am splitting the each byte in it and putting them into firstByte and secondByte. cause i have to send the configured data byte by byte and the data is never more then 2 bytes.


firstByte = "0x"+hexadecimaldata.left(2);
secondByte = "0x"+hexadecimaldata.mid(1, 2);
char i = port->write(firstByte);
char i = port->write(secondByte);


the configured hexadecimaldata is not wrting to the port..

Pls tell me if i am going wrong some wer. ?

thank you

alainstgt
1st March 2011, 15:40
have firstByte and secondByte been declared somewhere, and hexadecimaldata also been initialised?
Try also qdebug() << firstByte << secondByte; to check your values before sending them to the port.

mcosta
1st March 2011, 15:44
hi,

your code

firstByte = "0x"+hexadecimaldata.left(2);
secondByte = "0x"+hexadecimaldata.mid(1, 2);

is strange.
you are pre-pending the sequence "0x" to your binary data.
this is what you want?

pay attention that you extract 2 bytes from hexadecimaldata with left() and mid()

rex
1st March 2011, 16:07
hi, thank you for the reply.. :) i did try qDebug()<< firstByte << secondByte; i can see the value there. I have firstByte ,secondByte,hexadecimaldata declared as QByteArray in the .h file i am doing some calculations and getting a decimal data which i convert into hexadecimal and append it into the byte array hexadecimaldata.
This is of 2 bytes i need to send the first byte and after a small delay the second byte, thats when the hardware i am communicating with will respond to the data i send.


void SingleChDACQ::CalEqu()
{
float e = lineEdit_3->text().toFloat();
float s = lineEdit_2->text().toFloat();
float f = 5 * s;
float g = 1000 * e;
volt = f/g;
// qDebug()<<volts;

volts = volt * 1000;
hexvaluebuff = volts / 2;
finhexval = hexvaluebuff/0.61035;
uint decimal = finhexval;
hexadecimal.setNum(decimal,16);

firstByte = "0x"+hexadecimal.left(1);
secondByte = "0x"+hexadecimal.mid(1, 2);
sendFrame() ;
// in send frame i am trying to send these 2 bytes. char i = port->write(firstByte);
//char i = port->write(firstByte);
//char i = port->write(secondByte);
}

Added after 4 minutes:

hi thanks for your reply,
i have appended 0x because even though i am converting the decimal to hex format i am not able to communicate cause the hardware is not detecting the bytes sent. But if i put 0x before the data i have i get the repose from the hardware.


firstByte = "0x"+hexadecimaldata.left(2);
secondByte = "0x"+hexadecimaldata.mid(1, 2);
where do you think i am going wrong.?? not able to send the data when i send using qbytearray. but when i do this it works properly. but when i try to send the data with a byte array
char i = port->write(firstByte); i am not able to get send the data cause i am not getting a response.

static const char mydata[] = {0xAC,0XCA};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
char i = port->write(mydata,sizeof(mydata));
i tried so many diffrent things but was not sucessfull.
thank you.

^NyAw^
1st March 2011, 16:45
Hi,



hexadecimal.setNum(decimal,16);

Here, "hexadecimal" contains the value stored as hexadecimal into a QByteArray.



firstByte = "0x"+hexadecimaldata.left(2);


Here you are prepending "0x" to the hexadecimal value.

In the first step, if you have a value=1, the QByteArray contains "01".
On the second step you are prepending "0x" and you get "0x01" into QByteArray.
Don't you think that you are prepending something that is not needed? Try removing the second step.

rex
1st March 2011, 16:52
hi
i did even try that i initially did not append any "0x" to the data itself, but since that did not work i though may be i had to append "0x" but it did not help. I am only able to send data when i have fixed string

static const char mydata[] = {0xFF};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
char i = port->write(data,sizeof(mydata));
but when i append the data into byte array's
firstByte and secondByte the data is not sent on the port.
when i do this char i = port->write(secondByte); the data is not sent.
don't know where i am going wrong.!!

thank you

^NyAw^
1st March 2011, 17:07
Hi,

Try using this serial port monitor that will help you to see what you are sending throught th port http://www.hhdsoftware.com/serial-monitor
Open first the serial port sniffer and then your application.

rex
1st March 2011, 17:41
oki sir i will download the analyzer now and check what i am sending.. :) thank you for the suggestion ,

Added after 30 minutes:

Hello, i solved the problem.. :) thank you every one for the help.

cheers

marcvanriet
1st March 2011, 20:03
Hi,

Is it really your intention to send (for example) the text "0x01" to the device ? Or do you have to send a single byte with value (for example) 1 ?

Best regards,
Marc

rex
2nd March 2011, 06:25
hello i was doing something very silly, i though i converted the decimal to hex but that din't happen. I figured it out when i checked it on the network analyser..

i just did this convert it to hexa decimal and bingo it writes to the port.. :)


databuff.append(secondByte.toInt(0,16));
char i = port->write(databuff);
databuff.clear();


Regards

chriskibuchi
16th September 2013, 10:35
hello i was doing something very silly, i though i converted the decimal to hex but that din't happen. I figured it out when i checked it on the network analyser..

i just did this convert it to hexa decimal and bingo it writes to the port.. :)


databuff.append(secondByte.toInt(0,16));
char i = port->write(databuff);
databuff.clear();


Regards
I appreciate this code because it has saved me on something I was working. I had a string which I wanted to write to the port and through this code I have made it


QString st="0200000748"+nambayacard.mid(0,2)+nambayacard.mid(2,2)+nambaya card.mid(4,2)+nambayacard.mid(6,2)+checksumdata+"03";
QString stringhere="";
QByteArray data;
for (int i = 0; i < st.length(); i++)
{
stringhere=st.mid(i,2);
data.append(stringhere.toInt(0,16));
i+=1;
}
port->write(data);

I have been working on it since Friday and now I am free
Thank you and God bless you

A9am
9th December 2013, 07:18
Hi everyone,

I am using serial port to send some data to device. I am sending the data continuously and receiving acknowledgement from the device. This works fine for some time . Then suddenly the application crash at the write function. I am confused as this is working fine for some time.

port->write(byteArray);

What could be the issue? Please help me.

I am using the port in a thread...