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.
Related
I am trying to write a program in python that connects to a DGT chess board and recieves the data and prints the moves in real time as they are being made (example: ng1f3). Ive tried asking chat gpt but as usual it has been no help. Ive also looked online for similar projects but found nothing like what i am trying to do.
ive tried getting the data using the serial library. the port and the baudrate are both correct. the board sends 8 bytes of data for each move. this code runs but does not print anything when i move the pieces on the board. ive even tried contacting the manufacturer to see if the boards have any sort of protection against this but they said they couldnt help me with this and instead just sent me the document with the description of the comunication protocol (one drive link to file: https://1drv.ms/b/s!AqF5qQ2nPQ81nkBZqQAMfY93mfdJ?e=eia1mO). i am also fairly new to python and this website.
import serial
ser = serial.Serial("COM7", 9600)
while True:
data = ser.read(8)
print(data)
edit: I have since tried:
import serial
ser = serial.Serial("COM7", 9600, timeout=2)
ser.write(bytes([0x45])) # message to request serial number
test = ser.read(20)
output = test.decode('ascii', errors='replace') # convert bytes to string print(output)
A response was received, but it wasn't quite as expected.
If you feel confident that everything is connected properly, try doing this:
import serial
ser = serial.Serial("COM7", 9600, timeout=2)
ser.write(bytes([0x45])) # I think this is the message to request serial number.
test = ser.read(20) # Not sure how many bytes are returned
print(test) # this prints a byte string
print(test.hex()) # this prints the byte string as a string of hex w/out the "0x/" characters
print("The Message ID is " + str(test[0:1])) # You sent a'0x45' command you get back a'0x91' reply
print("The length of the message is " + str(test[1:3]))
print("The Serial number is " + str(test[3:8]))
Does that print anything at all?
edit: Changed the serial write to a byte type.
edit 2: It looks like the board might write something upon start up. Another thing to try is:
import serial
ser = serial.Serial("COM7", 9600, timeout=5) # timeout long enough to turn on board
test = ser.read(1000) # excess amount to read, but we don't want to miss anything
print(test)
edit 3: Now that we have verified communications with the board, we can start experimenting with trying to write a script that will read the movement of the chess pieces. Let's try this:
import serial
ser = serial.Serial("COM7", 9600, timeout=2)
DTG_REQ_UPDATE_BOARD = bytes([0x44])
while 1:
timeout = 0
ser.write(DTG_REQ_UPDATE_BOARD)
while timeout < 10:
movement = ser.read(20)
if movement != 0:
print(movement)
timeout = 10
else:
timeout += 1
continue
Turn on your board.
Start the above script.
Start moving pieces like you would if you were playing a game.
Look for things to start printing out in your console/terminal.
It's a quick and dirty way of looping (it's an infinite loop and you will have to force stop the process when you are done running your script), but it should do the job for testing purposes.
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.
Hi there i'm quite new with xBee and struggle with data transferring. My objective is to take picture with Raspberry pi wide and send it back to computer via xBee by turn that image to hexlify code. after recieving the code with python on computer i use binascii library to turn those code back to image by this code
ASCII to IMG:
import binascii
with open("file.txt", "r") as f:
data=f.read()
data = data.strip()
data = data.replace('\n', '')
data = binascii.a2b_hex(data)
with open('image.png', 'wb') as image_file:
image_file.write(data)
but after running that code the image is corrupted. So i start taking a look at the receiving code but I'm not sure if the code is correct, because the text file that i got has a lot of "0" in it
Receiving code:
from digi.xbee.devices import XBeeDevice
PORT = 'COM11'
BAUD = 19200
ser = XBeeDevice(PORT, BAUD)
try :
ser.open()
def data_receive_callback(xbee_message):
data = xbee_message.data.decode("utf-8")
with open("file.txt","a") as f:
f.write(data)
ser.add_data_received_callback(data_receive_callback)
print("Waiting for data...\n")
input()
finally:
if ser is not None and ser.is_open():
ser.close()
camera code in RPi:
from picamera import PiCamera
import serial
import binascii
ser =serial.Serial(
port='/dev/ttyS0',
baudrate=19200,
parity= serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
camera=PiCamera()
camera.resolution(1920,1080)
camera.capture("img.png")
with open("image.png",'rb') as f:
content=f.read()
a=binascii.hexlify(content)
ser.write(a)
ser.close
What should I do or try to fix the code. I think its the receiving code that is a main problem.
Ps. i already try to convert image file to hexlify in both computer and Raspberry Pi and reverse it back and it still work fine.
Questions:
Why are you using an XBee instead of Wi-Fi and a standard TCP protocol like HTTP or FTP?
Work on debugging a piece of your system at a time. Instead of capturing an image on the Pi, use a small text file and see if it comes through correctly. It will be easier to see if you're dropping bytes in the middle, beginning or end of the file.
Whenever using an XBee module, be sure to enable hardware flow control (pins D6 and D7) so you don't lose any serial data. With hardware flow control, each side of the connection has the ability to signal the other side to temporarily stop sending while it processes data.
I also recommend increasing your baud rate to 115200 or even 230400 bps. That, in conjunction with the hardware flow control, will result in quicker transfers without lost bytes.
There's no need to hexlify the data -- the XBee is capable of sending 8-bit bytes when running in transparent serial mode (essentially a serial cable replacement).
That said, you don't have any way to indicate the start or end of the file -- the receiver doesn't know when the image begins or when to finish and close the file. If you continue to hexlify the data, you could send characters other than 0-9a-f to indicate that it's the start of an image or the image is complete.
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
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.