Skip to content

Commit db0df59

Browse files
cshekhardsjkelly
authored andcommitted
simplify write method
(cherry picked from commit 4b7817e)
1 parent a7ac66e commit db0df59

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/SerialPorts.jl

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,35 +78,26 @@ function Base.write(serialport::SerialPort, data::ASCIIString)
7878
end
7979

8080
function Base.write(serialport::SerialPort, data::UTF8String)
81-
bytes = encode(data,"UTF-8")
82-
83-
if bytes[1] == 87
84-
if sizeof(bytes) == 3 serialport.python_ptr[:write](bytes) end
85-
if sizeof(bytes) == 4
86-
if bytes[3] == 195 bytes[4] = bytes[4] + 64 end
87-
three_bytes = [ bytes[1] , bytes[2] , bytes[4] ]
88-
serialport.python_ptr[:write](three_bytes)
89-
end
90-
91-
elseif bytes[1] == 77
92-
if sizeof(bytes) == 3 || sizeof(bytes) == 4 serialport.python_ptr[:write](bytes) end
93-
if sizeof(bytes) == 5
94-
if bytes[4] == 195 bytes[5] = bytes[5] + 64 end
95-
four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ]
96-
serialport.python_ptr[:write](four_bytes)
97-
end
98-
99-
elseif bytes[1] == 83
100-
if sizeof(bytes) == 4 serialport.python_ptr[:write](bytes) end
101-
if sizeof(bytes) == 5
102-
four_bytes = [ bytes[1] , bytes[2] , bytes[3] , bytes[5] ]
103-
serialport.python_ptr[:write](four_bytes)
104-
end
105-
106-
else
107-
serialport.python_ptr[:write](bytes)
108-
end
109-
81+
bytes = encode(data,"UTF-8")
82+
if sizeof(bytes) == length(data)
83+
serialport.python_ptr[:write](bytes)
84+
else
85+
i = 1
86+
a = Array(Int64,1)
87+
while i <= sizeof(data)
88+
if sizeof(string(data[i:i])) == 2
89+
if bytes[i] == 195 bytes[i+1] = bytes[i+1]+64 end
90+
push!(a,i+1)
91+
i = i+2
92+
else
93+
push!(a,i)
94+
i = i+1
95+
end
96+
end
97+
a = a[2:end]
98+
bytes = bytes[a]
99+
serialport.python_ptr[:write](bytes)
100+
end
110101
end
111102

112103
function Base.read(ser::SerialPort, bytes::Integer)

0 commit comments

Comments
 (0)