Skip to content

Commit 1d42efc

Browse files
committed
Have adm command respond with ready\r\n before listening for binary writes.
This forces the user to check for potential errors in the command without incurring a readlines timeout penalty if there are no errors. Also adds example to the readme of how a binary write works.
1 parent 6eeb87d commit 1d42efc

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@ end\n
107107

108108
<img width="470" alt="documentation" src="https://github.com/pmiller2022/prawn_digital_output/assets/75953337/932b784f-346f-4598-8679-b857578e0291">
109109

110+
This is a python script that employs the binary write `adm` command.
111+
In order to function correctly, the data to be written must be in a numpy structured array where each column dtype can be specified uniquely.
112+
Below assumes the `bits` and `cycles` are 1-D arrays that have already been created.
113+
It also assumes `do` is a pyserial handle to the device.
114+
115+
```python
116+
data = np.zeros(len(bits), dtype=[('bits', 'u2'), ('cycles','u4')])
117+
data['bits'] = bits
118+
data['cycles'] = cycles
119+
120+
serial_buffer = data.tobytes()
121+
122+
do.write(f'adm 0 {len(data):x}\r\n'.encode())
123+
resp = do.readline().decode()
124+
assert resp == 'ready\r\n', f'Not ready for binary data. Response was {repr(resp)}'
125+
do.write(serial_buffer)
126+
resp = do.readline().decode()
127+
if resp != 'ok\r\n':
128+
# if response not ok, probably more than one line, read them all
129+
# done this way to prevent readlines timeout for standard operation
130+
extra_resp = do.readlines()
131+
resp += ''.join([st.decode() for st in extra_resp])
132+
print(f'Write had errors. Response was {repr(resp)}')
133+
```
134+
110135
## Compiling the firmware
111136

112137
If you want to make changes to the firmware, or want to compile it yourself (because you don't trust binary blobs from the internet), we provide a docker configuration to help you do that.

prawn_do/prawn_do.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,15 @@ int main(){
525525
int parsed = sscanf(serial_buf, "%*s %x %x", &start_addr, &inst_count);
526526
if(parsed < 2){
527527
fast_serial_printf("Invalid request\r\n");
528+
continue;
528529
}
529-
530530
// Check that the instructions will fit in the do_cmds array
531-
if(inst_count + start_addr > MAX_INSTR){
531+
else if(inst_count + start_addr > MAX_INSTR){
532532
fast_serial_printf("Invalid address and/or too many instructions (%d + %d).\r\n", start_addr, inst_count);
533+
continue;
534+
}
535+
else{
536+
fast_serial_printf("ready\r\n");
533537
}
534538

535539
// reset do_cmd_count to start_address

0 commit comments

Comments
 (0)