PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

RPi to Win 10 file communication

Mon May 15, 2017 2:01 pm

I am trying to get a python sketch on an RPi (Sketch 1) to write a simple text file and have another python sketch (sketch 2) on a win 10 system read that file over wifi.

Sketch 1 on the RPi writes the text file with new data every 2 seconds and reads it back to "prove" it was written.
Sketch 2 on the win 10 system reads the file every 2 seconds, but for some reason the data isn't current. The initial read appears to be correct, but the data never changes as it should after that.

Of course if I run both sketches on the RPi, everything works fine.

I have been struggling with this for days and so far, no help found on the python forum or this RPi forum.

Here is sketch 1 on the RPI

Code: Select all

from __future__ import print_function
import time

filename = "sprinkler/status.json"
count = 0

while 1:
	f = open(filename, 'w')
	f.write(str(count))
	print("Write ", end = '')
	print(count)
	f.close()
	count += 1
	print("Read  ",end = '')
	f = open(filename, 'r')
	print(f.read())
	f.close
	time.sleep(1)
Here is the sketch on the win 10 system:

Code: Select all

from __future__ import print_function
import time

filename = "//raspi/pi/sprinkler/status.json"
count = 0

while 1:
	print("Read  ",end = '')
	f = open(filename, 'r')
	print(f.read())
	f.close
	time.sleep(1)
Here are the results I get with the sketches running on the 2 systems:
https://www.dropbox.com/s/o04t8z6vqmbze ... 2.JPG?dl=0


Here is the results with both sketches running on the RPI:
https://www.dropbox.com/s/co7jqmmnrq2knjv/test.JPG?dl=0

andrum99
Posts: 844
Joined: Fri Jul 20, 2012 2:41 pm

Re: RPi to Win 10 file communication

Mon May 15, 2017 3:37 pm

The file is stored on the Pi and is accessed from the Windows 10 machine via samba, from what I can see. It looks like the file is being cached on the Windows side, or perhaps samba is not being informed that the file has changed. Try closing, then re-opening the file on the Windows box between each read - that should let you see the new version of the file.

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Mon May 15, 2017 6:47 pm

I believe you are correct that the file is cached somewhere in Windows or Samba, but even though I explicitly close the files on both sides after reads and writes, the problem continues. I even went ahead and added an extra open/close after the sleep times to try to flush the cache on both ends.

I found python flush and fsync and added those to the writes, but again, no help.
I can't be the first person to write a file on the RPI and want to read it on windows over the network. If you know someone who has done that please direct them to this thread.

Any and all other suggestions are welcome.

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: RPi to Win 10 file communication

Mon May 15, 2017 8:54 pm

Please post your smb.conf, it could be that something is putting a lock on the file or that the file owner only exists on one of the machines.

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Tue May 16, 2017 12:36 am

The file access does not seem to be a problem because I can read the file easily over the network with explorer. In fact, after reading it with explorer, the win sketch picks up the correct data one time. It's a real bizarre behavior.

PS: I can also write to the file from explorer.

Here is the smb.conf file:
#
# Sample configuration file for the Samba suite for Debian GNU/Linux.
#
#
# This is the main Samba configuration file. You should read the
# smb.conf(5) manual page in order to understand the options listed
# here. Samba has a huge number of configurable options most of which
# are not shown in this example
#
# Some options that are often worth tuning have been included as
# commented-out examples in this file.
# - When such options are commented with ";", the proposed setting
# differs from the default Samba behaviour
# - When commented with "#", the proposed setting is the default
# behaviour of Samba but the option is considered important
# enough to be mentioned here
#
# NOTE: Whenever you modify this file you should run the command
# "testparm" to check that you have not made any basic syntactic
# errors.

#======================= Global Settings =======================

[global]

## Browsing/Identification ###

# Change this to the workgroup/NT-domain name your Samba server will part of
workgroup = WORKGROUP

# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable its WINS Server
# wins support = no

# WINS Server - Tells the NMBD components of Samba to be a WINS Client
# Note: Samba can be either a WINS Server, or a WINS Client, but NOT both
; wins server = w.x.y.z

# This will prevent nmbd to search for NetBIOS names through DNS.
dns proxy = no

#### Networking ####

# The specific set of interfaces / networks to bind to
# This can be either the interface name or an IP address/netmask;
# interface names are normally preferred
; interfaces = 127.0.0.0/8 eth0

# Only bind to the named interfaces and/or networks; you must use the
# 'interfaces' option above to use this.
# It is recommended that you enable this feature if your Samba machine is
# not protected by a firewall or is a firewall itself. However, this
# option cannot handle dynamic or non-broadcast interfaces correctly.
; bind interfaces only = yes



#### Debugging/Accounting ####

# This tells Samba to use a separate log file for each machine
# that connects
log file = /var/log/samba/log.%m

# Cap the size of the individual log files (in KiB).
max log size = 1000

# If you want Samba to only log through syslog then set the following
# parameter to 'yes'.
# syslog only = no

# We want Samba to log a minimum amount of information to syslog. Everything
# should go to /var/log/samba/log.{smbd,nmbd} instead. If you want to log
# through syslog you should set the following parameter to something higher.
syslog = 0

# Do something sensible when Samba crashes: mail the admin a backtrace
panic action = /usr/share/samba/panic-action %d


####### Authentication #######

# Server role. Defines in which mode Samba will operate. Possible
# values are "standalone server", "member server", "classic primary
# domain controller", "classic backup domain controller", "active
# directory domain controller".
#
# Most people will want "standalone sever" or "member server".
# Running as "active directory domain controller" will require first
# running "samba-tool domain provision" to wipe databases and create a
# new domain.
server role = standalone server

# If you are using encrypted passwords, Samba will need to know what
# password database type you are using.
passdb backend = tdbsam

obey pam restrictions = yes

# This boolean parameter controls whether Samba attempts to sync the Unix
# password with the SMB password when the encrypted SMB password in the
# passdb is changed.
unix password sync = yes

# For Unix password sync to work on a Debian GNU/Linux system, the following
# parameters must be set (thanks to Ian Kahan <<kahan@informatik.tu-muenchen.de> for
# sending the correct chat script for the passwd program in Debian Sarge).
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .

# This boolean controls whether PAM will be used for password changes
# when requested by an SMB client instead of the program listed in
# 'passwd program'. The default is 'no'.
pam password change = yes

# This option controls how unsuccessful authentication attempts are mapped
# to anonymous connections
map to guest = bad user

########## Domains ###########

#
# The following settings only takes effect if 'server role = primary
# classic domain controller', 'server role = backup domain controller'
# or 'domain logons' is set
#

# It specifies the location of the user's
# profile directory from the client point of view) The following
# required a [profiles] share to be setup on the samba server (see
# below)
; logon path = \\%N\profiles\%U
# Another common choice is storing the profile in the user's home directory
# (this is Samba's default)
# logon path = \\%N\%U\profile

# The following setting only takes effect if 'domain logons' is set
# It specifies the location of a user's home directory (from the client
# point of view)
; logon drive = H:
# logon home = \\%N\%U

# The following setting only takes effect if 'domain logons' is set
# It specifies the script to run during logon. The script must be stored
# in the [netlogon] share
# NOTE: Must be store in 'DOS' file format convention
; logon script = logon.cmd

# This allows Unix users to be created on the domain controller via the SAMR
# RPC pipe. The example command creates a user account with a disabled Unix
# password; please adapt to your needs
; add user script = /usr/sbin/adduser --quiet --disabled-password --gecos "" %u

# This allows machine accounts to be created on the domain controller via the
# SAMR RPC pipe.
# The following assumes a "machines" group exists on the system
; add machine script = /usr/sbin/useradd -g machines -c "%u machine account" -d /var/lib/samba -s /bin/false %u

# This allows Unix groups to be created on the domain controller via the SAMR
# RPC pipe.
; add group script = /usr/sbin/addgroup --force-badname %g

############ Misc ############

# Using the following line enables you to customise your configuration
# on a per machine basis. The %m gets replaced with the netbios name
# of the machine that is connecting
; include = /home/samba/etc/smb.conf.%m

# Some defaults for winbind (make sure you're not using the ranges
# for something else.)
; idmap uid = 10000-20000
; idmap gid = 10000-20000
; template shell = /bin/bash

# Setup usershare options to enable non-root users to share folders
# with the net usershare command.

# Maximum number of usershare. 0 (default) means that usershare is disabled.
; usershare max shares = 100

# Allow users who've been granted usershare privileges to create
# public shares, not just authenticated ones
usershare allow guests = yes

#======================= Share Definitions =======================

[homes]
comment = Home Directories
browseable = no

# By default, the home directories are exported read-only. Change the
# next parameter to 'no' if you want to be able to write to them.
read only = yes

# File creation mask is set to 0700 for security reasons. If you want to
# create files with group=rw permissions, set next parameter to 0775.
create mask = 0700

# Directory creation mask is set to 0700 for security reasons. If you want to
# create dirs. with group=rw permissions, set next parameter to 0775.
directory mask = 0700

# By default, \\server\username shares can be connected to by anyone
# with access to the samba server.
# The following parameter makes sure that only "username" can connect
# to \\server\username
# This might need tweaking when using external authentication schemes
valid users = %S

# Un-comment the following and create the netlogon directory for Domain Logons
# (you need to configure Samba to act as a domain controller too.)
;[netlogon]
; comment = Network Logon Service
; path = /home/samba/netlogon
; guest ok = yes
; read only = yes

# Un-comment the following and create the profiles directory to store
# users profiles (see the "logon path" option above)
# (you need to configure Samba to act as a domain controller too.)
# The path below should be writable by all users so that their
# profile directory may be created the first time they log on
;[profiles]
; comment = Users profiles
; path = /home/samba/profiles
; guest ok = no
; browseable = no
; create mask = 0600
; directory mask = 0700

[printers]
comment = All Printers
browseable = no
path = /var/spool/samba
printable = yes
guest ok = no
read only = yes
create mask = 0700

# Windows clients look for this share name as a source of downloadable
# printer drivers
[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
browseable = yes
read only = yes
guest ok = no
# Uncomment to allow remote administration of Windows print drivers.
# You may need to replace 'lpadmin' with the name of the group your
# admin users are members of.
# Please note that you also need to set appropriate Unix permissions
# to the drivers directory for these users to have write rights in it
; write list = root, @lpadmin

[global]
workgroup = WORKGROUP
netbios name = RASPI
realm = RASPI

[share1]
path = /home/pi

[pi]
path = /home/pi
valid users = pi
read only = no

[Raspi]
path = /
valid users = pi
read only = no

exit(0)

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: RPi to Win 10 file communication

Tue May 16, 2017 2:39 am

PickyBiker wrote:Any and all other suggestions are welcome.
https://technet.microsoft.com/en-us/lib ... 86200.aspx

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Tue May 16, 2017 3:20 am

stderr's post looked promising. The exact location of the cachelifetime entries in the registry were different from that listed in the document, but I changed all three from 60 seconds to 0. A reboot was done and those changes had no apparent effect on the problem.

andrum99
Posts: 844
Joined: Fri Jul 20, 2012 2:41 pm

Re: RPi to Win 10 file communication

Tue May 16, 2017 7:54 pm

PickyBiker wrote:I believe you are correct that the file is cached somewhere in Windows or Samba, but even though I explicitly close the files on both sides after reads and writes, the problem continues. I even went ahead and added an extra open/close after the sleep times to try to flush the cache on both ends.

I found python flush and fsync and added those to the writes, but again, no help.
I can't be the first person to write a file on the RPI and want to read it on windows over the network. If you know someone who has done that please direct them to this thread.

Any and all other suggestions are welcome.
If you're using Windows 10 Pro try the NFS client - it installs from Control Panel -> Programs and Features -> Add / Remove Windows Components. Then export the directory from the Pi using the NFS server.

You could also try posting on the Samba mailing list for help - see samba.org for a link.

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: RPi to Win 10 file communication

Tue May 16, 2017 9:17 pm

Your smb.conf (once you remove all the comments) is this:

[global]
workgroup = WORKGROUP
dns proxy = no
log file = /var/log/samba/log.%m
max log size = 1000
syslog = 0

panic action = /usr/share/samba/panic-action %d
server role = standalone server
passdb backend = tdbsam
obey pam restrictions = yes
unix password sync = yes
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
pam password change = yes
map to guest = bad user
usershare allow guests = yes

[homes]
comment = Home Directories
browseable = no
read only = yes
create mask = 0700
directory mask = 0700
valid users = %S

[printers]
comment = All Printers
browseable = no
path = /var/spool/samba
printable = yes
guest ok = no
read only = yes
create mask = 0700

[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
browseable = yes
read only = yes
guest ok = no


[global]
workgroup = WORKGROUP
netbios name = RASPI
realm = RASPI

[share1]
path = /home/pi

[pi]
path = /home/pi
valid users = pi
read only = no

[Raspi]
path = /
valid users = pi
read only = no

exit(0)

Several problems ;-)

there are a few default settings, but you can ignore these.
You have a couple of shares '[share1]' & '[pi]' these are the same share, remove one.
You also have this:

[global]
workgroup = WORKGROUP
netbios name = RASPI
realm = RASPI

You can only have one '[global]' , the workgroup is already set, the netbios name will be set automatically and you do not use 'realm' on a standalone server.

Oh and don't bother posting on the Samba mailing list, I will just tell you the same there ;-)

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Tue May 16, 2017 10:06 pm

That's great, thanks.

If I followed your instructions correctly I now have the following smb.conf file.
[global]
workgroup = WORKGROUP
netbios name = RASPI
realm = RASPI

dns proxy = no
log file = /var/log/samba/log.%m
max log size = 1000
syslog = 0

panic action = /usr/share/samba/panic-action %d
server role = standalone server
passdb backend = tdbsam
obey pam restrictions = yes
unix password sync = yes
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
pam password change = yes
map to guest = bad user
usershare allow guests = yes

[homes]
comment = Home Directories
browseable = no
read only = yes
create mask = 0700
directory mask = 0700
valid users = %S

[printers]
comment = All Printers
browseable = no
path = /var/spool/samba
printable = yes
guest ok = no
read only = yes
create mask = 0700

[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
browseable = yes
read only = yes
guest ok = no

[pi]
path = /home/pi
valid users = pi
read only = no

[Raspi]
path = /
valid users = pi
read only = no

exit(0)

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: RPi to Win 10 file communication

Wed May 17, 2017 9:45 am

One thing I failed to comment on, why have you got 'exit(0)' at the end of your smb.conf ?
This is a python thing and has nothing to do with smb.conf

andrum99
Posts: 844
Joined: Fri Jul 20, 2012 2:41 pm

Re: RPi to Win 10 file communication

Wed May 17, 2017 10:34 am

Another possibility is that there is a bug in Python's SMB layer on the Windows box. This seems likely, given that you said that accessing the file from Windows Explorer correctly shows the file being updated, if I understood you correctly.

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Wed May 17, 2017 11:31 am

When I was initially researching the smb.conf file I ran into something that said be sure to have exit(0) st the very end of the file. Sounds like that was bogus and I will remove it. Thanks again for the help with this. I'll reply with results when I get a chance to try this.

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Sun May 21, 2017 12:17 am

Unfortunately, nothing so far allows me to communicate via a file to the win 10 system from the RPi.

Does anyone have other suggestions on how to send just a few bytes from the RPi to the program on the win 10 system?

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: RPi to Win 10 file communication

Sun May 21, 2017 2:55 am

PickyBiker wrote:Unfortunately, nothing so far allows me to communicate via a file to the win 10 system from the RPi.

Does anyone have other suggestions on how to send just a few bytes from the RPi to the program on the win 10 system?
If you are just periodically sending small bits of data, would not it make more since to just use a TCP connection between the two computers, no need to use files at all, just a TCP stream. Just use socks like with any other TCP connection.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Sun May 21, 2017 2:56 am

I'll give that a try. Thanks.

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: RPi to Win 10 file communication

Sun May 21, 2017 7:42 am

Try adding 'max protocol = NT1' to smb.conf

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Sun May 21, 2017 12:56 pm

Tried max protocol = NT1 in the [global] block. That did not help. I did add the file modified date to the win sketch. It sees the file modified date update but the data stubbornly refuses to update. It has to be stuck in a cache somewhere.

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Sun May 21, 2017 2:12 pm

Has anyone else tried these two scripts? Same result - different result?

JumpZero
Posts: 1032
Joined: Thu Mar 28, 2013 7:35 pm
Location: 127.0.0.1

Re: RPi to Win 10 file communication

Sun May 21, 2017 6:41 pm

Hi
You said windows explorer reads the file but the Python script fails.
To help to locate the problem you can try to read the file on windows side with something else than Python. A simple batch file can do it, or any other language you know.
--
Jmp0

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Sun May 21, 2017 9:07 pm

It does not work with a .cmd batch file nor in a VB.net program.

File Explorer must do something unique when it opens a file over the network.

hortimech
Posts: 323
Joined: Wed Apr 08, 2015 5:52 pm

Re: RPi to Win 10 file communication

Mon May 22, 2017 12:25 pm

OK, try turning off oplocks, add these lines to smb.conf, then restart smbd & nmbd:

oplocks = no
kernel oplocks = no

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Mon May 22, 2017 6:17 pm

BINGO! Works like a charm.

I really didn't think this one was going to be solved, but you did it.
Finally after more than 2 weeks, I can move forward of this sticky spot.

Thank you very much for figuring this out.
You are one steely-eyed samba man!

Very best regards.
https://www.dropbox.com/s/u28e7x57794kz55/win.jpg?dl=0

andrum99
Posts: 844
Joined: Fri Jul 20, 2012 2:41 pm

Re: RPi to Win 10 file communication

Mon May 22, 2017 9:33 pm

Was it oplocks then?

PickyBiker
Posts: 80
Joined: Tue Nov 26, 2013 6:26 pm
Location: Texas

Re: RPi to Win 10 file communication

Mon May 22, 2017 10:43 pm

I commented out kernel oplocks and it still works, so yes, it was oplocks, not kernal oplocks. Thanks again.
oplocks = no
; kernal oplocks = no

Return to “General discussion”