Reading serial port faster
<p>I have a computer software that sends RGB color codes to Arduino using USB. It works fine when they are sent slowly but when tens of them are sent every second it freaks out. What I think happens is that the Arduino serial buffer fills out so quickly that the processor can't handle it the way I'm reading it.</p><pre><br /><code>#define INPUT_SIZE 11<br /><br />void loop() {<br /> if(Serial.available()) {<br /> char input[INPUT_SIZE + 1];<br /> byte size = Serial.readBytes(input, INPUT_SIZE);<br /> input[size] = 0;<br /><br /> int channelNumber = 0;<br /><br /> char* channel = strtok(input, " ");<br /> while(channel != 0) {<br /> color[channelNumber] = atoi(channel);<br /><br /> channel = strtok(0, " ");<br /> channelNumber++;<br /> }<br /><br /> setColor(color);<br /> }<br />}<br /></code><br /></pre><p>For example the computer might send <code>255 0 123</code> where the numbers are separated by space. This works fine when the sending interval is slow enough or the buffer is always filled with only one color code, for example <code>255 255 255</code> which is 11 bytes (<code>INPUT_SIZE</code>). However if a color code is not 11 bytes long and a second code is sent immediately, the code still reads 11 bytes from the serial buffer and starts combining the colors and messes them up. How do I avoid this but keep it as efficient as possible?</p><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34469728/reading-serial-port-faster">http://ift.tt/1OPLX6t</a></h2>
Nhận xét
Đăng nhận xét