How to check Ethernet port connection status in Qt? - qt

I am new to Qt .Actually, i want to check Whether the Ethernet is connected or not through qt application.
For Example: when the Data is transfer from source to Destination through Ethernet. if i suddenly, remove the Ethernet cable my Qt application has to give some pop-up message like "the connection is unavailable". Is there any way to find this one? Finally,Sorry for my English.

You might want to consider the Qt "Bearer Management" API:
http://doc.qt.io/qt-5/bearer-management.html
For example:
/ Set Internet Access Point
QNetworkConfigurationManager manager;
const bool canStartIAP = (manager.capabilities()
& QNetworkConfigurationManager::CanStartAndStopInterfaces);
// Is there default access point, use it
QNetworkConfiguration cfg = manager.defaultConfiguration();
if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
QMessageBox::information(this, tr("Network"), tr(
"No Access Point found."));
return;
}

If you are on Linux, I found a nice idea somewhere to check with QT and QProcess if the Ethernet Cable is physically connected.
QProcess process;
QString sNetCableStatus = "";
process.start("sh", QStringList()<<"-c"<<"grep \"\" /sys/class/net/eth0/carrier");
process.waitForFinished();
sNetCableStatus = process.readAllStandardOutput();
if (sNetCableStatus.length() != 0)
{
if (sNetCableStatus.left(1) == "1")
{
//connected
}
else if(sNetCableStatus.left(1) == "0")
{
//disconnected
}
else
{
//that should not happen
}
}

Related

How to use USB to RS232 communication with Android Things

I am using MAX232 device for UART(Raspberry Pi) to RS232 communication(Other Board).
But I want to use USB to Serial cable for this. I have attached the image of a cable. How can I use this in Android Things?
Can anyone tell this.
You can use libraries like this or that. And in case of FTDI-based USB<->UART converters you can use FTDI's solution for Android like d2xx driver with Java wrapper. And you can find many examples (like this) of it's use:
...
private static D2xxManager ftD2xx = null;
private FT_Device ftDev;
...
try {
ftD2xx = D2xxManager.getInstance(this);
int devCount = 0;
devCount = ftD2xx.createDeviceInfoList(this);
Log.d(TAG, "Device number : "+ Integer.toString(devCount));
D2xxManager.FtDeviceInfoListNode[] deviceList = new D2xxManager.FtDeviceInfoListNode[devCount];
ftD2xx.getDeviceInfoList(devCount, deviceList);
if(devCount <= 0) {
return;
}
if(ftDev == null) {
ftDev = ftD2xx.openByIndex(this, 0);
} else {
synchronized (ftDev) {
ftDev = ftD2xx.openByIndex(this, 0);
}
}
...
} catch (D2xxManager.D2xxException ex) {
Log.e(TAG,ex.toString());
}

MS Media Foundation - can't get IMFTransform interface to a H264 Encoder MFT object?

I'm getting started with MS Media Foundation and so I just entered in the code from Tutorial: Encoding an MP4 File. This uses the Source Resolver to create a media source and the MFCreateTranscodeTopology() function to create a topology including an H.264 encoder, as described in the reference.
Then I wanted to test my new understanding of the Media Foundation structures by analyzing the topology. I created the following function which I call from the example code immediately before their call to StartEncodingSession() in the tutorial.
HRESULT GetTopologyInfo(IMFTopology *pTopology)
{
HRESULT hr = 0;
WORD num_nodes = 0;
hr = pTopology->GetNodeCount(&num_nodes);
if (SUCCEEDED(hr))
{
for (WORD i = 0; i < num_nodes; i++)
{
IMFTopologyNode *pNode = NULL;
IUnknown *pNodeObject = NULL;
IMFAttributes *pAttribute = NULL;
IMFTransform *pTransform = NULL;
MF_TOPOLOGY_TYPE type;
hr = pTopology->GetNode(i, &pNode);
if (SUCCEEDED(hr))
{
hr = pNode->GetNodeType(&type);
// Get the node object's pointer.
hr = pNode->GetObject(&pNodeObject);
if (SUCCEEDED(hr))
{
hr = pNodeObject->QueryInterface(IID_PPV_ARGS(&pAttribute));
if (SUCCEEDED(hr))
{
GUID guid;
LPWSTR szGuid = NULL;
LPWSTR szFriendlyName = NULL;
hr = pAttribute->GetGUID(MFT_TRANSFORM_CLSID_Attribute, &guid);
if (SUCCEEDED(hr))
{
hr = StringFromIID(guid, &szGuid);
std::wcout << szGuid << std::endl;
}
hr = pAttribute->GetAllocatedString(MFT_FRIENDLY_NAME_Attribute, &szFriendlyName, NULL);
if (SUCCEEDED(hr))
{
std::wcout << szFriendlyName << std::endl;
}
}
hr = pNodeObject->QueryInterface(IID_PPV_ARGS(&pTransform));
if (SUCCEEDED(hr))
{
std::cout << "got transform interface" << std::endl;
}
else
{
std::cout << std::hex << hr << std::endl;
}
}
}
}
}
return hr;
}
This function finds 3 nodes in the topology: a MF_TOPOLOGY_SOURCESTREAM_NODE, a MF_TOPOLOGY_TRANSFORM_NODE, and a MF_TOPOLOGY_OUTPUT_NODE.
This function raises a lot of questions about things that don't seem to work correctly.
Foremost,
The QueryInterface() call to get the IMFTransform interface on the middle node (which has type MF_TOPOLOGY_TRANSFORM_NODE) fails with error E_NOINTERFACE. Yet, when I query the CLSID and name of the transform from the IMFAttributes interface I get "H264 Encoder MFT". This transform is supposed to expose the IMFTransform interface per H.264 Video Encoder documentation. Can anyone see what I'm doing wrong?
Further questions:
the output from this function is
{6CA50344-051A-4DED-9779-A43305165E35}
H264 Encoder MFT
80004002
80004002
The call to GetObject fails for the first node (which has type MF_TOPOLOGY_SOURCESTREAM_NODE). How can a topology node fail to return a node object?
The QueryInterface() call to get the IMFAttributes interface also fails on the third node (which has type MF_TOPOLOGY_OUTPUT_NODE). Shouldn't every node expose the IMFAttributes interface?
Where is the H.264 decoder in this topology? The source file is an MP4 file with a single H.264 video elementary stream
Asking generally, are there any good references that explain Media Foundation? The online resources don't seem to tell the complete story. So far Media Foundation seems opaque to me. Thanks for any help you can offer.
Using:
Visual Studio 2017 (v141) (but same behavior with Visual Studio 2010)
Windows SDK Version 10.0.17134.0
Windows 7 Home Premium, Service Pack 1
You should analyze the topology only after MESessionTopologySet is received in the Invoke handler.
This event is sent when the full topology is created by the Media Session after a call to IMFMediaSession::SetTopology

I want my multihopOscilloscope to send data through radio and Serial port as well

I am trying to modify the multihop Oscilloscope program so that the sink node is able to send data both to UART and radio medium as well. As far as researched, I found out that the same hardware is used for sending packets via UART and radio too.
In this case, how do I modify my code so that I can send data to UART or radio based on a condition I receive. Here in the sample prorgram, I send data via radio on every 10 packets received.
The receive module for my sink node is:
event message_t* Receive.receive(message_t* msg, void *payload, uint8_t len) {
oscilloscope_t* in = (oscilloscope_t*)payload;
counter++;
am_addr_t rec = call AMPacket.source(msg);
oscilloscope_t* out;
counter++;
call Leds.led0On();
if (uartbusy == FALSE) {
out = (oscilloscope_t*)call SerialSend.getPayload(&uartbuf, sizeof(oscilloscope_t));
if (len != sizeof(oscilloscope_t) || out == NULL) {
return msg;
}
else {
memcpy(out, in, sizeof(oscilloscope_t));
}
uartlen = sizeof(oscilloscope_t);
post uartSendTask();
} else {
message_t *newmsg = call UARTMessagePool.get();
if (newmsg == NULL) {
report_problem();
return msg;
}
//Serial port busy, so enqueue.
out = (oscilloscope_t*)call SerialSend.getPayload(newmsg, sizeof(oscilloscope_t));
if (out == NULL) {
return msg;
}
memcpy(out, in, sizeof(oscilloscope_t));
if (call UARTQueue.enqueue(newmsg) != SUCCESS) {
call UARTMessagePool.put(newmsg);
fatal_problem();
return msg;
}
}
if(counter % 10 == 0){
oscilloscope_t* btrpkt = (oscilloscope_t*)(call Packet.getPayload(&pkt, sizeof(oscilloscope_t)));
call Leds.led1On();
if (call AMSend.send(rec, &pkt, sizeof(oscilloscope_t)) == SUCCESS) {
call Leds.led0On();
sendbusy = TRUE;
}
}
return msg;
}
Once the data sends back to the node from where it received the packet , it is unable to process it through UART again. Could anyone help me how could I solve my problem?
According to the question and comments:
You must instantiate AMSenderC with the same id as for the receiver. In this case, AM_OSCILLOSCOPE if you want a message to be processed by the same code. Or another id plus a new implementation of the Receive interface.
You missed putting payload into btrpkt.
You must check for sendbusy - it is a bug if you try to use the radio stack when it is busy.

DDE connection failing for unknown reasons

I'm trying to create and implement a DDE dll with Qt but as for now I'm being unable to properly connect to a service which I know to be working after testing it with Excel.
The dll connection function is as following:
UINT respTemp;
respTemp = DdeInitializeA(&pidInst, NULL, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0L);
//handle error messages here
//...
//![]
hszService = DdeCreateStringHandleA(pidInst, (LPCSTR)service.utf16(), CP_WINANSI); //service.toLatin1().toStdString().c_str()
hszTopic = DdeCreateStringHandleA(pidInst, (LPCSTR)topic.utf16(), CP_WINANSI); //topic.toLatin1().toStdString().c_str()
hConv = DdeConnect(pidInst, hszService, hszTopic, NULL);
DdeFreeStringHandle(pidInst, hszService);
DdeFreeStringHandle(pidInst, hszTopic);
if (!hConv)
{
UINT ddeLastError = DdeGetLastError(pidInst);
switch (ddeLastError)
{
case DMLERR_DLL_NOT_INITIALIZED: return DDEConn_DLLNotInitialized;
case DMLERR_INVALIDPARAMETER: return DDEConn_InvalidParameter;
case DMLERR_NO_CONV_ESTABLISHED: return DDEConn_NoConvEstablished;
default: return DDEConn_NoConnectionStablished;
}
}
connStatus = true;
return DDEConn_NoError;
The test function is as follows:
void MainWindow::on_start_clicked()
{
const QString application = "profitchart"; //=profitchart|COT!VALE5.ult
const QString topic = "COT";
const QString item = "VALE5.ult";
test = CommDDE::instance();
CommDDE::DDEConnectionErrorList resp = test->connect(application,topic);
if (resp == CommDDE::DDEConn_NoError)
{
qDebug() << "request RESULT: " << test->request(item);
}
else
qDebug() << "Can't connect to application" << resp;
}
Always when I try to connect I get error DMLERR_NO_CONV_ESTABLISHED after the call to DdeConnect. I couldn't find guidence on what to do when such error occurs. I don't know too much about the details of configuring such functions so I used the default configuration used by a working dll from which I got part of the raw material for this dll. Should I try a different configuration I'm not aware of? Remembering that the call is working on Excel.
It would seem I found the answer: the commented way of writting the service and topic names were the right ways of passing the parameters to DdeCreateStringHandleA and DdeCreateStringHandleA.

LwIP Netconn API + FreeRTOS TCP Client Buffer Issue

I've been trying to modify the tcp server example with LwIP in STM32F4DISCOVERY board. I have to write a sender which does not necessarily have to reply server responses. It can send data with 100 ms frequency, for example.
Firstly, the example of TCP server is like this:
static void tcpecho_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL) {
/* Bind connection to well known port number 7. */
err = netconn_bind(conn, NULL, DEST_PORT);
if (err == ERR_OK) {
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1) {
/* Grab new connection. */
newconn = netconn_accept(conn);
/* Process the new connection. */
if (newconn) {
struct netbuf *buf;
void *data;
u16_t len;
while ((buf = netconn_recv(newconn)) != NULL) {
do {
netbuf_data(buf, &data, &len);
//Incoming package
.....
//Check for data
if (DATA IS CORRECT)
{
//Reply
data = "OK";
len = 2;
netconn_write(newconn, data, len, NETCONN_COPY);
}
} while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
} else {
printf(" can not bind TCP netconn");
}
} else {
printf("can not create TCP netconn");
}
}
I modified this code to obtain a client version, this is what I've got so far:
static void tcpecho_thread(void *arg)
{
struct netconn *xNetConn = NULL;
struct ip_addr local_ip;
struct ip_addr remote_ip;
int rc1, rc2;
struct netbuf *Gonderilen_Buf = NULL;
struct netbuf *gonderilen_buf = NULL;
void *b_data;
u16_t b_len;
IP4_ADDR( &local_ip, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3 );
IP4_ADDR( &remote_ip, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
xNetConn = netconn_new ( NETCONN_TCP );
rc1 = netconn_bind ( xNetConn, &local_ip, DEST_PORT );
rc2 = netconn_connect ( xNetConn, &remote_ip, DEST_PORT );
b_data = "+24C"; // Data to be send
b_len = sizeof ( b_data );
while(1)
{
if ( rc1 == ERR_OK )
{
// If button pressed, send data "+24C" to server
if (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET)
{
Buf = netbuf_new();
netbuf_alloc(Buf, 4); // 4 bytes of buffer
Buf->p->payload = "+24C";
Buf->p->len = 4;
netconn_write(xNetConn, Buf->p->payload, b_len, NETCONN_COPY);
vTaskDelay(100); // To see the result easily in Comm Operator
netbuf_delete(Buf);
}
}
if ( rc1 != ERR_OK || rc2 != ERR_OK )
{
netconn_delete ( xNetConn );
}
}
}
While the writing operation works, netconn_write sends what's on its buffer. It doesnt care whether b_data is NULL or not. I've tested it by adding the line b_data = NULL;
So the resulting output in Comm Operator is like this:
Rec:(02:47:27)+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C+24C
However, I want it to work like this:
Rec:(02:47:22)+24C
Rec:(02:47:27)+24C
Rec:(02:57:12)+24C
Rec:(02:58:41)+24C
The desired write operation happens when I wait for around 8 seconds before I push the button again.
Since netconn_write function does not allow writing to a buffer, I'm not able to clear it. And netconn_send is only allowed for UDP connections.
I need some guidance to understand the problem and to generate a solution for it.
Any help will be greately appreciated.
It's just a matter of printing the result in the correct way.
You can try to add this part of code before writing in the netbuf data structure:
char buffer[20];
sprintf(buffer,"24+ \n");
Buf->p->payload = "+24C";
I see one or two problems in your code, depending on what you want it exactly to do. First of all, you're not sending b_data at all, but a constant string:
b_data = "+24C"; // Data to be send
and then
Buf->p->payload = "+24C";
Buf->p->len = 4;
netconn_write(xNetConn, Buf->p->payload, b_len, NETCONN_COPY);
b_data is not anywhere mentioned there. What is sent is the payload. Try Buf->p->payload = b_data; if it's what you want to achieve.
Second, if you want the +24C text to be sent only once when you push the button, you'll have to have a loop to wait for the button to open again before continuing the loop, or it will send +24C continuously until you stop pushing the button. Something in this direction:
while (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_0) == Bit_SET) {
vTaskDelay(1);
}

Resources