The Godfather talking
You can run, but you can't hide.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2017, 09:38:44 21:38


Login with username, password and session length


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

Posts: 82

Thank You
-Given: 58
-Receive: 4


« on: March 10, 2017, 03:43:42 03: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: 631

Thank You
-Given: 127
-Receive: 167


« Reply #1 on: March 10, 2017, 05:49:26 05: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
Active Member
***
Offline Offline

Posts: 224

Thank You
-Given: 52
-Receive: 111


« Reply #2 on: March 10, 2017, 07:32:45 07: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
Senior Member
*****
Offline Offline

Posts: 292

Thank You
-Given: 352
-Receive: 656



« Reply #3 on: March 10, 2017, 12:26:38 12: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, 03:27:36 15:27 by mars01 » Logged
towlerg
Active Member
***
Offline Offline

Posts: 166

Thank You
-Given: 326
-Receive: 56

What is this for?


« Reply #4 on: March 10, 2017, 05:41:33 17: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 x86 SP1 on HP2570p
mars01
V.I.P
Senior Member
*****
Offline Offline

Posts: 292

Thank You
-Given: 352
-Receive: 656



« Reply #5 on: March 10, 2017, 06:02:31 18: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: 82

Thank You
-Given: 58
-Receive: 4


« Reply #6 on: March 10, 2017, 09:32:06 21: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
ab
a
b
ab
ab
ab
ab
a
b
ab
ab
ab
ab
ab
ab
ab
ab
ab

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
Junior Member
**
Offline Offline

Posts: 93

Thank You
-Given: 64
-Receive: 50


« Reply #7 on: March 10, 2017, 09:52:11 21: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: 82

Thank You
-Given: 58
-Receive: 4


« Reply #8 on: March 10, 2017, 09:56:20 21: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: 631

Thank You
-Given: 127
-Receive: 167


« Reply #9 on: March 10, 2017, 09:57:54 21: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: 82

Thank You
-Given: 58
-Receive: 4


« Reply #10 on: March 10, 2017, 10:21:01 22: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: 545

Thank You
-Given: 189
-Receive: 555



« Reply #11 on: March 10, 2017, 10:41:19 22: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 really am ruggedly handsome, aren't I?
biomed12
Junior Member
**
Offline Offline

Posts: 82

Thank You
-Given: 58
-Receive: 4


« Reply #12 on: March 10, 2017, 10:55:52 22: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: 82

Thank You
-Given: 58
-Receive: 4


« Reply #13 on: March 11, 2017, 03:53:34 03: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: 631

Thank You
-Given: 127
-Receive: 167


« Reply #14 on: March 11, 2017, 05:32:19 17: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: 120

Thank You
-Given: 56
-Receive: 43


« Reply #15 on: March 11, 2017, 10:29:18 22: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