简体   繁体   中英

python script to read tcp, apply math, format string, and write file

absolute newbie to python after years of C/C++. I want to write a Python script to tell my Rasberry Pi to read a smart relay board, calculate a temperature, and write a formatted string to a file. After much googling and newsgroup searching, I may have most of it:

import socket

// open TCP client socket:

IPADDR = '192.168.99.9'


PORTNUM = 2101

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((IPADDR, PORTNUM))



// send command to read A/D value: two hex bytes

tdata = bytes.fromhex('FEA4')

s.send(tdata)



// receive 2-byte (hex) reply

rdata=s.recv(256)



// close socket

s.close()



// convert reply to a voltage reference (unsigned short)

vRef = (rdata[0] * 256)+(rdata[1])



// convert vref to float as degrees Farenheit

degF = vRef * 4930 / 1024

degF = degF / 10

degF = degF - 273.15

degF = degF * 9 / 5 + 32



// open text file

fo = open("\mnt\stuff\temp.txt", "w")



// write formatted string as number only e.g., 32.6

fo.write("{:+.1f}.format(degF)\n")



// Close file

fo.close()

I'm not sure about accessing the received data and creating the unsigned short value. I will receive something like /x02/x55, which is (2*256)+85 = 597.

The floating-point math, not sure here either, but that's how I convert a reading of 597 to a degrees-F value of 57.6

Finally, I need to write the string "57.6" to a file.

Lastly, and not in this code, I will need a way to have the RasPi run this code once per minute to update the file. I have a web server that reads the file and creates HTML text from that.

thanks ANYONE for any help...

I'll assume your socket code is right, and I'll fix the comments:

import socket

# open TCP client socket:
IPADDR = '192.168.99.9'
PORTNUM = 2101
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IPADDR, PORTNUM))

# send command to read A/D value: two hex bytes
tdata = bytes.fromhex('FEA4')
s.send(tdata)
# receive 2-byte (hex) reply
rdata=s.recv(256)
# close socket
s.close()

I'll assume your math is mostly correct, but if you're using Python 2, you might be doing floor division if rdata[0] is an integer (class int ).

# convert reply to a voltage reference (unsigned short)
vRef = (rdata[0] * 256)+(rdata[1])

To be safe, first convert vRef to float before the other calculations:

vRef = float(rdata[0] * 256)+(rdata[1])

And then procede:

# convert vref to float as degrees Farenheit
degF = vRef * 4930 / 1024
degF = degF / 10 
degF = degF - 273.15
degF = degF * 9 / 5 + 32

This:

# open text file
fo = open("\mnt\stuff\temp.txt", "w")
# write formatted string as number only e.g., 32.6
fo.write("{:+.1f}.format(degF)\n")
# Close file
fo.close()

Can be replaced with this:

with open("\mnt\stuff\temp.txt", "w") as file:
    file.write("{:+.1f}.format(degF)\n")

The with keyword opens the file as a context manager, and automatically closes the file for you, even if your code raises an exception. Sockets don't work as context managers in Python 2, unless you create a wrapper for it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM