Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 25, 2024, 02:02:28 14:02


Login with username, password and session length


Pages: [1]
Print
Author Topic: [HELP] Serial port reading from windows gui  (Read 3892 times)
0 Members and 1 Guest are viewing this topic.
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« on: March 10, 2017, 02:43:42 02:43 »

Hello everyone,

I am developing a gui program with Qt c++ framework. I have a trouble it makes me mad. I could not any solution on www. Problem is that:

when I try to read data with Labview from my arm device, there is no problem. But in my gui program, i have a frame drifting error. For example suppose that, text sended from device is "testData"...

received data is looks like that,

testData
testData
te
stData
testData
Datatest
tatestDa
..
..

I could not find any solution on the web. In qt examples, there is no different coding apart from mine. If someone wants, I can add codes but it is simple serial comm codes...

I thought that also, my gui program a bit larger now. About 1000 line of codes. Maybe, do i need async programming? Just an idea... 

(I could not decide where I should open topic, computer or electronics. Sorry administrators)
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #1 on: March 10, 2017, 04:49:26 04:49 »

you didn't tell us how you are doing framing!

do you frame with line feed or carriage return?  are you framing by packet length? are you framing by packet time?
Logged
sam_des
Senior Member
****
Offline Offline

Posts: 253

Thank You
-Given: 124
-Receive: 146


« Reply #2 on: March 10, 2017, 06:32:45 06:32 »

Hi,
I think newer Qt (v5.x I guess) includes serial comm QSerialPort class built-in. Or you may use QSerialDevice class with Qt v4.x. Both have quite a few examples.
Windows does not respond to serial port in real time, especially if your're using a USB Virtual Comm Port. Its better to use Asynchronous &/or hardware or software flow control, so as not to miss data &/or under/overrun your buffers.

From what you're receiving, it seems your code isn't parsing end of frame(most probably CR/LF) correctly.
And yes I agree with Gallymimu, you haven't given enough info about your frames.

sam_des
Logged

Never be afraid to do something new. Remember Amateurs built the Ark, Professionals built the Titanic !
mars01
V.I.P
Hero Member
*****
Offline Offline

Posts: 536

Thank You
-Given: 693
-Receive: 1763



« Reply #3 on: March 10, 2017, 11:26:38 11:26 »

Hi,

There may be issues at the drivers/hardware stage.
I remember reading in the GRBL webpage (open source CNC firmware) that they saw problems (truncated transmissions) with the CH340 chinese serial-to-USB IC when working at a certain baudrate. Try to lower the baudrate.
Other IC's may have similar issues.

« Last Edit: March 10, 2017, 02:27:36 14:27 by mars01 » Logged
towlerg
Senior Member
****
Offline Offline

Posts: 263

Thank You
-Given: 474
-Receive: 104

What is this for?


« Reply #4 on: March 10, 2017, 04:41:33 16:41 »

I've had success using fixed length packets and WaitCommEvent (to detect the first character) in a seperate thread.I used fixed length because that allow you to specify the number of characters in the ReadFile rather than rely on time outs. 
 
Try to get a copy of The Windows Serial Port Programming Handbook by Ying Bai.
Logged

Win 7 Ult x64 SP1 on HP2570p
mars01
V.I.P
Hero Member
*****
Offline Offline

Posts: 536

Thank You
-Given: 693
-Receive: 1763



« Reply #5 on: March 10, 2017, 05:02:31 17:02 »

@towlerg, thanks for the book tip. I downloaded the book (first hit on a google search) and it seems interesting.
Logged
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« Reply #6 on: March 10, 2017, 08:32:06 20:32 »

you didn't tell us how you are doing framing!

do you frame with line feed or carriage return?  are you framing by packet length? are you framing by packet time?

this is my device code. I use cortex-m4 based mcu -> ft232rl

Code:
//uart send function
void UARTSend(uint32_t ui32UARTBase, const uint8_t *pui8Buffer, uint32_t ui32Count)
{
    while(ui32Count--)
    {
        UARTCharPut(ui32UARTBase, *pui8Buffer++);

while(!UARTBusy(ui32UARTBase))
{

}
    }
}
...

//program task
void progTask()
{

DelayMsec(100); //my milisecond delay func

//read value from adc13
Adc13Read(adcValue.ui32Part); //my adc read funct, keep adc result in adcValue union

sendData[0] = 'a';
sendData[1] = adcValue.bytes[0];
sendData[2] = (adcValue.bytes[1] & 15);
sendData[3] = 'b';

//send test data
UARTSend(UART6_BASE,sendData , 4);
}

int main()
{
    progTask();
}


This code works without any issue in labview. labview always reads data like "axxb". But in qtserialprogram like this
(these are real results)

ÿb
aÿb
aÿ
b
aÿb
aÿb
aÿb
aÿb
a
ÿb
aÿb
aÿb
aÿb
aÿb
aÿb
aÿb
aûb
aûb
aÿb

Dear Gallymimu,

I thought that after your comment, i dont know somethings about framing and Labview makes these stuffs instead of me. Am I right???


Also this is my device settings,

Code:
    ROM_UARTConfigSetExpClk(UART6_BASE,systemClock , baud,
                            (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE|
                             UART_CONFIG_PAR_NONE));

it is same at the computer program.


Posted on: March 10, 2017, 09:21:50 21:21 - Automerged

I've had success using fixed length packets and WaitCommEvent (to detect the first character) in a seperate thread.I used fixed length because that allow you to specify the number of characters in the ReadFile rather than rely on time outs. 
 
Try to get a copy of The Windows Serial Port Programming Handbook by Ying Bai.

I dont wait for reading data. QT's signal slot mechanism allow me that:

connect(serial,SIGNAL(readyread()),this,SLOT(readData)));

system invokes the readData func. and i read all of data in that functin like:

serial->setReadBufferSize(4);
QByteArray serData = serial->readAll();

But, although this mechanism, i think multithreading. So, my class a bit larger. I think it needs multithreading because it is doing some stuffs when data is came.
Logged
PICker
Active Member
***
Offline Offline

Posts: 162

Thank You
-Given: 207
-Receive: 109


« Reply #7 on: March 10, 2017, 08:52:11 20:52 »

You can find some examples of QT SIGNAL/SLOT (for serial communicaton use) by reading the source code of ScriptCommunicator (https://sourceforge.net/projects/scriptcommunicator/)
Logged
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« Reply #8 on: March 10, 2017, 08:56:20 20:56 »

You can find some examples of QT SIGNAL/SLOT (for serial communicaton use) by reading the source code of ScriptCommunicator (https://sourceforge.net/projects/scriptcommunicator/)

It looks great. Thanks.
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #9 on: March 10, 2017, 08:57:54 20:57 »

you really must have some method of framing.  labview may help you parse things, but you are defining the framing when you write the transmission protocol.
Logged
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« Reply #10 on: March 10, 2017, 09:21:01 21:21 »

you really must have some method of framing.  labview may help you parse things, but you are defining the framing when you write the transmission protocol.

Can you give me any book, documents etc for this topic? I am not sure if google gives me right sources.
Logged
Sideshow Bob
Cracking Team
Hero Member
****
Offline Offline

Posts: 985

Thank You
-Given: 230
-Receive: 961



« Reply #11 on: March 10, 2017, 09:41:19 21:41 »

What Labview do for you is to set up a buffer that works in the background. You will always be pulling serial data from this buffer in Labview. Not the serial port directly. I think in labview this buffer will be set to 1K even if no buffer size is defined. What I think may be going on here is is some sort of buffer overrun in your serial port communicating. Your serial port communicating class should also have functions that can check for errors at UART level. Even if you are using a USB Virtual Comm Port this would be true.
Logged

I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« Reply #12 on: March 10, 2017, 09:55:52 21:55 »

What Labview do for you is to set up a buffer that works in the background. You will always be pulling serial data from this buffer in Labview. Not the serial port directly. I think in labview this buffer will be set to 1K even if no buffer size is defined. What I think may be going on here is is some sort of buffer overrun in your serial port communicating. Your serial port communicating class should also have functions that can check for errors at UART level. Even if you are using a USB Virtual Comm Port this would be true.

Alright, if i work with virtual usb serial port on usb protocol(usb cdc class), will the frame errors decrease? If this will be true, I will desing a new board without using ft232rl.
Logged
biomed12
Junior Member
**
Offline Offline

Posts: 94

Thank You
-Given: 67
-Receive: 5


« Reply #13 on: March 11, 2017, 02:53:34 02:53 »

Hello everyone. I coded frames like below and everything works great.

device side:
Code:
	sendData[0] = (char)'a';
sendData[1] = (char)'k';
sendData[2] = adcValue.bytes[0];
sendData[3] = (adcValue.bytes[1] & 15);

c++ side:
Code:
 if(serial->bytesAvailable()<4)
    {
            //qDebug()<<QString::number(serial->bytesAvailable());
        //
        serial->clear();
        if(serial->bytesAvailable() == 0)
        {
            ui->dataWriter->append("there is no data!");
            //serial->clear();
        }

        return;
    }

    QByteArray serialInData = serial->readAll();

    //my algorithm
    if(serialInData[0] == 'a' && serialInData[1] == 'k')
    {
        serData.bytes[0] = serialInData[2];
        serData.bytes[1] = serialInData[3];

    }else if(serialInData[2] == 'a' && serialInData[3] == 'k')
    {
        serData.bytes[0] = serialInData[0];
        serData.bytes[1] = serialInData[1];
    }
    else if(serialInData[1] == 'a' && serialInData[2] == 'k')
    {
        serial->read(1);
        return;
    }else if(serialInData[0] == 'k' && serialInData[3] == 'a')
    {
        serData.bytes[0] = serialInData[1];
        serData.bytes[1] = serialInData[2];

    }
}

I think, I will need multithreading for high sampling frequencies.
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #14 on: March 11, 2017, 04:32:19 16:32 »

Can you give me any book, documents etc for this topic? I am not sure if google gives me right sources.

http://eli.thegreenplace.net/2009/08/12/framing-in-serial-communications/
Logged
Signal
Active Member
***
Offline Offline

Posts: 197

Thank You
-Given: 111
-Receive: 81



« Reply #15 on: March 11, 2017, 09:29:18 21:29 »

I think, I will need multithreading for high sampling frequencies.
I'm not an expert in Qt framework at all but I am pretty sure, you already have multithreading as the callback function readData() is called from another Qt thread. So do not forget about multithreading synchronization between callback function and main thread. I'd start from reading a Qt manual for correct usage examples of connect(serial,...).

Logged

Give a right name to a right game and play it right
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC