Manage serial port from two processes simultaneosly - sockets

I have the following scenario:
Rasperry pi connected to a device via Serial port
3g Dongle connected to the raspberry (with the ability to make/recieve calls)
One process reading the data from the serial port and redirecting it to a server (using 3g)
Another process waiting for a incoming call, and when someone calls the program takes the data from the serial port and redirect it via the 3g dongle using AT commands ( like fax-call). When someone calls, the call is made using AT commands and the caller should be able to "speak" with the final device connected to the serial port.
The problem is that the two processes can not live together since they are using the same serial port, and when one process is already started, the other can not read the data from the serial (port busy).
Is there a way to achieve this ? Can i make like a "fake" serial port, or something that redirects the data?
Thank you very much

You may write a single service that communicates with the real serial port but offers two virtual serial ports itself like described here Virtual Serial Port for Linux

Like all good GSM things there's a specification for that :)
GSM 07.10 is the specification and there are libraries out there for some time that can support you. Some are libraries you can build into your server systems and some are actual daemons.
A quick google for "gsm multiplexing" will get your started that I am sure.

I had a similar problem managing a serial port between some independent processes. Finally my best solution was to use Redis to listen for calls to the port.
The process that wants to send something, publishes through redis, in the channel 'uart_request' a json with the arguments to make the call to the serial port, and also a hash ('hash_message') made with the timestamp. Just before posting the json, the process subscribes to 'hash_message'.
Finally, a process listens for the posts in 'uart_request'. When a post arrives, take the 'hash_message' from the json, make the call to the serial port, wait for the response and post the response in 'hash_message'.
The point is that there is just one process that control the serial port, so its not necessary to open and close. Works really good.

Related

How to get serial port data in an event form in flutter windows desktop application

I am making a windows application which communicates with a custom hardware over serial port (Sends and receives data to/from the device, processes it and displays it on relevant widgets).
I am stuck at the receive part of data. I want a function to be called whenever data is received on a serial port without having to check for it constantly. I am using the serial_port_win32 package to communicate with my device.
As far as I understood the package, there is no way to subscribe to serial port receive event and call a relevant function.
I could make an function which gets called by a timer event and reads serial port data, but this does not sound like a good solution.
I have looked at dart stream option. The way I understood it is that, the Stream function's definition will have a code to check if there is some data available available on serial port and yield it, if there is any. But there too I will have to provide some timeout in the read function in case there is no data.
All of the solutions I could think of are not really "event trigger on their type". I am sure there would some elegant way of doing this.

Modbus client and server with message forwarding

I am working on a legacy modbus program for an industrial SCADA system.
Currently, the c++ program acts as both a modbus TCP server and client.
Client behaviour:
It reads from a number of vendor PLCs (servers) on site, performs calculations and sends control commands back to the PLCs based on the data received across the site.
Server behaviour:
responds to a variety of TCP read and write requests from web interfaces and laptops on site.
Until now, this has worked fine, but we have recently installed a logging client on the network which polls our program very frequently (sub-second) and this has revealed timing issues: the program can potentially take a very long time in its client loop performing calculations and reading PLC values before acting as a server and responding to incoming requests.
Easy solution would be to split the programs into a modbus server and client instance, and keep them both running on the same embedded PC.
The issue I have is that the remote web interface (HMI) must be able to control the behaviour of vendor PLC 2 and Vendor PLC 2 will only allow one TCP connection from the embedded PC. In the past the program has handled writes requests from the HMI by forwarding them on to the PLC 2 via the open socket.
I'd be keen to gather thoughts on best practices here.
My thinking:
the modbus server program will need to respond to the HMI requests and somehow store the information required for vendor PLC 2, and it will also need to set a status register to inform the modbus client that there is data for vendor PLC 2.
The modbus client program will need to read the status register (and data) from the server and pass this on to vendor PLC 2.
Am I heading in the right direction?
Without having details on your implementation I can only guess the problem is that your program is single-threaded, and delays are caused by waiting responses from PLCs.
So, if my assumption is correct, you need to switch to 'select' function and redesign your software to be totally async. You have to put all sockets (both connected and accepted) in a FDs set and wait events on them.
win32:
https://learn.microsoft.com/en-us/windows/desktop/api/winsock2/nf-winsock2-select
linux:
https://www.opennet.ru/cgi-bin/opennet/man.cgi?topic=select&category=2
I've written the same app ages ago on win32 (but without calculations) and it easily processed about 200 PLCs, working on the same machine with SCADA.

A recommendation how to create an info monitor server

I want to create a data streaming server, and I need a solution, cuz I can't understand what I should to use.
Okay, I have a central home server, which contains infos about temperature, light, motion sensors, music on my central home sound bus and more. And I want to create solution like:
Server continuously sends the data to the local network via port.
And at any time I have an opportunities to connect. For e.g. I connect to 192.168.1.2:1234 and immediately start to listening all information. I can do it from any clients and at the same time. And the server may not even know about connections. It only sends the data to the local network via port, and nothing more.
UPD:
I used TCP protocol to exchange the data between server and clients, but it wasn't a good solution, cuz it's not suit to my described idea.

Saving data that's being sent to a process through TCP/IP

I want to capture and save the data that's being sent to a certain process through internet .
Are there any tools for the job?
If not, does listening to the same port as the process that I'm trying to get data from, will get me the data?
Any help appreciated !
You can try Wireshark: http://www.wireshark.org/
Or RawCap: http://www.netresec.com/?page=RawCap
I don't know what is the data format you are trying to capture. I used these two tools to capture xml data from web service.
On Windows, use Winsock Packet Editor (WPE). You will be able to hook a process' all Winsock-related functions and capture (and even modify/block) any TCP/IP, UDP packets that the application receives or sends. For all other operating systems, you will have to either:
write your own tool that hooks various socket functions (e.g. send, recv, etc.)
or just use Wireshark which will capture all Layer-3 packets that goes through your network card. You will have to use your own knowledge of the application that you're trying to monitor in order to filter the packets that are specific to the application.
Are there any tools for the job?
Wireshark. But what have you tried?
If not, does listening to the same port as the process that i'm trying to get data from, will get me the data?
Not if you don't forward the traffic to the real destination, otherwise the other party will be waiting forever on a response, or simply timeout and close the connection. You should create something like a proxy.
Wireshark is easier.

How to listen on a network port in Objective-C

I am trying to make an application for iPhone that can listen for traffick on a specific network port.
A server on my network is sending out messages (different status messages for devices the server handles) on a specific port.
My problem is that when I make a thread and makePairWithSocket I block the port for others who want to send messages to the server, so I only want to listen to the traffic on a specifyed port and then check for specific heraders and then use those messages.
I know how to make the connection and talk to the server using write and read streams, but then I makePairWithSocket and block the port for all other devices on the network
Any one that has any suggestions on how to listen on a port in Objective-C without pairing with the server?
Thanks in advance
Daniel
Check out CocoaAsyncSocket. It gives you a nice and structured way (with delegates) to send and receive data... also with multiple clients. The documentation is quite good. project link
edit: Have a look at the AsyncUdpSocket class for a stateless UDP connection.
I think this requires network support well below the socket API level, perhaps at the hardware driver level, assuming the packets are even being routed to your device.