Logic Machine Forum
Read/write file to LM5 connected USB - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Read/write file to LM5 connected USB (/showthread.php?tid=678)



Read/write file to LM5 connected USB - Rodriguez - 16.03.2017

Hello, I am working with the logic machine 5 and I want to do a test of reading and writing files through a USB, but the example I find is focused on linux, does it work with windows? Or does it have a different syntax?
Another question, this code


- create usb mount directory
The.execute ('mkdir -p / mnt / usb')
 
- find first matching usb storage device
Dev = io.readproc ('ls / dev / sd * 1 2> / dev / null'): match ('/ dev / sd% l1')
 
- found it, mount
If dev then
The.execute ('mount' .. dev .. '/ mnt / usb')
Alert ('[usb-mount] mounted% s', dev)
- nothing found, local flash will be used
Else
Alert ('[usb-mount] no device found')
End


Hurt directly in the script or would relate to common functions?
Thank you.


RE: Read/write file to LM5 connected USB - buuuudzik - 16.03.2017

Here you have a script for mounting USB:
Code:
part = '/dev/sda1'

if io.exists(part) then
 os.execute('mount ' .. part .. ' /mnt')
 alert('mount ok')
else
 alert('usb disk not found')
end

And this is for writing and reading a file from USB:
Code:
path = '/mnt/'
status = io.exists(path)

io.writefile(path .. 'new.txt', 'This is my new file.')

result = io.readfile(path .. 'new.txt')
log(status, result)



RE: Read/write file to LM5 connected USB - Rodriguez - 16.03.2017

You have been very helpful, thank you.


RE: Read/write file to LM5 connected USB - buuuudzik - 16.03.2017

This is from adminWink


RE: Read/write file to LM5 connected USB - Shalltear - 29.03.2017

Hi, I was working with that code but it is not writing to me in memory, it is only printing the message in the registers (logs), but in memory it does not write anything to me, what could happen?


RE: Read/write file to LM5 connected USB - admin - 30.03.2017

What kind of task are you trying to achieve? If you want to store some data externally then FTP or remote database is a much better solution than a USB drive which is not designed for 24/7 operation.


RE: Read/write file to LM5 connected USB - Shalltear - 30.03.2017

Hi, what I want is to be able to write on an existing file from the logic machine, and also be able to read this, since I need to perform this task with USB at specific times.


RE: Read/write file to LM5 connected USB - Rodriguez - 10.04.2017

Hi , a while ago I worked this example, I want to take it back, but it tells me that the connection is created but at the moment of writing in memory it does not, what error could I be presently presenting?


RE: Read/write file to LM5 connected USB - admin - 11.04.2017

First, reboot your LM. Since you have tried different scripts your mount table is probably messed up. Second, make sure your flash drive is formatted as FAT or FAT32 (but not exFAT). Run this script once, then go to System Config > Status > System Status. In Partitions tab you should see that /dev/sda1 or similar is mounted under /mnt. Then you can use io.writefile('/mnt/myfile.txt', 'test') to write something onto USB drive.

Code:
devs = io.ls('/sys/class/block/')
table.sort(devs)
for _, dev in ipairs(devs) do
  if dev:match('^sd%a%d$') then
    part = dev
    break
  elseif not devn and dev:match('^sd%a$') then
    devn = dev
  end
end

part = part or devn
if part then
  os.execute('umount -f /mnt 2>&-')
  res, stat = io.readproc('mount /dev/' .. part .. ' /mnt 2>&1')
  if stat == 0 then
    alert('USB mount OK')
  else
    alert('USB mount failed: ' .. tostring(res))
  end
else
  alert('No valid USB devices found')
end



RE: Read/write file to LM5 connected USB - Rodriguez - 12.04.2017

It worked! Thanks, I really was very helpful. Big Grin Wink