This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Read/write file to LM5 connected USB
#1
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.
Reply
#2
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)
Reply
#3
Smile 
You have been very helpful, thank you.
Reply
#4
This is from adminWink
Reply
#5
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?

Attached Files Thumbnail(s)
       
Reply
#6
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.
Reply
#7
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.
Reply
#8
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?
Reply
#9
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
Reply
#10
Wink 
It worked! Thanks, I really was very helpful. Big Grin Wink
Reply


Forum Jump: