简体   繁体   English

读取tcp,应用数学运算,格式化字符串和写入文件的python脚本

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

absolute newbie to python after years of C/C++. 经过多年的C / C ++,Python的绝对新手。 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. 我想编写一个Python脚本来告诉我的Rasberry Pi读取智能继电器板,计算温度并将格式化后的字符串写入文件。 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. 我不确定要访问接收到的数据并创建无符号的short值。 I will receive something like /x02/x55, which is (2*256)+85 = 597. 我会收到类似/ x02 / x55的信息,即(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 浮点数学,也不确定,但这就是我将597的读数转换为57.6的F度的方法

Finally, I need to write the string "57.6" to a file. 最后,我需要将字符串“ 57.6”写入文件。

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. 最后,不是在此代码中,我将需要一种方法让RasPi每分钟运行一次此代码以更新文件。 I have a web server that reads the file and creates HTML text from that. 我有一台Web服务器,该服务器读取文件并从中创建HTML文本。

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 ). 我假设您的数学大部分都是正确的,但是如果您使用的是Python 2,并且如果rdata[0]是整数(类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,然后再进行其他计算:

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. with关键字将作为上下文管理器打开文件,并自动为您关闭文件,即使您的代码引发异常也是如此。 Sockets don't work as context managers in Python 2, unless you create a wrapper for it yourself. 套接字在Python 2中不能用作上下文管理器,除非您自己为其创建包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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