Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a write() method for UTF8Strings #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.3
Compat
Conda
Docile
PyCall
StringEncodings
45 changes: 26 additions & 19 deletions src/SerialPorts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down