Hi,
You said that the code that you currently have works when you have Highlighted Excution on (light bulb); this leads me to belive that you have a timing issue. You can start by adding saome delays between the read and write functions, this way yuo give enough time to the instrument to respond to each command.
Please let me know if you have more specific questions on this.
Regards,
Juan Carlos.