From f1e8ce436f12c7dde9d1d21023b15b51ddbdfa65 Mon Sep 17 00:00:00 2001 From: Chandrashekhar Dhulipala Date: Wed, 1 Jun 2016 14:31:04 +0530 Subject: [PATCH 1/6] Update REQUIRE Added StringEncodings dependency --- REQUIRE | 1 + 1 file changed, 1 insertion(+) diff --git a/REQUIRE b/REQUIRE index ce3fc73..afe7d13 100644 --- a/REQUIRE +++ b/REQUIRE @@ -3,3 +3,4 @@ Compat Conda Docile PyCall +StringEncodings From 8439872fab7f07c493cdef804f5172cb139eddab Mon Sep 17 00:00:00 2001 From: Chandrashekhar Dhulipala Date: Wed, 1 Jun 2016 14:43:57 +0530 Subject: [PATCH 2/6] Added write method to accept Unicode Strings --- src/SerialPorts.jl | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/SerialPorts.jl b/src/SerialPorts.jl index be11832..36b56db 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, Conda, PyCall, StringEncodings VERSION < v"0.4-" && using Docile const PySerial = PyCall.PyNULL() @@ -93,6 +93,31 @@ 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 bytes[1] == 87 + if sizeof(bytes) == 3 serialport.python_ptr[:write](bytes) end + if sizeof(bytes) == 4 + if bytes[3] == 195 bytes[4] = bytes[4] + 64 end + three_bytes = [ bytes[1] , bytes[2] , bytes[4] ] + serialport.python_ptr[:write](three_bytes) + end + + elseif bytes[1] == 77 + if sizeof(bytes) == 3 || sizeof(bytes) == 4 serialport.python_ptr[:write](bytes) end + if sizeof(bytes) == 5 + if bytes[4] == 195 bytes[5] = bytes[5] + 64 end + four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ] + serialport.python_ptr[:write](four_bytes) + end + + else + serialport.python_ptr[:write](bytes) + end + +end + function Base.read(ser::SerialPort, bytes::Integer) ser.python_ptr[:read](bytes) end From c0d713282bfd761f3ace3c2bc34a82480e130290 Mon Sep 17 00:00:00 2001 From: Chandrashekhar Dhulipala Date: Tue, 14 Jun 2016 12:26:20 +0530 Subject: [PATCH 3/6] Update write function for Unicode strings --- src/SerialPorts.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/SerialPorts.jl b/src/SerialPorts.jl index 36b56db..35ffd87 100644 --- a/src/SerialPorts.jl +++ b/src/SerialPorts.jl @@ -110,7 +110,14 @@ function Base.write(serialport::SerialPort, data::UTF8String) if bytes[4] == 195 bytes[5] = bytes[5] + 64 end four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ] serialport.python_ptr[:write](four_bytes) - end + end + + elseif bytes[1] == 83 + if sizeof(bytes) == 4 serialport.python_ptr[:write](bytes) end + if sizeof(bytes) == 5 + four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ] + serialport.python_ptr[:write](four_bytes) + end else serialport.python_ptr[:write](bytes) From 6e93442d1a400e96c00660760e70cbf2ecc03217 Mon Sep 17 00:00:00 2001 From: Chandrashekhar Dhulipala Date: Mon, 20 Jun 2016 14:34:54 +0530 Subject: [PATCH 4/6] follow up to original repo --- src/SerialPorts.jl | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/SerialPorts.jl b/src/SerialPorts.jl index 35ffd87..7380ea7 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, StringEncodings +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) From 976fe0172e5ad4fda067113f18a56ef50ddb41ef Mon Sep 17 00:00:00 2001 From: Chandrashekhar Dhulipala Date: Mon, 20 Jun 2016 14:35:57 +0530 Subject: [PATCH 5/6] remove conda dependency --- REQUIRE | 1 - 1 file changed, 1 deletion(-) diff --git a/REQUIRE b/REQUIRE index afe7d13..89f8f67 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,6 +1,5 @@ julia 0.3 Compat -Conda Docile PyCall StringEncodings From 4b7817ee4da836e80ebd7459b39e1dbf354fb71b Mon Sep 17 00:00:00 2001 From: Chandrashekhar Dhulipala Date: Mon, 20 Jun 2016 14:44:22 +0530 Subject: [PATCH 6/6] simplify write method --- src/SerialPorts.jl | 49 +++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/SerialPorts.jl b/src/SerialPorts.jl index 7380ea7..8726c78 100644 --- a/src/SerialPorts.jl +++ b/src/SerialPorts.jl @@ -78,35 +78,26 @@ function Base.write(serialport::SerialPort, data::ASCIIString) end function Base.write(serialport::SerialPort, data::UTF8String) - bytes = encode(data,"UTF-8") - - if bytes[1] == 87 - if sizeof(bytes) == 3 serialport.python_ptr[:write](bytes) end - if sizeof(bytes) == 4 - if bytes[3] == 195 bytes[4] = bytes[4] + 64 end - three_bytes = [ bytes[1] , bytes[2] , bytes[4] ] - serialport.python_ptr[:write](three_bytes) - end - - elseif bytes[1] == 77 - if sizeof(bytes) == 3 || sizeof(bytes) == 4 serialport.python_ptr[:write](bytes) end - if sizeof(bytes) == 5 - if bytes[4] == 195 bytes[5] = bytes[5] + 64 end - four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ] - serialport.python_ptr[:write](four_bytes) - end - - elseif bytes[1] == 83 - if sizeof(bytes) == 4 serialport.python_ptr[:write](bytes) end - if sizeof(bytes) == 5 - four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ] - serialport.python_ptr[:write](four_bytes) - end - - else - serialport.python_ptr[:write](bytes) - end - + 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)