This is my function. i am trying to send the word sensors to the COM Port and then get back the result.
def serialportcommunication():
ser = serial.Serial(
port='COM5',
baudrate=115200
)
print('Writing SENSORS command to COM7')
ser.write(b"sensors")
time.sleep(4)
print('Reading 10 bytes from serial buffer')
read_val = ser.read(size=10)
print(read_val)
print('Done printing output')
Output I get of the code above:
Writing SENSORS command to COM7
Reading 10 bytes from serial buffer
b'sensors\r\n '
Done printing output
If I execute the command "sensors" to the COM Port using a Terminal Program like Putty, I get a wall of text from my target device (the sample output is shown below, I had to white out most of the output though).
This text I am reading back, I want to read it in Python using teh command above of ser.read(size = ??), but I don't read anything back.
How to read it back?
MY SCREENSHOT FROM USING PUTTY
Resolved in comments:
Do you have to hit Enter when typing the command manually? Then you need to do the same here - ser.write(b"sensors\r") (or maybe \n, or \r\n). – jasonharper
Yes, when i enter the command in Putty, I to hit enter. Thank you thank you. I added the '\n in the end and it works now!!!! – Neil Dey
Related
Firstly, I have also researched an seen that this question is answered alot of places, but the answers don't work for me.
I am trying to send ASCII command over serial port to a PCBA and then attempting to receive the response.
HW setup:
Computer
USB-RS232 Cable
PCBA
SW:
Python 3.8
I have sent commands over RS232 terminals (realterm) and they work perfectly (LED turns on) but can't implement the same with python. The command in question is "led_r_on".
And yes, I get "COM5 is open" prompt, meaning COM5 is alive.
Thanks for helps!
code as follows:
import serial
import time
from time import sleep
from serial import Serial
port = 'COM5'
baud = 115200
ser = serial.Serial(port, baud, timeout=2)
if ser.isOpen():
print(ser.name + ' is open...')
sleep(1)
ser.write(b'led_r_on')
print(ser.readline(1000))
Since you didn't give any information on the pcba you are using. I will just assume, that you are missing some kind of end delimiter in your bytesequence. Now there are many ways of ending a transmission over serial. The most frequent ones I ran across are these two:b'led_r_on\r\n' or just b'led_r_on\n'. Try changing your bytes in the ser.write() function to this. If that does not work you might need to use this: b'led_r_on\x04' this will send the EOT (End of Transmission) ASCII character. But I don't think this will be needed.
So I'm trying to control a Thermo Scientific temperature bath over serial (USB-A to USB-B) and when I send a command I get the response "F001" telling me that that command is known. The format is "command" "carriage return" and here is what I have:
ser = serial.Serial('/dev/tty.usbserial-A800dars', 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=5) #no timeout=0
print(ser.name) # check which port was really used
ser.write(b'RT\r') # read internal temp
# TODO probably not getting 100 bytes here, need to find what to expect and whether or not to do a timeout
serial_response = ser.read(100) # read up to one hundred bytes or as much is in the buffer
print(serial_response)
I've tried adding a \n after the \r , I've tried multiple other commands, I've included a space between the RT and the \r and I've set the baud rate on the temperature bath to the same as I am sending over serial. I've also made sure I used the right drivers here from http://www.ftdichip.com/FTDrivers.htm.
Any thoughts? I'm using a Mac, sending over a USB-A to USB-B cable, and I only get the F001 response when the temperature bath is on and running.
Well as it turns out I was given the incorrect manual by the manufacturer. After finding the correct one and now knowing the correct commands to send over serial all the above code works just fine.
I'm relatively new to programming, so bear with me. I'm trying to communicate with the measurement device METEX M-4650CR https://sigrok.org/wiki/Voltcraft_M-4650CR and I'm using a windows 7 64bit OS. I simply want to read out the data the device measures to my python procedure and display it and calculate with it.
I found in the manual http://elektron.pol.lublin.pl/elekp/labor_instr/METEX_M-4650CR_Manual.pdf (page 25ff), that it works with a baudrate of 1200, a bytesize of 7 (with ASCII coding) and 2 stopbits.
Furthermore, it can be requested to send data to the computer by simply giving it the command "M". It then returns 14 bytes to the computer. Without anything to measured connected to it, it should return something like 'DC 00.0000V CR'. CR is the terminator here (I hope that is the right name).
Here is my code:
import pyserial
import time
ser = serial.Serial(port='COM5', baudrate=1200,
bytesize=7, stopbits=2, timeout=1,
rtscts=False, dsrdtr=True)
time.sleep(1)
ser.write("M")
time.sleep(1)
bytestoread = ser.inWaiting()
print bytestoread
output = ''
output += ser.read(1000)
print 'output:' + str(output)
time.sleep(1)
ser.close()
My problem is, that I cannot read out the data properly with pyserial. I send the command "M" to the METEX and in the display it says 'send' for a short moment, so I guess my write command works fine. But after that (it should have send the data), all I get when from ser.inWaitung is '0L' or '1L' and the ser.read command gives nothing at all.
I don't think it is a problem of the hardware, because with another programme, called 'serialwatcher', I'm able read out the data correctly. It gives exactly the characters described in the manual.
I also tried the following while loop, having the problem, that most of the time inWaiting == 0, such that it never initialises the loop.
while ser.inWaiting() > 0:
output += ser.read(1)
if output != '':
output = outpus.rstrip()
print output
So, how can I read out the data correctly, that were send to the serial port? Thanks in advance.
Unfortunately I cannot test your code because I have no serial device with me, but you could try the following:
You could set a flag, e.g. alive when you are expecting data and simply try to read something. This worked for me when I was trying to receive data from a really old spectrometer.
while alive: #loop
text = ser.read(1) #try to read one line
if text: #if there is data
n = ser.inWaiting() #look if there is more to read
if n: #if so
text = text + ser.read(n) #get all of it
A more sophisticated example can be found here wxTerminal - Pyserial example You could also simply try to modify this brilliant code for your purpose and see if you are more successful.
I am using the GhostPDL pcl6 application to print text files to PDFs. The text files contain captured text from a serial port connected to a PLC controller. Some of the print data is from an older system that used only line feed LF for end of line termination and did not provide the CR carriage return character. The results were the PDF prints had the "Stair Stepping" problem. A laser printer can be set to AUTO CR to allow the prints to print normally. However, I could find no solution to set up the -sDEVICE=pdfwrite settings in the pcl6 print command to add the auto CR.
My serial capture program is based on a modified version of pySerial tcp_serial_redirect.py.
https://github.com/jaredly/pydbgp/blob/master/symbian/serial_tcp_redirect.py
This is used on a Raspberry Pi to be used as a programmable Device Server. The serial print of forwarded to an Ethernet printer port 9100.
Along with other features, in order to capture the serial data to a text file, I added a write to file command to the program:
if data:
logfile = open("/var/www/active_log.txt","a")
logfile.write(data)
logfile.close()
The problem was the text file did not have the required CR characters. The Python program in Windows creates a text file and converts the LF to LF/CR but not on the Raspberry Pi.
My solution was found in the original pyserial program:
http://sourceforge.net/p/pyserial/code/HEAD/tree/trunk/pyserial/examples/tcp_serial_redirect.py
I simplified the join and split formula.
if data:
data = '\r\n'.join(data.split('\n'))
logfile = open("/var/www/active_log.txt","a")
logfile.write(data)
logfile.close()
After hours of searching for a solution, this one seems to be the simplest. I tried stty settings and other avenues. Perhaps this may help someone or maybe there is a better solution?
As I understand it you are trying to replace all the \n with \r\n. How about something like this?
data = data.replace('\n', '\r\n')
Hope this helps.
I am new to Pyserial and Hardware area. I am trying to run the sample applications given in http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports
import serial
ser = serial.Serial(0) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
ser.close()
I have written this program in a python file and running it. Now if I want to test this application to check if I am sending correct string (eg: Hyperterminal or something) how can I do this. Can anyone guide me?
Use virtual serial port for your test.
For Windows I use com0com and socat for Linux.
Then, use Putty for visualization of your send.
Another quick way to test a physical serial port is to take a wire/screwdriver, crocodile clamp or anything that you have in hand and bridge the RX and TX (receive and transmit) together. At that point, everything that you send out will be looped back at you. YOu can receive it afterward by using this code snippet here:
import serial
ser = serial.Serial(0, timeout = 1) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
msg = ser.read("100") #read the content of the input buffer until you get 100 byte or a timeout event
print(msg) #print the content you might need to decode it print(decode(msg))
ser.close()
The key aspect again for this code to work properly is to bridge RX and TX together. A lot of tutorial will show you how to do this.