BerryScript: Change serial port parity on the fly #22272
-
I'm trying to emulate 9-bits RS-485 communication using parity bit. def write(buf)
end` The code works, but only for some calls of the output() function, then, after 20-30 calls of the output() every 1 sec, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
You are not supposed to recreate the serial object repeatedly, it has some deep consequences for the underlying layers. If it's about changing the parity, let me explore if there are other options possible |
Beta Was this translation helpful? Give feedback.
-
Yes, I need change the serial port parity settings without to recreate the serial() object. |
Beta Was this translation helpful? Give feedback.
-
Good news is esp-idf does have a function |
Beta Was this translation helpful? Give feedback.
-
I merged a new feature to change parity. Now you code should be: var parityb = bytes('9669699669969669699696699669699669969669966969969669699669969669')
var ser = serial(-1, 39, 9600, serial.SERIAL_8N1)
def write(buf)
var ch
var ser
ch = buf[0]
if b.setbits(ch, 1) == 0
ser.config(serial.SERIAL_8O1)
else
ser.config(serial.SERIAL_8E1)
end
ser.write(ch)
ser.flush()
end I took the liberty to optimize your big array of 256 elements takes more than 4KB of RAM (which is huge). You can use a bit-array instead which consumes only 204 bytes in total. To compute it, you can do: var parity=[
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0]
var b = bytes()
b.resize( (size(parity)+7) / 8)
for i: 0..size(parity)-1
b.setbits(i, 1, parity[i])
end
print(b)
# prints bytes('9669699669969669699696699669699669969669966969969669699669969669') |
Beta Was this translation helpful? Give feedback.
I merged a new feature to change parity.
Now you code should be:
I took the liberty to optimize your big array of 256 elements takes more than 4KB of RAM (which is huge). You can use a bit-array instead which consumes only 204 bytes in total.
To compute it, you can do: