LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Networking Labview and C++ Application

Hello,

 

 

I am trying to send some data between two computers, one uses an application built in LabVIEW and the other one has a C++ program (to be more precise, built with a framework called Qt).

 

I am currently using TCP and I managed to send a string from the C++ program to LabVIEW successfully, although the original string was something like this:

 

"This is a String"

 

LabVIEW received: " 4  & T h i s   i s   a   S t r i n g" (or similar)

 

But since the pattern seemed to be always the same, 6 "rubbish" characters and the rest of the characters with spaces inbetween, I manage to recover the data quite easily.

 

 

When I try the reverse, send message from LabVIEW to C++, I cannot even establish a connection using TCP.

 

 

Can anyone give me any pointers on how could I solve this problem?

 

Thank You

Davide

0 Kudos
Message 1 of 13
(3,769 Views)

Can you share some of your code?  When you say you cannot establish a TCP connection, what error do you get?  TCP is bi-directional, so if you can establish a connection (as you've apparently already demonstrated) then you can send a message in either direction over that connection.  How do you know how long the string is in order to read the correct number of characters?  In your C++ code, are you using a string (more specifically an array of char) or some higher-level string construct?  If it's a higher-level class, is it possible it's using something other than a byte to represent each character?  It would help a lot to see what you're doing.

0 Kudos
Message 2 of 13
(3,764 Views)
void MainWindow::on_connectButton_clicked()
{
    // connect to server on ip address on the port specified
    ipAddress = ui->ipString->text();
    portNumber = ui->portNumber->text().toInt();

    blockSize = 0;          // data to read
    newSocket->abort();

    // connect to server
    newSocket->connectToHost(ipAddress, portNumber);

    // wait 5 seconds, after send timeout signal
    if (!newSocket->waitForConnected(2000)) {
        // connection NOT successful
        ui->connectedLabel->setText("Connection failed");
        ui->messageReceived->setDisabled(true);
        ui->getMessageButton->setDisabled(true);

    } else {

        ui->messageReceived->setDisabled(false);
        ui->getMessageButton->setDisabled(false);

    }

}

void MainWindow::readFortune()
{
    // prepare to read the message
    QDataStream in(newSocket);
    in.setVersion(QDataStream::Qt_4_0);

    if (blockSize == 0) {

        if (newSocket->bytesAvailable() < (int)sizeof(quint16))
            return;

        in >> blockSize;
    }

    if (newSocket->bytesAvailable() < blockSize)
        return;

    in >> message;


    // receive the information in the text browser
    ui->messageReceived->append(message);
}

 

Hi, thanks for the quick response.

That is the C++ program relevant for making the connection, consider stuff like header files, opening TCP socket to be correctly implemented.

This program can receive data coming from another C++ program successfully.

I don't know whether you are familiar with Qt, but the connection is established when I call:

 

newSocket->connectToHost(ipAddress, portNumber);

 

This will wait 2000 milliseconds for the connection, after that will return a state of failure:

 

newSocket->waitForConnected(2000)

 

How I read the data is not relevant yet, because i cannot establish a connection, e.g. waitForConnected(2000) returns false.

I don't get any error, my program simply returns that.

 

At the moment I cannot post the vi that I'm using on LabVIEW, however I'm following the scheme:

 

Listen for incoming connections ===> Send data ===> Close connection

 

 

You talk about TCP being bi-directional, however right now I'm exploring a simple Client-Server approach, where the Server listens for incoming connection from any IP on a specific port, the Client connects to the Server on the specific port using it's IP and when the connection is established the Server send some Data to the Client.

 

About the string, I am using a QString. it's still a string, but it's the proper type of string to use since Qt does not implement some of the standard C++ string libraries such as stringstream (now that I think about it that can be the cause of the problem I mentioned on the first post).

I'm not sure whether a QString is simply an array of QChar (char in Qt).

I don't know how long is the string yet, but I can do what I did in reading data from LabVIEW, which is to read a fixed amount of data, and make sure that the string I'm sending is smaller than that data.

 

I'm not quite sure what you mean by:


If it's a higher-level class, is it possible it's using something other than a byte to represent each character?


 

However I'm using strings just because they seemed to be the easiest type of data to send around, as they don't require a TYPECAST before writing or reading from/to a socket.

0 Kudos
Message 3 of 13
(3,762 Views)

The string that you received appears to be a unicode string. The first couple of bytes indicate the type of unicode encoding and the remaining string is comprised of 16-bit values representing the characters.

 

Have you determined if you have any issues with firewalls that are blocking the traffic? The port you are trying to listen on may be blocked. It would be helpful to see the LabVIEW code but from what you described it seems pretty basic. Have you tried the shipping LabVIEW example of a simple server?



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 13
(3,756 Views)

Looks like Mark beat me to most of what I was about to write.  His comment about sending a Unicode string is what I meant by using something other than an array of bytes to represent a string - in this case, it's apparently 2 bytes per character instead of the normal 1 byte.

0 Kudos
Message 5 of 13
(3,755 Views)

Firewall may be an issue, but it does not give me any popups mentioning a firewall problem so I can't tell if that is the problem

I will post my LabVIEW code later, but it is pretty basic as you mentioned

0 Kudos
Message 6 of 13
(3,743 Views)

This is the vi that should connect to my client.

When the connection is established, it should send the message in the control, the problem is that it never connects.

The vi is very similar to the example vi.

 

I can't see anything wrong with it, do I need to add something?

 

 

I would like to add that the ClientSide.vi can receive the data perfectly even on another maching, so I guess that it's not a matter of firewall.

0 Kudos
Message 7 of 13
(3,730 Views)

Have you run this VI with execution highlighting enabled (the light bulb in the toolbar) to see what it's doing?  Checking for error 56 is not sufficient - there could be some other error code.  Also, once you do get a connection established, you'll again have the problem of mismatched string encodings - LabVIEW will send an ASCII string, but it seems your C++ code expects a Unicode string.

0 Kudos
Message 8 of 13
(3,706 Views)

I have implemented several forms of IPC with QT and LV but had not used TCP yet.  As a quick test here is a quick and dirty server than can be used in conjunction with the Fortune Client example.

 

Run the simple server in the snippet below, I use 1729 as the default port since it is a very interesting number, pick any one you want.  Most of the code is simply dealing with the expected format of the QByteArray.

 

In QT, build the fortuneclient example and run it.  Assuming you are testing with the localhost, simply type the port number from the LV server into the box.  Push the Get Fortune button a few times.

 


SimpleFortuneServer.png

 

 

Message 9 of 13
(3,695 Views)

I did, it only gives me ERROR 56.

 

I think I understood what the problem is.

I tried to make the vi into an executable, when I run the executable a dialog from Windows firewall pops up, so I guess that is a firewall issue.

It's very weird by the way, because the firewall pops up when I start to listen for incoming connections, not when my C++ program tries to connect to the LabVIEW vi.

 

I don't really know a whole lot about firewalls but it does not make too much sense to me, because when I run the vi within the LabVIEW environment there is no firewall dialog. 😞

 

 

 

@Darin.K

Thank you for your effort, I'll try your vi on monday when I go back to work 🙂

0 Kudos
Message 10 of 13
(3,692 Views)