Java dbus ModemManager - dbus

I'm tring to access without luck dbus from Java for ModemManager.
Im using dbus-java from https://github.com/hypfvieh/dbus-java.
What I've done so far is:
Get a connection to the Bus
DBusConnection conn = DBusConnection.getConnection(DBusConnection.DBusBusType.SYSTEM);
Imported Introspection xml, generated on device (But I'm not sure this work is ok)..
Tried a connection to the specific bus to handle informations:
Modem modem = conn.getRemoteObject("org.freedesktop.ModemManager1", "/org/freedesktop/ModemManager1/Modem", Modem.class)
Connect an handler to get info:
conn.addSigHandler(Modem.StateChanged.class, new DBusSigHandler<Modem.StateChanged>() {
#Override
public void handle(Modem.StateChanged s) {
System.out.println("State" + s._new);
}
});
Result.. no event are fired. On device i see (using dbus-monitor --system) that events are fired for all the time i run.
for example, in dbus-monitor i see:
signal time=1615482074.152996 sender=:1.5 -> destination=(null destination) serial=478 path=/org/freedesktop/ModemManager1/Modem/0; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.freedesktop.ModemManager1.Modem"
array [
dict entry(
string "SignalQuality"
variant struct {
uint32 80
boolean true
}
)
]
array [
]
Someone can point me to the right direction? I'm sure Im doing something wrong.
Thanks
Gianmaria

The second string in this setup is wrong:
Modem modem = conn.getRemoteObject("org.freedesktop.ModemManager1", "/org/freedesktop/ModemManager1/Modem", Modem.class)
It should be the full object path; e.g. for modem at index 0:
Modem modem = conn.getRemoteObject("org.freedesktop.ModemManager1", "/org/freedesktop/ModemManager1/Modem/0", Modem.class)

Related

LwIP Clilent can't establish a connection

I want to connect two F746ZG boards so that they can communicate via TCP. I am using the STM implementation of LwIP with the netconn API. The IP address is supplied via DHCP, but it is always the same address. Also, the address matches the expected value. The problem I am facing is that the client seemingly can't establish a connection. I am binding the connection to port 8880. Since I ran into this issue, I have written a debug client that should just periodically send a predefined message to a server. Here is the code for the client:
static void tcpecho_client_thread(void const *arg)
{
struct netconn *xNetConn = NULL;
err_t bind_err, connect_err;
char* b_data = "OK"; // Data to be sent
uint16_t b_len = sizeof ( b_data );
IP4_ADDR(&local_ip, IP_ADDR0_CLIENT, IP_ADDR1_CLIENT, IP_ADDR2_CLIENT, IP_ADDR3_CLIENT);
IP4_ADDR(&pc_ip, IP_ADDR0_PC, IP_ADDR0_PC, IP_ADDR2_PC, IP_ADDR3_PC);
xNetConn = netconn_new ( NETCONN_TCP );
if (xNetConn != NULL){
bind_err = netconn_bind ( xNetConn, &local_ip, TCP_PORT_NETCONN );
if(bind_err == ERR_OK){
// Try to connect to server
for(;;){
connect_err = netconn_connect ( xNetConn, &pc_ip, TCP_PORT_NETCONN);
if (connect_err == ERR_OK){
// We are connected
while(1){
BSP_LED_On(LED1);
netconn_write(xNetConn, b_data, b_len, NETCONN_COPY);
vTaskDelay(1000); // To see the result easily in Comm Operator
}
}
}
}else{
// Failed to bind the connection
BSP_LED_On(LED3);
}
}else{
// Failed to allocate a new connection
BSP_LED_On(LED3);
}
}
When I debug this, netconn_connect never manages to actually connect to something. Since I am able to ping the board and get a response, I am confused, what is going wrong here. I have tried to use Hercules to set up a TCP server on my PC so that the board can connect to that, but that also doesn't work. Using Wireshark, I can see the responses to my ping command coming in, but I don't see anything that would indicate the board trying to connect to my PC.
I have tested the corresponding server on the second board, but that runs fine. I can connect to it with Hercules and send data, so I doubt there is anything fundamentally wrong with the LwIP stack.
What I could guess is that I messed up the netconn_bind, I am not 100% sure what IP you are supposed to bind the connection to. The way it currently is, is how I read the documentation. For the server, I have bound it to IP_ADDR_ANY. Besides that, my implementation mostly matches with the examples you can find online (e.g. LwIP Wiki).
I have figured out the problem. After I delete the netconn_bind call, everything works fine for me.

C# communicating with Arduino COM access denied

I'm working with a fingerprint sensor on Arduino, but sometimes, on runtime, my WPF app throws an exception on sp.Open() saying "COM3" Access denied"
Here's the code on c#:
public string ConfigPort()
{
SerialPort sp = new SerialPort();
sp.BaudRate = 9600;
sp.PortName = AutodetectArduinoPort();
sp.Open();
string s = "";
while (true)
{
s = sp.ReadLine();
Console.WriteLine(s);
return s;
}
}
Here's the AutodetectArduinoPort method:
public string AutodetectArduinoPort()
{
ManagementScope connectionScope = new ManagementScope();
SelectQuery serialQuery = new SelectQuery("SELECT * FROM Win32_SerialPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(connectionScope, serialQuery);
try
{
foreach (ManagementObject item in searcher.Get())
{
string desc = item["Description"].ToString();
string deviceId = item["DeviceID"].ToString();
if (desc.Contains("Arduino"))
{
return deviceId;
}
}
}
catch (ManagementException e)
{
/* Do Nothing */
}
return null;
}
Any solution??
This happens quite a lot with Arduino, personally I think there's a problem with the FTDI driver....certainly wouldn't be the first issue with them. What I do when this happens is physically unplug the device from the USB port, wait for the notification sound from Windows and then plug it back in again. This seems to "reset" the port and force whatever is holding it open to release it, and you're good to go again. You may need to do this quite a bit over time, so the only other tip I have is to use a cheap USB hub so that you wind up wearing out the port pins on that rather than your expensive laptop/desktop.
The error you're getting means that the port is already open. Make sure that you only call ConfigPort() once, and after you're done processing data from the Arduino, close the port with sp.Close(). I would suggest converting sp into a global variable, and subscribing to the OnClosing event of your WPF window, so you can close the port when the user closes the application.

How to clean up akka-http websocket resources following disconnection and then retry?

The code below successfully establishes a websocket connection.
The websockets server (also akk-http) deliberately closes the connection using Andrew's suggested answer here.
The SinkActor below receives a message of type akka.actor.Status.Failure so I know that the flow of messages from Server to Client has been disrupted.
My question is ... How should my client reestablish the websocket connection? Has source.via(webSocketFlow).to(sink).run() completed?
What is best practice for cleaning up the resources and retrying the websocket connection?
class ConnectionAdminActor extends Actor with ActorLogging {
implicit val system: ActorSystem = context.system
implicit val flowMaterializer = ActorMaterializer()
private val sinkActor = context.system.actorOf(Props[SinkActor], name = "SinkActor")
private val sink = Sink.actorRefWithAck[Message](sinkActor, StartupWithActor(self.path), Ack, Complete)
private val source = Source.actorRef[TextMessage](10, OverflowStrategy.dropHead).mapMaterializedValue {
ref => {
self ! StartupWithActor(ref.path)
ref
}
}
private val webSocketFlow: Flow[Message, Message, Future[WebSocketUpgradeResponse]] =
Http().webSocketClientFlow(WebSocketRequest("ws://localhost:8080"))
source
.via(webSocketFlow)
.to(sink)
.run()
Try the recoverWithRetries combinator (docs here).
This allows you to provide an alternative Source your pipeline will switch to, in case the upstream has failed. In the most simple case, you can just re-use the same Source, which should issue a new connection.
val wsSource = source via webSocketFlow
wsSource
.recoverWithRetries(attempts = -1, {case e: Throwable => wsSource})
.to(sink)
Note that
the attempts = -1 will retry to reconnect indefinetely
the partial function allows for more granular control over which exception can trigger a reconnect

Write an array to Serialport visual c++

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

How to inform via interrupt when a new message has been received, using AT-Commands +CNMA or +CNMI in GSM Modems?

I'm developing a winform program in which I need to send and receive SMS messages.
I have no problem sending SMS, but I don't know how to inform when a new message has been received via a GSM modem. I want to have an acknowledgement like an interrupt or event, when a new message has been received.
As far as I know I should work to at+CNMI or at+CNMA, but unfortunately I can't find an example or suitable reference for those. Furthermore, I need to know how to get delivery message or how to handle that?
AT+CNMI and AT+CNMA are standard AT commands defined in the spec 3GPP TS 27.005, available here.
For confirmation of successful delivery, see section 3.5.1 of this document which is the description for "Send message, +CMGS".
When you are defining new port in your connection to connect, you should get a data received trigger to it.
SerialPort port = new SerialPort();
//...
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
and in that trigger:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
MessageBox.Show(sp.ReadExisting()); //Data received
}

Resources