diff --git a/REQUIRE b/REQUIRE index ce3fc73..89f8f67 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,5 +1,5 @@ julia 0.3 Compat -Conda Docile PyCall +StringEncodings diff --git a/src/SerialPorts.jl b/src/SerialPorts.jl index be11832..8726c78 100644 --- a/src/SerialPorts.jl +++ b/src/SerialPorts.jl @@ -5,7 +5,7 @@ module SerialPorts export SerialPort, SerialException, setDTR, list_serialports, check_serial_access -using Compat, Conda, PyCall +using Compat, PyCall, StringEncodings VERSION < v"0.4-" && using Docile const PySerial = PyCall.PyNULL() @@ -28,24 +28,8 @@ immutable SerialPort <: IO end function __init__() - try - copy!(PySerial, pyimport("serial")) - copy!(PySerialListPorts, pyimport("serial.tools.list_ports")) - catch e - if PyCall.conda - info("Installing serial via the Conda package...") - Conda.add("pyserial") - copy!(PySerial, pyimport("serial")) - copy!(PySerialListPorts, pyimport("serial.tools.list_ports")) - else - error("""Failed to pyimport("serial"): SerialPorts will not work until you have a functioning pyserial module. - For automated serial installation, try configuring SerialPorts to use the Conda Python distribution within Julia. Relaunch Julia and run: - ENV["PYTHON"]="" - Pkg.build("Serial") - using PyPlot - pyimport exception was: """, e) - end - end + copy!(PySerial, pyimport_conda("serial", "pyserial")) + copy!(PySerialListPorts, pyimport("serial.tools.list_ports")) end function serialport(port, baudrate) @@ -93,6 +77,29 @@ function Base.write(serialport::SerialPort, data::ASCIIString) serialport.python_ptr[:write](data) end +function Base.write(serialport::SerialPort, data::UTF8String) + bytes = encode(data,"UTF-8") + if sizeof(bytes) == length(data) + serialport.python_ptr[:write](bytes) + else + i = 1 + a = Array(Int64,1) + while i <= sizeof(data) + if sizeof(string(data[i:i])) == 2 + if bytes[i] == 195 bytes[i+1] = bytes[i+1]+64 end + push!(a,i+1) + i = i+2 + else + push!(a,i) + i = i+1 + end + end + a = a[2:end] + bytes = bytes[a] + serialport.python_ptr[:write](bytes) + end +end + function Base.read(ser::SerialPort, bytes::Integer) ser.python_ptr[:read](bytes) end