Read char data from Arduino in C# -
so developing code able read data coming arduino through serialport
. defined characters want read arduino , depending on receive send arduino.
what receive based on 3 characters: @, r , \r.
string arroba = "@" + "r" + "\r"; byte[] asciibytes = encoding.ascii.getbytes(arroba);
i've mande code don't know if gonna work.
when arduino sent me want read :
comport.readbyte(asciibytes);
could figure out how can read data coming arduino this?
first, should listen incoming data adding event handler comport:
comport.datareceived += received;
inside handler, can read received bytes. check bytes if specific sequence (@, r, \r) in received data:
private void received(object sender, serialdatareceivedeventargs e) { int bytes = comport.bytestoread; byte[] buffer = new byte[bytes]; comport.read(buffer, 0, bytes); if (buffer.except(encoding.ascii.getbytes("@r\r")).any() == true) { //your sequence received } }
since except
linq, don't forget add
using system.linq;
Comments
Post a Comment