How to read data sent to an XBee on Windows? - python

How can I read the data I am sending to my XBee connected to my Windows machine?
I want to see if the data is being sent correctly, because my code is compiling correctly in IDLE, but if I try to read the serial console in XCTU it says the port is currently being occupied. Any ideas on how to read the data I'm sending?
import serial
i = 'A'
ser = serial.Serial('com3',9600,timeout =1)
ser.write(i)
ser.close()

You have both XBees connected, while communicating to one in the python code, you have the other in the serial console in XCTU. Writing to the port will display the message.

Are you trying to open COM3 in XCTU? You won't be able to do that since you have it open in Python. Is that XBee module paired with one on another serial port where you'd be able to see the output?
You might want to add a delay between the ser.write() and ser.close() calls to ensure you're giving it time to send the data. It's possible that the first call just queues the data to send.

Have you considered using the Python-Xbee library also? It makes decoding packets easier:
https://github.com/nioinnovation/python-xbee
This library also has support for Zigbee.
Jim

Related

How to make python sniffs on serial port that is used by another program

I have an accounting program in windows 10 that I want to extract data from it using python code.
I want to do that by sniffing on the serial port that the accounting program writing data to it.
I have two problems:
1-I used process manager to see the internal serial port that the programs are using but I can't find the serial port that the accounting program is using to write data to it.
2- I used virtual ports to try to sniff on it using python serial library, when I try to access the red/write com ports I have PermissionError(13, 'Access is denied.', None, 5).
I'm expecting that the accounting program only start using serial port after some trigger like start printing order or something else.
and after I find the serial port that the program is using how to sniff on it using python?
I would appreciate it if someone can make the idea clear.

i am sending commands through serial port in python but they are sent multiple times instead of one

i am sending some commands having particular response serially using com port..the commands are kept in a file..i am reading each command through the file line by line and sending it serially over the com port..but when i am seeing it from the receiver end using Magic Terminal(Software)..i found that each command is going multiple times..which i am sending only one time..i have made a code in pycharm..and in the console i am seeing that command is going only once but from the uart receiving end the story is something else..i am stuck with this problem..i have maintain the same baudrate and everything but not able to diagnose the issue..
github link for the code is: https://github.com/AkshatPant06/Akshat-Pant/blob/master/cmd%20list
def recvResponse():
ser.write(serial.to_bytes(intCmd))
time.sleep(1)
data_recv=ser.read(2)
return data_recv
this i have used to receive the 2 byte response..
There seems to be nothing wrong with your code. At least to the extent I could reproduce, it only sends the command once (I tried your function after setting up my serial port in loopback).
I cannot say for sure but it might be that the terminal you're using has two windows, one for input and another one for output and somehow you're getting confused with what is in and out of your port.
One easy way to deal with this kind of issue is to use a sniffer on your port. You can do that combining com0com and Termite on Windows, as I recently explained here.
As you can see there is only one window on this terminal, and after setting up the forwarding you'll everything that comes in and out of your port. That should make it easier to see what your code is writing and reading.
To give you a conventional scenario to apply the sniffer trick you can refer to the following screenshot:
In this case, we have two real serial ports on a computer. On the first (COM9) we are running a Modbus server (you can imagine it as a bunch of memory addresses, each of one storing a 16-bit number). On COM10 we have a client that is sending queries asking for the contents of the first 10 addresses (called registers using the Modbus terminology). In a general use case, we have those ports linked with a cable, so we know (theoretically) that the client on COM10 is sending a data frame asking for those ten registers and the server on COM9 is answering with the numbers stored on those registers. But we are only able to see the contents on the server (left side of the picture) and what the client is receiving (right). What we don't see is what is traveling on the bus (yeah, we know what it is, but we don't know exactly how the Modbus protocol looks like on the inside).
If we want to tap on the bus to see what is being sent and received on each side we can create a couple of virtual ports with com0com and a port forwarding connection with Termite, something like the following screenshot:
Now we have moved our Modbus server to one of the virtual serial ports (COM4 in this case). After installing com0com we got (by default, but you can change names or add more port pairs, of course) a pair of forwarded ports (COM4<-->COM5). Now, if we want to see what is circulating through the ports we open Termite (bottom-right side of the picture) and set up another port forwarding scheme, in this case from virtual port COM5 to the real port COM9.
Finally (and exactly the same as before we were sniffing), we have COM9 connected together with COM10 with a cable. But now we are able to see all data going to and fro on the bus (all those HEX values you see on Termite displayed with the green/blue font).
As you can see, this will offer something similar to what you can do with more professional tools.

python pyserial won't pull data from port

Having trouble with reading from a serial port.
i have a bit of hardware that shows a display with date and time and other details and on the PC you through some software you can bring up a live mirror of the display. what i want to do is log some data at intervals.
using pyserial its trivial to connect to the port, but when reading it using the simplest settings with timeout=none and read size =1 and leaving the port listening indefinitely there is no output from the equipment.
ive tested using virtual port pairs and i can read and write no problem.
Any ideas why there is no output from the serial port?
And i know its the right port because when i open it then it blocks the mirrored display on the PC.
Cheers

Using Pyserial to read and write data

I am trying to understand how Pyserial is interfacing with my windows machine. I am using this basic code from the pyserial website.
import serial
ser = serial.Serial('COM1') # open serial port
print(ser.name) # check which port was really useds
ser.write(b'hello') # write a string
ser.close() # close port
I am using python 2.7 and IDLE as the IDE for this code, I am wondering should I be able to read the string hello from my console after I run the code? My output as of right now only show COM1, while not printing hello to my console. Is the hello message being sent to my COM1 port? If so, how would I be able to read the message I sent?
Also as a bonus question, I sometimes randomly have this error happen to me.
SerialException: could not open port 'COM1': WindowsError(5, 'Access is denied.'). Sometimes when I re-run the code it goes away, other times I have to restart my computer. I appreciate any help, thanks.
COM1 is not connected to anything currently.
You should open the connection first with ser.open(). You can write the command like you are doing, but to read it, you need to use ser.readlines(). Then you can close the connection. This page has a lot of good info! What is connected to the COM1 port?

I have problem in sending a data via serial port using python

I am trying to ON my application by sending a command called ON via serial port using PYTHON..I already wrote a program in my controller that when ever i receive a command via serial port it has to perform some operations.
this is my code:
import serial
s=serial.Serial(0)
s.write('^ON') #this is my string to ON
s.close()
but the thing is it can able to read the data send by the controller
but it cant able to write the data in to the controller
Your microcontroller might be expecting "hardware flow control", using the RTS/CTS or DSR/DTR pins on the connector. That is, to receive, it may expect the transmitter to "raise" a certain pin, to alert the controller to prepare for a transmission. This hardware flow control seems to be getting less common, and so is disabled by default in PySerial.
Try this line:
s=serial.Serial(0, rtscts=True)
Or, if that doesn't work, try:
s=serial.Serial(0, dsrdtr=True)
If neither works, try this:
s=serial.Serial(0, rtscts=True, dsrdtr=True)
I hope one of those works for you!
(It might not: a lot of hobby projects' cables hard-wire the flow control pins. But, we'll see!)

Categories