Skip to content

Commit 8103bb0

Browse files
cshekhardsjkelly
authored andcommitted
simplify write method
(cherry picked from commit 4b7817e)
1 parent f385c84 commit 8103bb0

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
@@ -84,35 +84,26 @@ function Base.write(serialport::SerialPort, data::SerialString)
8484
end
8585

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

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

0 commit comments

Comments
 (0)