# Trouble reading from the RV50 serial port

**URL:** <https://forum.sierrawireless.com/t/trouble-reading-from-the-rv50-serial-port/8747>\
**Category:** ALEOS Application Framework\
**Created:** [August 24, 2016, 2:09pm UTC](https://forum.sierrawireless.com/t/trouble-reading-from-the-rv50-serial-port/8747 "2016-08-24T14:09:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dylan](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@dylan](https://forum.sierrawireless.com/u/dylan)\
**Post date:** [August 24, 2016, 2:09pm UTC](https://forum.sierrawireless.com/t/trouble-reading-from-the-rv50-serial-port/8747/1 "2016-08-24T14:09:48Z")

</div>

Hello,

My problem could be related to this one : [url][https://forum.sierrawireless.com/t/possible-bug-in-rv50-serial-port-read/8645/1](https://forum.sierrawireless.com/t/possible-bug-in-rv50-serial-port-read/8645/1)], but the provided answers to this post didn’t help me to fix mine.

I run an application on an RV50 that’s supposed to read data from the serial port. But the data frames that I read are wrong and don’t match with the frames I send (To be precise, it works sometimes but like 80% of the time datas are wrong and I need a 100% reliability system).

Here are pieces of my code :

In the first place, I reserve the serial port :

```auto
--------------------------------------------------------------------------------
-- Check if the serial port has been reserved for AAF --------------------------
-- if not, reserve it ----------------------------------------------------------
--------------------------------------------------------------------------------
function reserve_serial_port (string)

	LOG = LOG .. "inside reserve_serial_port, string: " ..'\10'
	local serial_available = devicetree.get(string)
	LOG = LOG .. "inside reserve_serial_port, result to devicetree.get: " .. serial_available..'\10'
	print("inside reserve_serial_port, result to devicetree.get: ", serial_available)
	if serial_available ~= 1 then
		local result = devicetree.set(string, 1)
		LOG = LOG .. "Reserving Serial Port for AAF. Result: " .. result..'\10'
		print ("Reserving Serial Port for AAF. Result: ", result)
	end
	LOG = LOG .. "Serial Port Reserved" .. '\10'
	print("Serial Port Reserved")
end
```

Then I configure the serial port :

```auto
--------------------------------------------------------------------------------
-- open and configure UART ----------------------------------------
--------------------------------------------------------------------------------
function open_UART (string)

	print ("Inside open_UART, string: ", string)
	LOG = LOG .. "Inside open_UART, string: " .. string..'\10'
	
	local portname
	local i = 0

	if string == "main"
	then
		portname = "/dev/ttyS0"
	elseif string == "aux"
	then
		portname = "/dev/ttyS2"
	end

	print("Portname called within open_UART: ", portname)

-- Serial configuration : 19200 bauds, 8 datas bits, No parity, No flowcontrol, 2 stop bits
	local serial_config =
	{
		baudRate=19200,
		flowcontrol = 'none',
		numDataBits=8,
		parity='none',
		numStopBits=2
	}
	
	local serialdev = serial.open(portname, serial_config)
	return serialdev

end
```

And to finish, I read from the serial port :

```auto
function receivefromuart(uarthandle)

	print("Inside synchronize")
	
	local err
	local Pointeur_Write_Buffer
	
	while true do -- permanently read on the UART
	uarthandle:settimeout (1)
	
	local read1_buffer, err = uarthandle:read("*a") --lecture des caractères arrivant sur l'UART
	

	if read1_buffer ~= nil then 
			
			synch_buffer = synch_buffer .. read1_buffer
			print(" _________________received data is:__________________")		
			tohexandprint(synch_buffer) 
			print(" ______________________________________________________")
			Pointeur_Write_Buffer = string.len(synch_buffer)
			print ("Longeur Buffer :",Pointeur_Write_Buffer)
			
				if Pointeur_Write_Buffer <= 24 then
				LOG = LOG .. "Longeur Buffer :" .. Pointeur_Write_Buffer .. '\10'
				else
					checkintegrity(synch_buffer)
					synch_buffer = ""
				end
	end 
	end 
end
```

Let me know I you need more details about the code.

I run this code on many LS300 and I never had problems. I tried on 2 different RV50 for the same result, but every time what is read is not what is sent…

I tried to upgrade the RV50 firmware (4.5.0.017 -\> 4.5.2.004), but same issue for both.  
I also tried to change the flowcontrol setting and I have only one application on the RV50.

My settings in the Acemanager : [https://puu.sh/qMIdI/f9e1d830b5.png](https://puu.sh/qMIdI/f9e1d830b5.png)

---

<div class="post-metadata">

**Author:** ![cbugot](https://avatars.discourse-cdn.com/v4/letter/c/c37758/32.png) [@cbugot](https://forum.sierrawireless.com/u/cbugot)\
**Post date:** [August 31, 2016, 9:44am UTC](https://forum.sierrawireless.com/t/trouble-reading-from-the-rv50-serial-port/8747/2 "2016-08-31T09:44:27Z")

</div>

Hi Dylan,

Could you be more specific on how the read data are wrong? (i.e. corrupted bytes, or misaligned frames?)  
I saw no obvious prb with your code.

---

<div class="post-metadata">

**Author:** ![dylan](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@dylan](https://forum.sierrawireless.com/u/dylan)\
**Post date:** [August 31, 2016, 4:31pm UTC](https://forum.sierrawireless.com/t/trouble-reading-from-the-rv50-serial-port/8747/3 "2016-08-31T16:31:50Z")

</div>

Sometimes bytes are missing, sometimes I just have zeros where usually datas are…  
I guess it’s not helpful…

I could eventually screenshot some good/bad frames to let you see if you don’t have any ideas but I don’t think it will help.

---

<div class="post-metadata">

**Author:** ![cbugot](https://avatars.discourse-cdn.com/v4/letter/c/c37758/32.png) [@cbugot](https://forum.sierrawireless.com/u/cbugot)\
**Post date:** [September 1, 2016, 7:37am UTC](https://forum.sierrawireless.com/t/trouble-reading-from-the-rv50-serial-port/8747/4 "2016-09-01T07:37:45Z")

</div>

If bytes were corrupted I would think of a hardware issue (bad cable for instance).  
For misalignment issue, it could be caused by the timeout, the way you read your data. In general I do not recommend using “\*a” pattern + timeout. for normal reading on the serial port. It is usually better to read the number of bytes you actually need.  
A typical algorithm:

1. Phase one: read each byte one by one (serial:receive(1)), until a synchronization pattern is found
2. Phase two: read a frame with the exact byte count. Some time the frame is variable size, in that case I read the minimum number, parse it and infer the rest of the frame size (for instance if the frame size is in the packet header)

Another note: I have found that closing/reopening the serial port while reading does cause some weird data to be read, so make sure you do not close/open the serial port during the receive operation.
