Results 1 to 12 of 12

Thread: qext-serial-port write problem.

  1. #1
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default qext-serial-port write problem.

    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.
    Qt Code:
    1. static const char mydata[] = { 0xAC,0xCA,0x1F};
    2. QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
    3. char i = port->write(mydata,sizeof(mydata));
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. firstByte = "0x"+hexadecimaldata.left(2);
    2. secondByte = "0x"+hexadecimaldata.mid(1, 2);
    3. char i = port->write(firstByte);
    4. char i = port->write(secondByte);
    To copy to clipboard, switch view to plain text mode 

    the configured hexadecimaldata is not wrting to the port..

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

    thank you
    Last edited by rex; 1st March 2011 at 14:25.

  2. #2
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qext-serial-port write problem.

    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.

  3. The following user says thank you to alainstgt for this useful post:

    rex (1st March 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qext-serial-port write problem.

    hi,

    your code
    Qt Code:
    1. firstByte = "0x"+hexadecimaldata.left(2);
    2. secondByte = "0x"+hexadecimaldata.mid(1, 2);
    To copy to clipboard, switch view to plain text mode 

    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()
    A camel can go 14 days without drink,
    I can't!!!

  5. The following user says thank you to mcosta for this useful post:

    rex (1st March 2011)

  6. #4
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: qext-serial-port write problem.

    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.

    Qt Code:
    1. void SingleChDACQ::CalEqu()
    2. {
    3. float e = lineEdit_3->text().toFloat();
    4. float s = lineEdit_2->text().toFloat();
    5. float f = 5 * s;
    6. float g = 1000 * e;
    7. volt = f/g;
    8. // qDebug()<<volts;
    9.  
    10. volts = volt * 1000;
    11. hexvaluebuff = volts / 2;
    12. finhexval = hexvaluebuff/0.61035;
    13. uint decimal = finhexval;
    14. hexadecimal.setNum(decimal,16);
    15.  
    16. firstByte = "0x"+hexadecimal.left(1);
    17. secondByte = "0x"+hexadecimal.mid(1, 2);
    18. sendFrame() ;
    19. // in send frame i am trying to send these 2 bytes. char i = port->write(firstByte);
    20. //char i = port->write(firstByte);
    21. //char i = port->write(secondByte);
    22. }
    To copy to clipboard, switch view to plain text mode 


    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.

    Qt Code:
    1. firstByte = "0x"+hexadecimaldata.left(2);
    2. secondByte = "0x"+hexadecimaldata.mid(1, 2);
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. static const char mydata[] = {0xAC,0XCA};
    2. QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
    3. char i = port->write(mydata,sizeof(mydata));
    To copy to clipboard, switch view to plain text mode 
    i tried so many diffrent things but was not sucessfull.
    thank you.
    Last edited by rex; 1st March 2011 at 16:21.

  7. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qext-serial-port write problem.

    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.
    Òscar Llarch i Galán

  8. #6
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: qext-serial-port write problem.

    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
    Qt Code:
    1. static const char mydata[] = {0xFF};
    2. QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
    3. char i = port->write(data,sizeof(mydata));
    To copy to clipboard, switch view to plain text mode 
    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

  9. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qext-serial-port write problem.

    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.
    Òscar Llarch i Galán

  10. The following user says thank you to ^NyAw^ for this useful post:

    rex (1st March 2011)

  11. #8
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: qext-serial-port write problem.

    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
    Last edited by rex; 1st March 2011 at 17:41.

  12. #9
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qext-serial-port write problem.

    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

  13. #10
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: qext-serial-port write problem.

    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..

    Qt Code:
    1. databuff.append(secondByte.toInt(0,16));
    2. char i = port->write(databuff);
    3. databuff.clear();
    To copy to clipboard, switch view to plain text mode 


    Regards

  14. #11
    Join Date
    Sep 2013
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Smile Re: qext-serial-port write problem.

    Quote Originally Posted by rex View Post
    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..

    Qt Code:
    1. databuff.append(secondByte.toInt(0,16));
    2. char i = port->write(databuff);
    3. databuff.clear();
    To copy to clipboard, switch view to plain text mode 


    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
    Qt Code:
    1. QString st="0200000748"+nambayacard.mid(0,2)+nambayacard.mid(2,2)+nambayacard.mid(4,2)+nambayacard.mid(6,2)+checksumdata+"03";
    2. QString stringhere="";
    3. for (int i = 0; i < st.length(); i++)
    4. {
    5. stringhere=st.mid(i,2);
    6. data.append(stringhere.toInt(0,16));
    7. i+=1;
    8. }
    9. port->write(data);
    To copy to clipboard, switch view to plain text mode 
    I have been working on it since Friday and now I am free
    Thank you and God bless you

  15. #12
    Join Date
    Oct 2011
    Posts
    48
    Thanks
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qext-serial-port write problem.

    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...
    Last edited by A9am; 9th December 2013 at 09:14.

Similar Threads

  1. Replies: 4
    Last Post: 10th July 2010, 17:34
  2. Serial Port Reading problem
    By sfabel in forum Qt Programming
    Replies: 12
    Last Post: 18th February 2010, 14:59
  3. Replies: 1
    Last Post: 16th June 2009, 09:09
  4. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 14:12
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.