Write an array to Serialport visual c++ - visual-c++-2012

I am writing a program to communicate via RS232 from the PC to a microchip.
I am use to C#, but I started using Visual C++.
I get the following error:
IntelliSense: no instance of overloaded function "System::IO::Ports::SerialPort::Write" matches the argument list argument types are: (RTC_Visual::uint8 [27U], int,RTC_Visual::uint8)
I wrote the command to write to the serial port as follows:
serialPort1->Write(TxStruct.u8_Buffer, 0, TxStruct.Message.u8_Length);
please can someone tell me what am I doing wrong and or what is the correct structure for the serialport->write method.
thanks in advance

Not very clear from your question; showing a bit of code would help.
Seems that the parameter TxStruct.u8_Buffer does not match either the expected Byte[] or Char[]
I imply by the dot (.) that TxStruct is not managed?
The following works:
SerialPort ^myport=gcnew SerialPort;
//configures the port --ptr is a class that interacts with the user
myport->PortName="COM"+ptr->getportnumber();
myport->BaudRate=ptr->getbauds();
myport->DataBits=ptr->getdatab();
myport->StopBits=ptr->getstopb();
myport->Parity=ptr->getparity();
myport->WriteBufferSize=4096;
myport->RtsEnable=false;
myport->ReceivedBytesThreshold=256;
myport->WriteTimeout = 500;
String^ datatowrite="Data to write";
array<Byte>^ mybytes= Encoding::UTF8->GetBytes(datatowrite);
try
{
myport->Open();
myport->Write(mybytes,0,mybytes->Length);
}
catch (Exception^ e)
{
//error
MessageBox::Show( e->Message, "Port Error",
MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
}
The above encodes to UTF8 as I deduct from the parameter TxStruct.u8_Buffer.
Beware of the length of the buffer you are using and the WriteBufferSize property of the serial port. Also, a too long buffer with a handshake XonXoff might result in a timeout exception.
Hope this helps.
All the best,
Adan

Related

How to set Powered property of /org/bluez/hci0 using sd_bus_set_property?

Recently I started to develop a bluetooth app using API exposed via D-BUS. After some research, I chose to use sd-bus library to communicate with D-Bus.
Here is my code:
#include <systemd/sd-bus.h>
sd_bus* bus_;
if (sd_bus_open_system(&bus_) < 0)
{
throw std::runtime_error("sd_bus_open_system");
}
sd_bus_error sd_error;
bool powered = true;
if (sd_bus_set_property(bus_,
"org.bluez",
"/org/bluez/hci0",
"org.bluez.Adapter1",
"Powered",
&sd_error,
"b", &powered) < 0)
{
throw std::runtime_error("Bluetooth Power On");
}
The code above throws "Bluetooth Power On" and the return value of sd_bus_set_property is -22 (EINVAL). I couldn't make much sense from sd_error, but for to whom may be interested, here is the details:
name:0x7fffffffdce8 "И\277UUU"
message:0x5555555d6fbd <handler::handler(std::span<door, 18446744073709551615ul>)+191> "\220H\213E\350dH+\004%("
_need_free:1439044320
I have checked the literal strings using D-Feet app and everything seems to be right.
I've tried running as root or without root.
I've tried adding the user to bluetooth group.
What is wrong with this code?
source code of sd_bus_set_property shows that internally, it calls "Set" from "org.freedesktop.DBus.Properties". Most of the bluetooth examples I've seen, do this without using "sd_bus_set_property", but I'm curious to see how it can be done via "sd_bus_set_property".
Thanks for reading.
I built systemd from source and stepped into its functions and saw at some point it checks the error and since it's not null, returns.
sd_bus_error sd_error = SD_BUS_ERROR_NULL;
fixed the issue.

on dart: cast string to double fails

I have socket listen on Dart, that receives messages from server (written on C).
Client (Dart):
(Uint8List data) async {
Future.delayed(const Duration(seconds: 1));
String serverResponse = String.fromCharCodes(data);
globals.progressEvent = double.parse(serverResponse));
}
Server(C):
send(client_fd, "90", BUFFER_LENGTH, 0);
When run it, I see an exception "Unhandled Exception: FormatException: Invalid double"
it fails on line "double.parse(serverResponse))"
What can be the reason?
Check String parser
As the commenter suggested, I'd recommend checking whether the String you get in Dart corresponds to what the server sends. Have you tried printing it to take a look?
Additionally, I'd generally advise you to use double.tryparse, which will not throw an exception, but instead return null when parsing fails. You can then see if it's null, and if so print some debugging information.
Encoding
I'm not too familiar with how Dart handles binary data, but to me it looks like you use 1 byte for each character (UTF-8), whereas Dart's fromCharCodes seems to conventionally use the UTF-16 format, with 2 bytes per character. When trying to parse your 9 and 0 together, I guess it gets a non-numeric value, causing the parser to faill. https://api.flutter.dev/flutter/dart-core/String/String.fromCharCodes.html

BlueNRG Bluetooth: read central device name

I'm using the STM BlueNRG-MS chip on my peripheral device and after connection I'd like to read the name of the connected central device (android phone).
I thought I can do this directly in my user_notify routine which is registered as hci callback
/* Initialize the Host-Controller Interface */
hci_init(user_notify, NULL);
So on the EVT_LE_CONN_COMPLETE event, I take the provided handle for the central device and I use aci_gatt_read_using_charac_uuid() to read what I thought is the characteristic with the device name (uuid 0x2a00).
case EVT_LE_META_EVENT:
{
evt_le_meta_event *evt = (void *)event_pckt->data;
switch(evt->subevent){
case EVT_LE_CONN_COMPLETE:
{
evt_le_connection_complete *cc = (void *)evt->data;
GAP_ConnectionComplete_CB(cc->peer_bdaddr, cc->handle);
uint16_t uuid = 0x2a00;
resp = aci_gatt_read_using_charac_uuid(cc->handle, 0, 1, UUID_TYPE_16, (uint8_t*)&uuid);
LOG("GATT read status: %d", resp);
enqueEvent(EVENT_BLE_CONNECTED);
}
break;
}
}
Long story short, it doesn't work. First thing I'm not sure about is, what is the start_handle and end_handle parameter of aci_gatt_read_using_charac_uuid(), it returns ERR_INVALID_HCI_CMD_PARAMS.
Can someone shed some light here?
update
What also puzzles me is that the function aci_gatt_read_using_charac_uuid() is nowehere referenced in the BlueNRG-MS Programming Guidelines.
update2
I changed the function call to aci_gatt_read_using_charac_uuid(cc->handle, 0x0001, 0xffff, UUID_TYPE_16, (uint8_t*)&uuid); but I still get the ERR_INVALID_HCI_CMD_PARAMS. What which paramter could even be invalid? The uuid exists, I can read the device name if I use the BlueNRG GUI with a bluetooth dongle.
update3
Has anyone ever used this function or somehow managed to read a characteristic from a central device? I'd highly appreciate any help or hint.
Here you go, The BlueNRG-MS Bluetooth® LE stack application command interface (ACI) - User manual
page 75 - 4.6.25 Aci_Gatt_Read_Charac_Using_UUID()
and make sure you have called Aci_Gatt_Init()
The user manual is last revised July 2019, the document you link to is from 2018, don't know if this is why ?
The start_handle and end_handle is the range of handles in your service as pictured here -
Here is a discussion to the closest thing I could find to match your question.
As it turned out there are two bugs in the BlueNRG API.
In bluenrg_aci_const.h file the OCF code OCF_GATT_READ_USING_CHARAC_UUID shall be 0x119 instead of 0x109.
And in the implementation of the aci_gatt_read_using_charac_uuid() function, there is a missing setting on event:
rq.event = EVT_CMD_STATUS;
Patching them fixed the issue.

Passing a FD to an unnamed pipe over DBus using Vala

I'm trying to send a large block of data between applications by sending a control message over DBus from one to the other requesting a Unix file descriptor. I have it so that the client can request this, the server creates a DBus message that includes a UnixFDList, and the client receives a reply message but it doesn't contain anything. On the server side in Vala the DBusConnection object is setup using register_object, unfortunately the Vapi hides the DBusInterfaceVTable parameter that all the C examples use that would let me specify a delegate for method calls. I've tried to use register_object_with_closures instead but I can't seem to get that to work and the Closure object in Vala is woefully undocumented.
It seems to me that I need one of these methods in order to receive the message from the DBusMethodInvocation object that you get from a call to the DBusInterfaceMethodCallFunc delegate, with that you can create a reply message. Is there a way to either specify a closure class that works with register_object_with_closures, or a way to specify a DBusInterfaceVTable object as part of the service data?
I know that one option is to just create the service in C, but I'd rather figure out and understand how this works in Vala.
Vala uses UnixFDList internally for methods that contain a parameter of type GLib.UnixInputStream, GLib.UnixOutputStream, GLib.Socket, or GLib.FileDescriptorBased.
Example:
[DBus(name="eu.tiliado.Nuvola")]
public interface MasterDbusIfce: GLib.Object {
public abstract void get_connection(
string app_id,
string dbus_id,
out GLib.Socket? socket,
out string? token) throws GLib.Error;
}

Sending AT Commands via LabWindows code to SPBT2632C2A

I am trying to make a LabWindows/CVI program that talks with SPBT2632C2A Bluetooth chip. I am using a st eval spbt3atv3 dongle. I am trying to push a button and send a command to chip, but chip do not answer to me. I know my LabWindows program can receive messages from dongle, because every time I press reset button on dongle it shows me the boot up message. As I searched I need to add \n\r to end of my string, but it still didn't work. Chip does not send even error message.
CODE
int CVICALLBACK rasti (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
sprintf(discovery,"AT+AB discovery\n\r");
if(ComWrt (4,discovery, 18)!= 18){
SetCtrlVal (panelHandle, PANEL_TEXTBOX, "Nesekmingas duomenu siuntimas");
//Unsuccessful sending data
}
else {
SetCtrlVal (panelHandle, PANEL_TEXTBOX, discovery);
}
break;
}
return 0;
}
It's event called by button. It appends my discovery string to text box. So I think it's sending data correctly to serial port. But I think my string is wrong so I don't get the answer from chip.
Any ideas how to solve this to get responses from chip?
Make sure to call OpenComConfig with the correct parameters before invoking ComWrt. You can look for the correct port settings in the Hyperterminal (since you mentioned it communicates correctly with the device).
Refer to NI's documentation for more on this.
Also, trying different types of line termination characters might help (try using \r\n, \n or \r).

Resources