B4R Question serial stream buffer max length

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I use this code to receive commands from PC to perform tasks i.e set date and time @115200. The problem is when trying to receive 106 bytes of data, i get buffer length error. on the other hand the code runs very well with smaller packets (12 bytes).

I tried to reduce baud rate to 9600 and also reduce bytes received to 75, but i still get errors. Is there a max limit for send and receive serial data? and how to handle this?

the problem in this code:
B4X:
Case REG_USER_DATA
            If ReadWrite = RW_WRITE Then
                Dim AccountArray (101) As Byte
                Dim RAF As RandomAccessFile
                RAF.Initialize(AccountArray,False)
                RAF.WriteBytes(Buffer,4,101,0)
                Log(AccountArray) 'for test only
              
            Else
               
            End If  

    End Select

here is the whole sub:

B4X:
private Sub ReadPCstream(Buffer() As Byte)
    'PC message frame should look like this(sent from PC)
    '0x02 start byte
    'RW read write byte, read = 1 , write - 0
    'REG Register ID byte
    'LN frame length byte, whole message length
    'MSG message bytes
    '0x04 end of transmission byte
    'a hello world message, 0x02 0x00 0x00 0x10 Hello World 0x04, write hello world to register 0x00
   
    Private const REG_USER_DATA As Byte = 0x00
    Private const REG_DATE_TIME As Byte = 0x01
    Private Const REG_LCD_BACKLIGHT_LEVEL As Byte  = 0x02
    Private Const REG_LCD_DARKNESS_LEVEL As Byte  = 0x03
    Private const REG_BATTERY_VOLTAGE As Byte = 0x04
    Private CONST REG_SHIFT As Byte = 0x05
    Private CONST REG_REASON As Byte = 0x06
    Private const REG_LOAD As Byte = 0x07
   
   
    Private const RW_READ As Byte = 1
    Private const RW_WRITE As Byte = 0
   
    If readRFIDbusy = True Or PCbusy = True Then
        Return
    End If
   
    IndicateAll(False,False,False)
    PCbusy = True
    IndicateBusy(True)
    StartDisplayTimeout
   
    Dim BufferLength As Byte = Buffer.Length
   
    Dim RegisterAddress As Byte
   
    If BufferLength < 5 Then
        PCbusy = False
        IndicateBusy (False)
        Return
    End If
   
    Dim BufferHeader As Byte = Buffer(0)
    Dim ReadWrite As Byte = Buffer(1) 'read = 1 write = 0
    If ReadWrite > 1 Then ReadWrite = 1
    Dim RegisterID As Byte = Buffer(2) 'register id, i.e. register 0x01 = date time register
    Dim FrameLength As Byte = Buffer(3) 'length of frame
    Dim BufferEndofTransmission As Byte = Buffer(BufferLength - 1)
   
    If FrameLength <> BufferLength Then
        SendPCMessage("E01")
        Log(FrameLength,"-",BufferLength)
       
        BeepLong
        PCbusy = False
        IndicateBusy (False)
        Return
        Else
           
    End If
   
    If BufferHeader <> 0x02 Then 'start of transmission charachter
        SendPCMessage("E02")
        BeepLong
        PCbusy = False
        IndicateBusy (False)
        Return
    Else
   
    End If
   
    If BufferEndofTransmission <> 0x04 Then 'end of transmission character
        SendPCMessage("E04")
        BeepLong
        PCbusy = False
        IndicateBusy (False)
        Return
    Else
   
    End If   


'************************************************************************************************************
'************************************************************************************************************
'************************************************************************************************************
'write date time register looks like this
'0x02 0x00 0x01 0x0C 16 10 27 02 14 40 22 0x04 'write 2016/10/27 Sunday 14:40:22 to date time register
'this packet sent from proton basic serial terminal #2#0#1#12#16#10#27#01#14#52#15#4

    Select RegisterID
        Case REG_DATE_TIME
            If ReadWrite = RW_WRITE Then
                SetRTCTime(Buffer(4) + 2000,Buffer(5),Buffer(6),Buffer(7),Buffer(8),Buffer(9),Buffer(10))
            Else
                Log(0x02,RW_WRITE,REG_DATE_TIME,0x0C,Years - 2000,Months,DayOfMonth,DayOfWeek,Hours,Minutes,Seconds,0x04)
            End If
           
           
        Case REG_LCD_BACKLIGHT_LEVEL
            If ReadWrite = RW_WRITE Then
                BackLightLevel = Buffer(4)
                SetBacklightLevel(BackLightLevel)
            Else
                Log(0x02,RW_WRITE,REG_LCD_BACKLIGHT_LEVEL,BackLightLevel ,0x06,0x04)
            End If
           
        Case REG_LCD_DARKNESS_LEVEL
            If ReadWrite = RW_WRITE Then
                ContrastLevel = Buffer(4)
                SetContrast(ContrastLevel)
            Else
                Log(0x02,RW_WRITE,REG_LCD_DARKNESS_LEVEL,ContrastLevel,0x06,0x04)
            End If   
           
        Case REG_BATTERY_VOLTAGE
            If ReadWrite = RW_READ Then
                GetBatteryInfo
                Log(0x02,RW_WRITE,REG_BATTERY_VOLTAGE,BatVolts,0x06,0x04)
            End If
       
        Case REG_USER_DATA
            If ReadWrite = RW_WRITE Then
                Dim AccountArray (101) As Byte
                Dim RAF As RandomAccessFile
                RAF.Initialize(AccountArray,False)
                RAF.WriteBytes(Buffer,4,101,0)
                Log(AccountArray) 'for test only
               
            Else
                
            End If   

       
               
    End Select
   
   
BeepShort
PCbusy = False
IndicateBusy (False)   
   
End Sub
 

Mostez

Well-Known Member
Licensed User
Longtime User
it is not real B4R erroe message, it is frame length error, attached three images, when I tried to send 100 bytes, i get only 63, even though they are not ascii charachters as sent, just emtry array, and when tried to send 5 or 25 bytes, received them OK. would you please explain what is B4RSerializator and how to use it.
 

Attachments

  • frame_ok.jpg
    frame_ok.jpg
    45.9 KB · Views: 218
  • frame_ok2.jpg
    frame_ok2.jpg
    48.9 KB · Views: 212
  • frame_err.jpg
    frame_err.jpg
    54.9 KB · Views: 219
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
I think I found the related parameter in AsyncStreams.cpp, I changed it to 128 instead of 100
B4X:
#include "B4RDefines.h"
namespace B4R {
    #define MAX_SIZE_NO_PREFIX 128
 
Upvote 0
Top