c# - System.IO.IOException error -


i wrote program reads serial port. no problem program run on computer has visual studio installed. ok. when copied release folder program , run have error system.io.ioexception. use code read data serial port.

byte[] buffer = new byte[42]; int readbytes = 0; int totalreadbytes = 0; int offset = 0; int remaining = 41;  try {         {         readbytes = serial.read(buffer, offset, remaining);         offset += readbytes;         remaining -= readbytes;         totalreadbytes += readbytes;     }      while (remaining > 0 && readbytes > 0); } catch (timeoutexception ex) {     array.resize(ref buffer, totalreadbytes); }   utf8encoding enc = new utf8encoding(); recieved_data = enc.getstring(buffer, 27, 5);                 dispatcher.invoke(dispatcherpriority.send, new updateuitextdelegate(writedata), recieved_data); 

how can solve problem ?

it seems reading more bytes port have transmitted, should check bytestoread property check how many are.

byte[] buffer = new byte[port.bytestoread]; int readbytes = 0; int totalreadbytes = 0; int offset = 0; int remaining = port.bytestoread;  try {         {         readbytes = serial.read(buffer, offset, remaining);         offset += readbytes;         remaining -= readbytes;         totalreadbytes += readbytes;     }      while (remaining > 0 && readbytes > 0); } catch (timeoutexception ex) {     array.resize(ref buffer, totalreadbytes); } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -