# Issue with readBytes

**URL:** <https://forum.inductiveautomation.com/t/issue-with-readbytes/5053>\
**Category:** Ignition\
**Created:** [August 24, 2012, 6:34pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053 "2012-08-24T18:34:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markdobtech](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@markdobtech](https://forum.inductiveautomation.com/u/markdobtech)\
**Post date:** [August 24, 2012, 6:34pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053/1 "2012-08-24T18:34:07Z")

</div>

I am testing system.serial.readBytes. I am experiencing an issue with data returned. I have a serial monitor between the devices and have verified that the device is returning expected data. However, using readBytes the data is garbled. Additionally, I ran a test using readLine in place of readBytes in the same code segment and it works fine. I am using serial module version 1.5.2.31. The code is a command out/response in scheme. i.e. command out = ‘TA\*’, device response upon receiving command. The device we are testing is a red lion cub 5 counter with RS232. The data returned from the device is 20 bytes in length, this includes terminating .

The readLine code fragment is:  
…  
try:  
value = system.serial.readLine(event.source.parent.comPort,10)  
event.source.parent.dataInA = value  
…  
The string returned in value is the expected:  
 CTA 6763  
Note: any data enclosed by \<\> is a substitution of the actual byte for clarity in reading string.

When using readBytes in place of readLine implemented as follows,  
…  
try:  
value = system.serial.readBytes(event.source.parent.comPort,20,10)  
event.source.parent.dataInA = value  
…

The string returned in value is:  
ICAgQ1RBICAgICAgICA4ODkyDQo

In both cases there were no changes with initializing and open/closing the port. readLine appears to work as expected, readBytes does not.

I have experimented with changing the timeout, but it does not appear to affect the quality of the return string.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 24, 2012, 6:43pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053/2 "2012-08-24T18:43:37Z")

</div>

Does system.serial.readBytesAsString() return the value you’re expecting?

---

<div class="post-metadata">

**Author:** ![markdobtech](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@markdobtech](https://forum.inductiveautomation.com/u/markdobtech)\
**Post date:** [August 24, 2012, 7:41pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053/3 "2012-08-24T19:41:07Z")

</div>

I just tested ‘readBytesAsString’ and it works fine as opposed to ‘readBytes’.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 24, 2012, 7:53pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053/4 "2012-08-24T19:53:48Z")

</div>

readBytes() returns a java.lang.byte[], so whatever you’re stuffing that into is probably not interpreting or displaying it properly. readBytesAsString() converts that byte[] into a String before returning it.

---

<div class="post-metadata">

**Author:** ![markdobtech](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@markdobtech](https://forum.inductiveautomation.com/u/markdobtech)\
**Post date:** [August 24, 2012, 8:21pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053/5 "2012-08-24T20:21:11Z")

</div>

It is being stuffed into a custom property of type string.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 24, 2012, 8:40pm UTC](https://forum.inductiveautomation.com/t/issue-with-readbytes/5053/6 "2012-08-24T20:40:24Z")

</div>

Yeah, that wouldn’t (and shouldn’t) work. You would have to convert it to a String first. Or use readBytesAsString().
