data over serial port

I am sending data to the serial port, when I send in the following format
Ex1:
write_to_serial_port2 (serdev2, “\2\5\b0”) the data sent is treated as numbers
but when I load the data in a variable and try to send it to the serial port it will be treated as a string
ex:2
local data = “\2\5\b0”
write_to_serial_port2 (serdev2, data)

in the second example, the data is not accepted by the device connected to the serial port because it is expecting the number of the values and not the string.
Can anyone advise how will I be able to send data as it is the case in my example 1 but be able to pass it to the write_to_serial_port2 function in a declared variable?

Hi,

I would expect both examples to behave the same way.
However those functions are not provided by AAF framework.

To answer more precisely the question, the application code is needed.
If you can’t give the whole application code, please at least give:

  • write_to_serial_port2 function code
  • the code calling write_to_serial_port2 function.

Regards,

Another remark: From the snippets above: Ex1 and Ex2 are strictly equivalent in terms of code. So there must be another reason why one is working and the other one not. As Laurent mentioned we’ll need the actual code to investigate further.
Thx.

that’s the write to serial function, please let me know what else you need me to provide?

function write_to_serial_port2 (sdevice2, sdata)
sdevice2:settimeout(4) – set a timeout in seconds
local write_cnt = assert (sdevice2:write (sdata))
print ('Number of characters written to serial port: ', write_cnt)
end

Hi,

Thank you for code of write_to_serial_port2.
This confirms what @cbugot said: both initial examples are identical.

Can you please provide more info:

  • is the result of print ('Number of characters written to serial port: ') always the same?
  • what do you expect to send with the string “\2\5\b0”?
    (I didn’t understand this:

)
What you are sending here is: characters with ascii code 2 and 5 (i.e. STX (Start of Text) and ENQ (Enquiry)), backslash (ascii code 8 ), and then the character ‘0’ (ascii code 48).

Other remark:

  • If the serial port timeout is always the same, you can set it one time (just after serial.open call).

Regards

logically the two examples should be the same however the output on the serial port isnt. the device connected to the serial port is expecting binary (\x\x\x) data but getting hex format data. simple test you can make to understand more what i mean, send the data string to the serial port in the two formats that i have provided in my example and you will notice that the number of characters written on the serial port is different for the two examples hense the difference in the output/outcome.

I tested, the result on serial port is the same if you use a variable or not.

Can you please give the Airlink Device and ALEOS version you are using?

If you want to send binary data, you might give a look at “pack” API, which is useful to handle binary data in string.
The doc is here:
source.sierrawireless.com/resour … /pack.html
Example:

require "pack"

local data = string.pack("bbb", 2, 5, 0)
write_to_serial_port2 (serdev2, data)

Regards,

I am using a GX400 4.4.2

OK I don’t have a device with old ALEOS firmware to test that right now.

Also,

  • Are you using IO XCard serial port or main serial port?
  • have you reserved the corresponding serial port usage for AAF application using the “Serial Port Reserved” field in ACEManager or corresponding device tree variable?

: as I can’t reproduce it: what is the difference? how many characters written in case 1 and in case 2?

if sending \xx this is considered one character, but if sending through a variable (variable = \xx) this will be 3 characters.

string.pack is not working, could it be related to the old firmware. I will upgrade my firmware tomorrow anyway

i am using the IO card and the reserve serial from AAF didnt work so I had to do it from Ace Manager.
does the reserve ser1 work?

Hi,

I’ve just tested on 4.4.4: reserving I/O Xcard Serial port using variable “system.aleos.reserve.ser1”, setting it to 1 ( as a number, like this devicetree.set(“system.aleos.reserve.ser1”, 1) ), works correctly.

Now that we are using the same 4.4.4 version, please tell us if string.pack fits your need to package and send binary data over the serial link.

Regards,

I will check the serial port and let you know.

not sure how string.pack can be used here, can you please provide a quick example to how i can form a string of lets say 5 bytes (binary) and push it to the serial port?

My previous example was to send 3 bytes.

Let’s do it again with 5 bytes:

--Don't forget to load pack API.
require "pack"

-- pack 5 bytes with values 1, 2 , 3, 4, 5
local data = string.pack("bbbbb", 1, 2, 3, 4, 5)

-- you could also do it like that:
--local data = string.pack("b5", 1, 2, 3, 4, 5)

write_to_serial_port2 (serdev2, data)

Please try to read pack doc here: source.sierrawireless.com/resour … /pack.html

the main idea is that the message that i am sending over the serial port will be a data string in a variable, can i then do something like the following?

function sendbyte(bytetosendoverserial)
local data = string.pack(“b”, bytetosendoverserial)
write_to_serial_port2 (serdev2, data)
end

function …
local datastring = “xxxxx”
for i=1, #datastring, 2 do
sendbyte(string.sub(datastring, i, i+1))
end

Well, I would recommend that you try and let us know :slight_smile:
What you could try is using string.byte instead of string.sub (as pack “b” is waiting for a number)