How to obtain bluetooth port direction with pyserial? - port

I'm trying to connect to an RN42, module through python. When the RN42 pairs with W10 it creates two virtual COM ports(outgoing and incoming). I need to connect to the outgoing port.
I'm trying to do this automatically. I've tried:
import serial
import serial.tools.list_ports as port_lst
ports = list(port_lst.comports())
bluetooth_ports = []
for p in ports:
if 'Bluetooth' in p.description:
bluetooth_ports += [p.device]
bluetooth_com = serial.Serial(bluetooth_ports[0],115200)
I thought that the first port was usually the outgoing one, but I've paired the module to another computer, and this didn't apply (the second port was the outgoing one). Is there a way to find out the direction of the COM ports?
Thanks!!!

Although this is an antique question, I have been searching for the answer to this for some time myself and since I finally figured it out I wanted others to be able to find the answer. With help from a blog entry at in the hand and its accompanying gist:
The trick is to acquire the hwid using pySerial, then parse the address. The incoming port in a pair has an address of zero and the outgoing port has a nonzero address. Here is some ugly Python code that decodes it:
import serial.tools.list_ports
cp=serial.tools.list_ports.comports()
for p in cp:
if "BTHENUM" in p.hwid:
start_of_address=p.hwid.rfind("&")
end_of_address=p.hwid.rfind("_")
address=p.hwid[start_of_address+1:end_of_address]
if int(address,16)==0:
port_type="incoming"
else:
port_type="outgoing"
print(p.hwid)
print(p.name, address, port_type)
And the output:
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&CC47540&0&000000000000_000000A8
COM4 000000000000 incoming
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\7&CC47540&0&209BA5420081_C00000000
COM5 209BA5420081 outgoing

Related

How do i check my nat type using stun? New on this

i want to know my nat type, i was trying with this command "netsh interface Teredo show state" however someone suggested me that i should ie a stun server or client to have a better idea of my nat. But i dont have any idea of this, i just want to have an open nat to play games, so please be patient with me :)
By the way i have enabled DMZ and UPNP on my modem.
Server Name : win1910.ipv6.microsoft.com.
Client Refresh Interval : 20 seconds
Client Port : unspecified
State : qualified
Client Type : teredo client
Network : unmanaged
NAT : symmetric (port)
NAT Special Behaviour : UPNP: Yes, PortPreserving: Yes
I googled for stun clients or servers but they dont have any info on how to install them or use them, like this one https://github.com/moien007/STUN so if someone can help me with an app that is easy to install or some instructions on how to install a stun server or client please.
Thank you very much.
Edit runing stuntcode.exe got this
Result of CTestDataStream: PASS
Result of CTestReader: PASS
Result of CTestBuilder: PASS
Result of CTestIntegrity: PASS
Result of CTestMessageHandler: PASS
Result of CTestCmdLineParser: PASS
Testing detection for DirectMapping
Testing detection for EndpointIndependent mapping
Testing detection for AddressDependentMapping
Testing detection for AddressAndPortDependentMapping
Testing detection for EndpointIndependentFiltering
Testing detection for AddressDependentFiltering
Testing detection for AddressAndPortDependentFiltering
Result of CTestClientLogic: PASS
Result of CTestRecvFromEx(IPV4): PASS
Result of CTestRecvFromEx(IPV6): PASS
Result of CTestFastHash: PASS
Result of CTestPolling: PASS
Result of CTestAtomicHelpers: PASS
Result of CTestRateLimiter: PASS
You can get a STUN client application for all platforms at https://www.stunprotocol.org (prebuilt EXE clients for Windows and Mac command line apps are up there too!)
Demonstration:
C:\StunServer> stunclient.exe stunserver.stunprotocol.org --mode behavior
Binding test: success
Local address: 192.168.1.7:52707
Mapped address: 101.102.33.44:52707
Behavior test: success
Nat behavior: Endpoint Independent Mapping
C:\StunServer> stunclient.exe stunserver.stunprotocol.org --mode filtering
Binding test: success
Local address: 192.168.1.7:39843
Mapped address: 101.102.33.44:39843
Filtering test: success
Nat filtering: Address Dependent Filtering
Running the two test modes, reveals Endpoint Independent Mapping and Address Dependent Filtering as described in RFC 5780 for classifying NATs.

Can't switch from staic IP to DHCP in nodemcu lua

wifi.sta.setip({ ip = "192.168.0.111", netmask = "255.255.255.0",gateway = "192.168.0.1"})
with above we can set static IP,
but by mistake if someone enters the netmask/gateway/ip a digit or to wrong (eg:netmask = "255.255.2.0" ) we have no way of detecting. only way is to
by re entering correctly
rebooting
(How to clear static IP configuration and start DHCP)
so it would be nice if we can detect the status somehow like with wifi.sta.status() do.
How to clear static IP configuration and start DHCP
wifi.sta.clearconfig()
Clears the currently saved WiFi station configuration, erasing it from
the flash. May be useful for certain factory-reset scenarios
Not sure if this also deletes the ip but you can probably just set the ip to "0.0.0.0" befor you reconnect.

HAProxy Lua how to change server port?

Server.set_addr (sv, addr)
I can not understand what the method takes as input.
It would seem that Server.get_addr(sv) returns ip:port - so you also need to pass ip:port, but no. This does not work.
The documentation reads:
See the documentation for the control socket for more information on the string format.
Well, I saw that you need to transfer the ip port: port (example: 127.0.0.1 port 80). But that doesn't work either.
The only thing that works is pass only ip (example: 127.0.0.1).
The question is how to change the port?
Code sample: (if not work - no have any entry in log)
-- work
-- log entry: changed its IP from 1.1.1.1 to 2.2.2.2 by Lua script.
local newAdr = backendServer.ip
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. ":" .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " " .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " port " .. backendServer.port
server:set_addr(newAdr)
Update: As of HAProxy 2.2dev7, this functionality has now been merged: set_addr takes an additional optional parameter to specify a new port.
It's not currently possible to do that via Lua. A look through HAProxy's source code reveals that set_addr calls hlua_server_set_addr, which calls server_parse_addr_change_request, which calls update_server_addr, which only updates the address and not the port. Contrast this with the management socket's set server, which calls cli_parse_set_server, which calls update_server_addr_port, which does update the port too.
However, there are two pieces of good news:
Since the management socket can do it, you can use it as a workaround.
It would be very simple to modify HAProxy's Lua interface to support updating the port, since all the logic is already there. I sent a patch that does so to their mailing list.

Two Lslidar(Leishen) connected with One Embedded Board(Jetson Xavier)`s possible?

Running one of the Lslidar(16 Channels) on the Embeded board(ROS development environments) is not a problem.
This is because you can use the default IP and ports.
But I plan to run two at the same time, and I want to use values other than the default values of ports and IP..
For example, you can use ports like 2368 and 2369.
I need a reference to refer to how to change the port value and IP value...
Help Plz...
Here is code from lsLidar driver from ROS Wiki:
bool LslidarC16Driver::loadParameters() {
//pnh.param("frame_id", frame_id, std::string("lslidar"));
pnh.param("lidar_ip", lidar_ip_string, std::string("192.168.1.222"));
pnh.param<int>("device_port", UDP_PORT_NUMBER,2368);
pnh.param<bool>("add_multicast", add_multicast, false);
pnh.param("group_ip", group_ip_string, std::string("234.2.3.2"));
inet_aton(lidar_ip_string.c_str(), &lidar_ip);
ROS_INFO_STREAM("Opening UDP socket: address " << lidar_ip_string);
if(add_multicast) ROS_INFO_STREAM("Opening UDP socket: group_address " << group_ip_string);
ROS_INFO_STREAM("Opening UDP socket: port " << UDP_PORT_NUMBER);
return true;
}
As you can see there is a place where you can change port and IP. For two devices I'd advise you to modify the original driver. All source code is available on https://github.com/tongsky723/lslidar_C16
Clone it to you workspace and create additional functionality for two LiDARs.

ROS - How do I publish a message and get the subscribed callback immediately

I have a ROS node that allows you to "publish" a data structure to it, to which it responds by publishing an output. The timestamp of what I published and what it publishes is matched.
Is there a mechanism for a blocking function where I send/publish and output, and it waits until I receive an output?
I think you need the ROS_Services (client/server) pattern instead of the publisher/subscriber.
Here is a simple example to do that in Python:
Client code snippet:
import rospy
from test_service.srv import MySrvFile
rospy.wait_for_service('a_topic')
try:
send_hi = rospy.ServiceProxy('a_topic', MySrvFile)
print('Client: Hi, do you hear me?')
resp = send_hi('Hi, do you hear me?')
print("Server: {}".format(resp.response))
except rospy.ServiceException, e:
print("Service call failed: %s"%e)
Server code snippet:
import rospy
from test_service.srv import MySrvFile, MySrvFileResponse
def callback_function(req):
print(req)
return MySrvFileResponse('Hello client, your message received.')
rospy.init_node('server')
rospy.Service('a_topic', MySrvFile, callback_function)
rospy.spin()
MySrvFile.srv
string request
---
string response
Server out:
request: "Hi, do you hear me?"
Client out:
Client: Hi, do you hear me?
Server: Hello client, your message received.
Learn more in ros-wiki
Project repo on GitHub.
[UPDATE]
If you are looking for fast communication, TCP-ROS communication is not your purpose because it is slower than a broker-less communicator like ZeroMQ (it has low latency and high throughput):
ROS-Service pattern equivalent in ZeroMQ is REQ/REP (client/server)
ROS publisher/subscriber pattern equivalent in ZeroMQ is PUB/SUB
ROS publisher/subscriber with waitformessage equivalent in ZeroMQ is PUSH/PULL
ZeroMQ is available in both Python and C++
Also, to transfer huge amounts of data (e.g. pointcloud), there is a mechanism in ROS called nodelet which is supported only in C++. This communication is based on shared memory on a machine instead of TCP-ROS socket.
What exactly is a nodelet?
Since you want to stick with publish/ subscribers, assuming from your comment, that services are to slow I would have a look at waitForMessage (Documentation).
And for an example on how to use it you can have a look at this ros answers question.
All you need to do is to publish your data and immediately call waitForMessage on the output topic and manually pass the received message to your "callback".
I hope this is what you were looking for.
To get this request/reply behaviour ROS has a mechanism called ROS service.
You can specify the input and output of your service in a service file similar to a ROS message definition. You can then call the service of a node with your input and the call will receive an output when the service is finished.
Here is a tutorial how to use this mechanism in python. If you prefer C++ there is also one, you should find it.

Resources