libmodbus modify default register offset - modbus

I have been testing a connection to a TCP modbus device using the open source libmodbus library. The very first register I had to read was at 45001 on the device. It turns out that using libmodbus I have to give it an address of 0 to get this register. If I want register 45010 I use 9.
There is another register I want to read at 44001. I don't see anything in the documentation about changing the base address. Is it possible to use a negative offset? Am I going to have to recompile the library?

No, you're going to need to figure out how addresses described in your device's documentation map to actual Modbus addresses. This is extremely common.
The library you're using is just doing things the way the Modbus specification defines, and exposing that to you through its interface.

Related

Adress external Hardware directly without driver?

Is it possible to access external hardware without using a driver, i.e. not having the driver abstraction layer in between program and external device?
Can you use a device by implementing your own driver-like controlling/handling directly in your program code?
I'm trying to understand a program that implements a Modbus protocol and some very specific Modbus configurations. Now I don't know how exactly it communicates with the Modbus devices.
It looks to me that this is very similar to what a driver does.
But can it even communicate DIRECTLY with the device without having a driver installed?
Yes, there are several micro-kernel OS's that always configure this way -- drivers are entirely implemented outside of the kernel.
The first thing you likely need is to get access to the device's registers; typically performed with mmap(), you may need to dig around a bit to find the right settings for cacheability, etc...
Second problem is interrupts. Unless you are running something like QNX, you won't have a way to have interrupts signal your program directly. You will probably have to turn them off and poll the device periodically.
If you are using linux and need io ports (inb, outb, etc...) man ioperm for more information.

Determine the layer of a packet in a pcap file created by tcpdump

I was reading through the libpcap tutorial and was using some captures that I want to analyze (I didn't create them myself), so I am using the offline mode.
When applying the tutorial, nothing seemed to match. I didn't get valid MAC addresses from the ethernet frame and not even the preamble seemed to be correct. After some expirementing I fired up wireshark and noticed something odd. Each packet start with a 6: These are not packets on the ethernet (data link) layer, they are already one layer up on the IP (network) layer (They are IPv6 packets, which is expected).
Of course, I can now easily continue, but I am still wondering: How do I know at which layer the captured packages are? It seems there are at least two options: Saving at the data link layer and saving at the network layer. Could it also be that I receive packets on the transport layer? How do I differentiate? Must the user tell me? I'd really hate to have to guess, but seeing as wireshark gets it right, there must be a simple way to determine it.
The libpcap file format has a field in its global header called network that specifies the data link type. This corresponds to documentation over at the tcpdump project about various Link-Layer Header Types.
From there, you would be able to determine how many bytes the link layer consists (if any) and will be a sure fire way for you to parse pcap files created with the libpcap library.
I am not aware of a program options in the "libpcap family" that would allow you to just capture starting at the transport layer.

SO_BINDTODEVICE option support for FreeBSD

I am implemneting a code based on Raw sockets. In order to receive coming Ethernet frames I have to bind the socket I created to an Ethernet interface. The only way to do that, as far as I am concerned, is via setsockopt() function with the option SO_BINDTODEVICE.
The problem is that I am using FreeBSD which does not support such option. Is there any patch to use in order to enable this feature or any other trusted alternative?
You can use sendto if you are working with IPv6, see this example.
Unfortunately this doesn't work with IPv4.
As antiduh said, you can use libpcap to capture packets, provided you have access to /dev/bpf (which is usually restricted to root).

pcap set 802.11 channel

Can I somehow set the 802.11 channel programmatically with the pcap library (or even anything else)?
I've written a sniffer and need to sniff on different channels, so I need to set the specific one first. I've been searching but I can't find anything, so I currently set it with the iw tool manually.
No, libpcap currently doesn't have an API to get a list of available 802.11 channels or set the current channel.
I infer from "iw tool" that you're doing this on Linux; you'll have to directly use netlink calls to set the channels - see, for example, the code in the ws80211_utils.c file in the top-level source directory of Wireshark.

What is the preferred way to find all other clients on local network?

Writing an iPhone app in which I want to save the user the grief of typing in IP addresses by finding other clients on the local network.
Is this possible?
If so, what is the preferred way of doing this?
Yes, use Bonjour. See NSNetService.
This class is used to advertise information about the service you're running using multicast DNS. Use NSNetServiceBrowser to find published services of the type you're interested in.
The clients need some way to register with the server. Or you can use portknocking. Long time back there was a protocol from Novell SLP. There is a OpenSLP now. Found a interesting link, I hope this helps
http://www.opendoor.com/shareway/slp.html