I dont know how to send and integer by serial port from raspberry to arduino

I am trying to connect my raspberry pi with an arduino mega via usb. I did the connection and I can send an integer but only if i put the number by keyboard here is the code:

import serial
import time
arduino=serial.Serial('/dev/ttyUSB0',baudrate=9600, timeout = 3.0)
arduino.close()
arduino.open()

var = raw_input("Introducir un Comando: ")
arduino.write(var)

It works

but if I want to send a varible integer like

var=1 it appears an error saying that the port is close, only making this change. I tried using var =1, var ='1' var ="1" but nothing.

Can anyone help me? thanks and sorry for my bad english.

Answers


My friends helped me. The way to send is:

after open the port wait with the time library 1 second.

import time
time.sleep(1)

and then the way to send it is:

var=b"3"

and we send a number 3. Thanks to everyone.


Need Your Help

mako template recursion: AttributeError: 'Undefined' object has no attribute 'id'

recursion undefined mako python-3.2

I've made a template for representing a tree structure. Each node of the tree has an id, a name, a list of children (tree_children) and an expanded property.

Dropzone: prevent addfile twice

javascript dropzone.js multifile-uploader

I'm using dropzone to upload images to gallery. I'm submiting by button. Is it possible to prevent adding same file twice? I'm not really sure if checking name or name and size. Here's my code: