Unable to send data using QSerialPort - qt

I am trying to write a QT application that will be able to communicate with my embedded system using serial port. For now I am testing the config with Null Modem emulator as I don't have the embedded system ready yet.
The emulator works fine as I have tested it in other software like Terraterm, RealTerm or Putty. My problem is that my example code doesn't work - it doesn't send the string to another com port.
This is my code:
void CSettingsDialog::on_pbSerialCheck_clicked()
{
QSerialPort serial;
serial.setPortName(ui->cbSerialPort->currentText());
if (!serial.open(QIODevice::ReadWrite))
{
QMessageBox::warning(this, tr("Serial port"),
tr("Serial port %1 is busy!")
.arg(ui->cbSerialPort->currentText()));
}
else
{
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
serial.write("TEST\n");
serial.close();
}
}
I am certain that I have everything set correctly looking at the QT examples.

I am certain that I have everything set correctly looking at the QT
examples. I would aprichiate all help!
When reading flush documentation, it seems that calling "write" buffers, with writes possible finishing asynchronously.
Also see "Certain subclasses of...are asynchronous", with comments on waitForBytesWritten

Related

MPLab Harmony CAN communication failing

I'm using MPLab Harmony to program a custom board based on ATSAME51J20A microprocessor from microchip.
I didn't find any Arduino library that would enable me to use the two CAN bus available on this chip, so I started looking into MPLab.
I've used the code generator that created me functions to use CAN0 and CAN1.
The problem comes when I try to send CAN messages:
The initialization works well. Then I call "CAN1_MessageTransmitFifo(...)" which returns true (no error) but no message arrives at the other end point.
The only error I can get is when I debug the program and look at the data returned by CAN1_ErrorGet(), it returns 0x707 which seems to be the bits indicating "CAN_PSR_LEC_NC_Val: (CAN_PSR) No Change" and I have no clue what it means.
Note that I was able to successfully use the CAN1 by using the adafruit library CAN.h. So on this bus we can reject the possibility of a hardware problem.
Below you will find screenshots of my configuration:
The project graph
Pin diagram
In the configuration options section I also get these weird warnings (for CAN0 and CAN1). I've highlighted them.
Configuration options
Here is the code sending the CAN message (after initialization):
CAN_TX_BUFFER msg = {0};
msg.id = 0xAB;
msg.data[0] = 'H';
CAN1_MessageTransmitFifo(1, &msg);
I'm sure I'm sending and receiving at the same speed (500KB/s) and on the logic analyser I only get garbage on the CAN lines (lots of random spikes).
Am I missing something ?
If you have a solution that doesn't use MPLab but permits the use of the two CAN bus and the two I2C I'm very interested.
And if you know what could cause the issue, please let me know.
Thank you in advance.

lwip netconn_recv netbuf overwritten from browser broadcast message

I have a project on a STM32H7 board with lwip 2.1.2 and freeRTOS V10.3.1 with a custom tcp server application running. So far I assumed that, when using netconn_recv(), the netbuf data are valid until the next netconn_recv() command. But sometimes it happens that an incoming BROWSER protocol broadcast message overwrites my rxbuffer and my application ends up with an error message, due to the corrupt data in the rxbuffer.
The error dos not occur, when I have wireshark running on the PC. But I can see artifacts of the broadcast message in the rx buffer when inspecting with the debugger.
My server loop is quite simple
while (running) {
errRet = netconn_recv(current_conn, &rxbuf);
if (errRet != ERR_OK) {
break;
}
do {
netbuf_data(rxbuf, (void*) &rxdata, &rxlen);
if (netconn_receive (current_conn, rxdata, rxlen) != ERR_OK)
break;
} while (netbuf_next(rxbuf) >= 0);
}
I enabled the stack and heap overflow hook function, but there seems all ok.
I found it out. I forgot to mention that my project is based on this STMCubeH7 repository application. So far I've been working with version 1.9.1, but now there's a newer version, v1.10.0. I did an update and it seems to work now.
By the way, I found the repository log message: "Full rework of Ethernet HAL driver".

Handle BLE user event on FreeRTOS

I'm running FreeRTOS on a ST Nucleo board with the BlueNRG shield.
The initialization of the BLE stack works, the devices advertises itself and I can establish a connection. My problem is that as soon as user event arrives (e.g. service discovery) the program ends up in the hardfault_handler().
I have 3 tasks running on my RTOS of which one should be a dedicated BLE task handling the user events.
void hci_user_evt_proc(void)
{
tHciDataPacket * hciReadPacket = NULL;
/* process any pending events read */
while (list_is_empty(&hciReadPktRxQueue) == FALSE)
{
list_remove_head (&hciReadPktRxQueue, (tListNode **)&hciReadPacket);
if (hciContext.UserEvtRx != NULL)
{
hciContext.UserEvtRx(hciReadPacket->dataBuff);
}
list_insert_tail(&hciReadPktPool, (tListNode *)hciReadPacket);
}
}
This is taken from an ST example code.
The whole thing works if I either
Just run this one task or
Give the BLE task a higher priority
Both solutions have the same outcome - and don't have a multitasking system anymore.
I don't know if a have to adapt the example code to run in a multitask system or if I have to run the BLE process interrupt driven, but if yes, how would I do that and how can I elaborate the root cause of a hardfault?
What I tried is to surround my user_evt_handler with a vTaskSuspendAll/xTaskResumeAll but that didn't change anything.
Issue solved by giving the BLE task more stack size

Detecting Serial port disconnect in Chrome App

I'm using Chrome's Serial Port API (http://developer.chrome.com/apps/serial.html) in a Web App.
The problem I have is that pretty much all serial ports now are implemented via USB devices. If the user disconnects (or just resets) the USB device, I don't have any way of knowing. Not only that, because the app hasn't disconnected in Chrome (because it didn't know), if the USB device is plugged back in, bad things happen (on Linux it just gets a different name, but in Windows it is not usable at all).
The best I can manage is:
var checkConnection = function() {
chrome.serial.getControlSignals(connectionInfo.connectionId, function (sigs) {
var connected = "cts" in sigs;
if (!connected) console.log("Disconnected");
});
} // called every second or so
Is there a better way? a callback would be ideal!
It looks it should be safe on all platforms to assume that getting a read callback with 0 bytes means EOF, which in turn is a good indication that the device has been disconnected.
chrome.serial.read(connectionId, function(readInfo) {
if (readInfo.bytesRead === 0) {
/// Safely assume the device is gone. Clean up.
chrome.serial.close(connectionId);
/// ...
}
});
The serial API will be improving over the next few weeks (in Canary at least) to add stability improvements, an event-based read API, and the ability to more clearly detect timeouts and error conditions (like a disconnected device). You can track that progress at http://crbug.com/307184.

Re-opening Closed Serial Port

i have an application that detects a USB 3G Dongle that will be used for sending SMS. My application queries the Dongle via AT Commands to determine if it is the RIGHT dongle, this means that that certain dongle can only be used in my application (even if the Dongle is of the same model). Sending and receiving is fine, no problems or whatsoever. If the 3G Dongle is removed from the USB port, the system detects this and executes the proper procedures.
Here's my problem. When the 3G Dongle is re-inserted, say on the same port (COM5), my application detects this and executes some AT Command to determine that the re-inserted dongle is the RIGHT dongle. But an error occurs stating:
THE RESOURCE IS IN USE
The application must be terminated or closed to be able to use the same port (say COM5). Then I encountered an application, almost with the same function, but is able to use the dongle when re-inserted.
BTW, my dongle is ZTE MF190, and the application I saw is from Huawei. I am using C#. Is there any work around on this? or better, is there a better logic on this? say using a service, etc..
EDIT:
every query done to the Dongle is done in a separate thread so as to be able to use my application while sending and receiving..
Thanks!
I too had a similar issue with the windows serial port component. There appears to be bugs in the C# code.
Long story short, I managed to get around this by closing the port in a background thread.
Here is my code, note that you may need to modify to fit your application:
private bool ClosePort()
{
_Closing = true;
_SerialPort.DiscardInBuffer();
_SerialPort.DiscardOutBuffer();
if (!_SerialPort.IsOpen) return true;
//We run this in a new thread to avoid issue when opening and closing
//The .NET serial port sucks apparently - and has issues such as hanging and random exceptions
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(DoClosePort));
t.Start();
//here we wait until is **SHOULD*** be closed - note the better way is to fire an internal event when its finished
//We may need to tinker with this wait time
System.Threading.Thread.Sleep(500);
return _SerialPort.IsOpen;
}
private void DoClosePort()
{
try
{
//System.Threading.Thread.Sleep(500);
_SerialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error closing " + _SerialPort.PortName + ". Error Message: " + ex.Message + "\r\n");
}
}
Note that if you try sending/receiving while you are closing, check the _Closing class variable before you attempt the send.
Hope this helps anyone.

Resources