How to send int to arduino with Python - python

I was trying to control a Servo with Python but I have only Arduino to control a Servo. I was found how to control servo with writing numbers but I can't send int to arduino.I was trying to send this
first = 680/ a
last = 180/ first
I was trying to send 'last' but I can't do it. Please help me and sorry for my bad English.

Here is code that connects to an Arduino and sends it data:
import serial
port = serial.Serial("/path/to/your/arduino/port", 9600)
port.write(bytes("180", "utf-8")) # This is just an example; you can use any string
# Close the port when you're done talking to the Arduino
port.close()
Notice that you need to convert the string to bytes before sending it, and that you need to specify the encoding. Just use whatever encoding your program uses. Here, it's UTF-8.

Related

USB Serial data sending gibberish

i am having an issue i cannot seem to resolve. i am using python on the raspberry pi to read from a usb connection on the pi (that is being converted from serial).
i am able to connect to the usb port and start receiving data with the code
ser = serial.Serial("myUsbPortID", 9600)
bytes = ser.inWaiting()
print ser.read(bytes)
i know that the baudrate is 9600 (hardware manufacturers docs) but for some reason when i try to read the stream of data i get a lot of gibberish in the form of different languages and characters. After i kill the program my screen still replaces my characters with the gibberish data as i type.
i'm sure this isn't the stream of data the hardware is sending. something somewhere is converting things but i have no idea what it may be.
when i boot up the device and it is initializing then i get readable information. but when the device start operating i only get this weird characters
is there a way to convert these characters to the data that it is actually coming in as?
example pic:
output screen
so i was able to solve the issue of the gibberish with this line on python code (for anyone else having this issue).
data = ":".join("{:02x}".format(ord(c)) for c in bytes)
where bytes is the raw data, i'm making it ':' delimited.
seems the connection and baud rate was ok. luckily the data was able to convert to hex ok.

How to read data sent to an XBee on Windows?

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

Moving Sign Communication Protocol v1.2 with Raspberry Pi

This is my first post here so forgive me if some questions might be out of scope, but basically I'm trying to write my own program to push messages from a Raspberry Pi to a LED scrolling sign via serial communication (USB on Pi -> RS232 adapter -> LED sign).
Im not very familiar with serial communication in general, but am attempting to use the PySerial library on the Pi (http://pyserial.sourceforge.net/pyserial.html) in the format of Moving Sign Protocol V1.2 (http://www.brgprecision.com/pdffiles/Protocol12.pdf).
Here is my python code thus far, error free, yet the plugged in sign doesnt receive any data.
import serial
#default port is /dev/tty/USB0
#portname, baudrate, timeout
port = serial.Serial('/dev/ttyUSB0', 9600)
port.open() port.write('0x00\0x01\"FF"\"03"\0x02\'A'\'A'\'A'\'2'\'2'\'7F'\'0100'\'1200'\'000'\'1'\"OMFG"\0x03\"0564"\0x04')
port.close()
Basically, I dont know how to parse the message Im trying to write into proper serial data packages. Do I send the protocol and text message all in one go like above? Or must I parse each of the fields and send them separately like:
port.write('01') # start of head
port.write('46') # pc address
port.write('46') # number 1 display
ect...
I should mention that I also have sniffed the USB serial communication on my PC and can confirm this serial information is correct, I just have no real idea how to use it on a RPi. Any help will be much appreciated!
you want to use bytes
0x00 == 0 == "\x00"
which is not the same as "0x00" which is really "\x30\x78\x30\x30" which is 4 characters not the single byte that you are hoping to achieve
so to send the part A
ser.write("\x00"*5) # 5 null bytes
to send the SOH part
ser.write("\x01")
Im not sure what the address stuff is... but that hopefully gives you an idea

Decoding Simulink's UDP Packets in Python

I have a Simulink model sending data via UDP to another program (Blender) where I can receive the packets, but I have not been able to figure out how to correctly decode them.
In the Simulink model I just have it sending a value that is based upon a sine wave, nothing fancy, just a single value like 1.452 or something. In Blender I have it spitting out the data it receives from the packet, and I'm receiving stuff like:
b'<\xa6ya\x05\x93\xe3?'
I have no idea how to decode this. It seems to have some hex values, but beyond that I'm lost. I'm not even sure what all this data contains. Is it just the value from Simulink, or does it contain information regarding things like the sender and receiver IP addresses, ports, etc...?
UPDATE:
I updated the Simulink model to transmit a constant value via UDP for debugging/investigation. The value is 0.5234, and the data that my Python script is spitting out is:
b'\xab>W[\xb1\xbf\xe0?'
Which, when converted into hexadecimal reads (using hexlify):
b'ab3e575bb1bfe03f'
How would I extract/decode 0.5234 out of that?
Thanks for any help!
You can use struct to decode your binary data, in this case it seems to be a double value:
>>> import struct
>>> struct.unpack('d', b'\xab>W[\xb1\xbf\xe0?')
(0.5234,)

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