why the maximum length of serial.available() is 32

Moderators: lily.li, violet, salmanfarisvp

Post Reply
louis.liu
Pre-kindergarten
Pre-kindergarten
Posts: 1
Joined: Fri Dec 19, 2014 3:27 pm

why the maximum length of serial.available() is 32

Post by louis.liu » Fri Dec 19, 2014 3:35 pm

I test followiing sample code, when input string is more than 32, serial will not work.
check serial.available() , its maximum length is 32, why? please help me, thanks.


String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete

void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}

void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}

/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: why the maximum length of serial.available() is 32

Post by loovee » Sat Dec 20, 2014 8:45 pm

Hi, yes, this is a bug, I'll tell you the way to fix it here, and we'll fix the SDK in the next version.

Open this file:

hardware\arduino\mtk\cores\arduino\UARTClass.cpp

In line 42 and line 47, there's a num 32, change it to ERIAL_BUFFER_SIZE:

Code: Select all

void UartIrqHandler(void* parameter, VM_DCL_EVENT event, VM_DCL_HANDLE device_handle)
{
    if(event == VM_UART_READY_TO_READ)
    {
        char data[SERIAL_BUFFER_SIZE];
        int i;
        VM_DCL_STATUS status;
        VM_DCL_BUFF_LEN returned_len;

        status = vm_dcl_read(device_handle,(VM_DCL_BUFF*)data,SERIAL_BUFFER_SIZE,&returned_len,vm_dcl_get_ownerid());
        if(status<VM_DCL_STATUS_OK)
        {
            vm_log_info((char*)"read failed");
        }  
        if(device_handle == g_APinDescription[0].ulHandle)
        {
            for(i=0;i<returned_len;i++)
            {
                Serial1._rx_buffer->store_char(data[i]);
            }
        }
        else
        {
            for(i=0;i<returned_len;i++)
            {
                Serial._rx_buffer->store_char(data[i]);
            }
        }
    }
    else
    {
    }
}
Then the serial buff will change to SERIAL_BUFFER_SIZE, it's 64 now, you can modify it here:
hardware/arduino/mtk/cores/arduino/RingBuffer.h

Please have a try, I test it ok here, thanks.

Post Reply