目录
1、修改GUI
2、修改client
3、 修改server
4、 修改driver_handele
5、 测试和提交
1、修改GUI
之前叫IIC,我们其实是借助EEPROM来测试IIC是不是好用所以名称改一改
长得有点奇怪
这样虽然一样长了但是还是很奇怪。
就先这样吧
layout_l = [[tool.name('NetWork'), sg.Button('Connect', key='Connect')],[tool.name('NetWork'), sg.Button('Disconnect', key='Disconnect')],[tool.name('NetWork'), sg.Output(size=(32, 1), key='IP')],[sg.Checkbox('Input', use_custom_titlebar, enable_events=True, key='input')],[sg.InputText(disabled=True, key='txbuff'), sg.Button('SEND', key='send')],[tool.name('Text'), sg.Button('SPI', key='SPI')],[sg.InputText(key='AT24C02_I', default_text=''), sg.Button('AT24C02 WRITE', key='AT24C02_W')],]# 右部layoutlayout_r = [[sg.Output(size=(16, 1), key='LED_O'), sg.Button('LED', key='LED')],[sg.Output(size=(16, 1), key='SR501_O'), sg.Button('SR501', key='SR501')],[sg.Output(size=(16, 1), key='SR04_O'), sg.Button('SR04', key='SR04')],[sg.Output(size=(16, 1), key='IRDA_O'), sg.Button('IRDA', key='IRDA')],[sg.Output(size=(16, 1), key='DHT11_O'), sg.Button('DHT11', key='dht11')],[sg.Output(size=(16, 1), key='DS18B20_O'), sg.Button('DS18B20', key='ds18b20')],[sg.Output(size=(16, 1), key='AT24C02_O'), sg.Button('AT24C02 READ', key='AT24C02_R')],#[tool.name('Text'), sg.Button('IIC', key='IIC')],]
2、修改client
'''
fuction : 客户端程序
author : 辛天宇
date : 2024-4-13
-------------------------------
author date modify
辛天宇 2024-4-15 结合GUI和网络通信'''
import show
import tcp
import tool
import socket
import global_vardef send_handle(window, client_socket, values):global_var.TX_BUF = values['txbuff'] print(f"txbuff={global_var.TX_BUF}")# 清理inputwindow['txbuff'].update(value='')data = global_var.TX_BUFclient_socket.sendall(data.encode())# 接收服务器的响应data = client_socket.recv(512)# 将字节字符串转化为字符串global_var.RX_BUF = data.decode('utf-8')# print(f"rx......{global_var.RX_BUF}") def quit_handel(client_socket):cmd='Q'client_socket.sendall(cmd.encode())tcp.disconnect_to_server(client_socket)def motor_handel(window, client_socket, values):i = int(values['MOTOR_I'])global_var.MOTOR_DATA = str(abs(i)%360)if i >= 0:global_var.MOTOR_DIRECTION='0'else:global_var.MOTOR_DIRECTION='1'message = 's'+global_var.MOTOR_DIRECTION+global_var.MOTOR_DATA# 清理inputwindow['MOTOR_I'].update(value='0')set_tx_buf('motor', message)send_cmd(client_socket)# 接收服务器的响应data = client_socket.recv(512)# 将字节字符串转化为字符串global_var.RX_BUF = data.decode('utf-8')# print(f"rx......{global_var.RX_BUF}") # 进行一次发送
def send_cmd(client_socket):data = global_var.TX_BUFclient_socket.sendall(data.encode())# 接收服务器的响应data = client_socket.recv(512)# 将字节字符串转化为字符串global_var.RX_BUF = data.decode('utf-8')# print(f"rx......{global_var.RX_BUF}") # 设置发送消息
def set_tx_buf(device, message): if device == 'sr04':global_var.TX_BUF = '@002'+messageif device == 'led':global_var.TX_BUF = '@000'+messageelif device == 'sr501':global_var.TX_BUF = '@001'+messageelif device == 'irda':global_var.TX_BUF = '@003'+messageelif device == 'motor':global_var.TX_BUF = '@004'+messageelif device == 'dht11':global_var.TX_BUF = '@005'+messageelif device == 'ds18b20':global_var.TX_BUF = '@006'+messageelif device == 'AT24C02':global_var.TX_BUF = '@007'+messageelif device == 'spi':global_var.TX_BUF = '@008'# 解析IRDA传入的数据
def irda_data_analysis(cmd):if cmd[5] == 'r':global_var.IRDA_DATA = "RED"elif cmd[5] == 'm':global_var.IRDA_DATA = "MENU"elif cmd[5] == 't':global_var.IRDA_DATA = "TEST"elif cmd[5] == '+':global_var.IRDA_DATA = "+"elif cmd[5] == 'b':global_var.IRDA_DATA = "BACK"elif cmd[5] == 'l':global_var.IRDA_DATA = "LEFT"elif cmd[5] == 'p':global_var.IRDA_DATA = "START"elif cmd[5] == 'r':global_var.IRDA_DATA = "RIGHT"elif cmd[5] == '0':global_var.IRDA_DATA = "0"elif cmd[5] == '1':global_var.IRDA_DATA = "1"elif cmd[5] == '2':global_var.IRDA_DATA = "2"elif cmd[5] == '-':global_var.IRDA_DATA = "-"elif cmd[5] == 'c':global_var.IRDA_DATA = "c"elif cmd[5] == '3':global_var.IRDA_DATA = "3"elif cmd[5] == '4':global_var.IRDA_DATA = "4"elif cmd[5] == '5':global_var.IRDA_DATA = "5"elif cmd[5] == '6':global_var.IRDA_DATA = "6"elif cmd[5] == '7':global_var.IRDA_DATA = "7"elif cmd[5] == '8':global_var.IRDA_DATA = "8"elif cmd[5] == '9':global_var.IRDA_DATA = "9"# 处理数据
def cmd_handle(window):cmd = global_var.RX_BUFglobal_var.RX_BUF="#000"if len(cmd) < 4:print("cmd ERROR")return -1if '@' == cmd[0]:# 目前驱动设备数量只有两位数if cmd[1] == '0':# LED: @000+1位命令位+1位数据位if cmd[2] == '0' and cmd[3] == '0':if cmd[5] == '1':print("LED Status change success")elif cmd[5] == '0':print("LED Status change failure")else:print("message ERROR")# SR501:@001+1位数据位elif cmd[2] == '0' and cmd[3] == '1':if cmd[5] == '1':print("有人")message='有人'window['SR501_O'].update(message)elif cmd[5] == '0':print("无人")message='无人'window['SR501_O'].update(message)else:print("message ERROR")# SR04elif cmd[2] == '0' and cmd[3] == '2':if cmd[4] == 'g':global_var.SR04_DATA = cmd[5:8]message = f"{global_var.SR04_DATA}cm"window['SR04_O'].update(message)else:print("SR04: message ERROR") # irdaelif cmd[2] == '0' and cmd[3] == '3':# message = cmd[5]# print(message)if cmd[4] == 'g':irda_data_analysis(cmd)window['IRDA_O'].update(global_var.IRDA_DATA)# motorelif cmd[2] == '0' and cmd[3] == '4':if cmd[4] == 's':print("MOTOR: message SCUESS") else:print("MOTOR: message ERROR") # dht11elif cmd[2] == '0' and cmd[3] == '5':if cmd[4] == 'g':try:global_var.TEM=cmd[5]+cmd[6]global_var.HUM=cmd[7]+cmd[8]message = f"{global_var.TEM}°C {global_var.HUM}%"window['DHT11_O'].update(message)except:message = "ERROR"window['DHT11_O'].update(message)print("DHT11: message ERROR")else:message = "ERROR"window['DHT11_O'].update(message)print("DHT11: message ERROR") # ds18b20elif cmd[2] == '0' and cmd[3] == '6':if cmd[4] == 'g':try:global_var.TEM=cmd[5]+cmd[6]message = f"{global_var.TEM}°C"window['DS18B20_O'].update(message)except:message = "ERROR"window['DS18B20_O'].update(message)print("DS18B20: message ERROR")else:message = "ERROR"window['DS18B20_O'].update(message)print("DS18B20: message ERROR")# AT24C02elif cmd[2] == '0' and cmd[3] == '7':if cmd[4] == 'g':try:print(cmd[5:])except:message = "ERROR"window['AT24C02_O'].update(message)print("AT24C02: message ERROR")elif cmd[4] == 'p':passelse:message = "ERROR"window['AT24C02_O'].update(message)print("AT24C02: message ERROR")#spielif cmd[2] == '0' and cmd[3] == '8':print(cmd[4:])# 处理事件
def event_handle(window, client_socket):led = 0# 事件循环while True: try:cmd_handle(window)event, values = window.read()if event == 'input':window['txbuff'].update(disabled=not values['input'])elif event == 'send':send_handle(window, client_socket, values)elif event == 'Clean':window['Output'].update(value='')elif event == 'dht11':window['DHT11_O'].update(value=' ')set_tx_buf('dht11', 'g')send_cmd(client_socket)elif event == 'ds18b20':window['DS18B20_O'].update(value=' ')set_tx_buf('ds18b20', 'g')send_cmd(client_socket)elif event == 'Quit': quit_handel(client_socket) print(f"See you.............")breakelif event is None:print(f"xxxxxxxxxxxxxxxxxxxx")breakelif event == 'LED':if led % 2 == 0:set_tx_buf('led','p1')else:set_tx_buf('led','p0')led+=1if led > 100:led = 0send_cmd(client_socket)elif event == 'SR501':set_tx_buf('sr501','g')send_cmd(client_socket)elif event == 'SR04':set_tx_buf('sr04','g')send_cmd(client_socket)elif event == 'IRDA':set_tx_buf('irda','g')send_cmd(client_socket)elif event == 'MOTOR':motor_handel(window, client_socket, values)elif event == 'AT24C02_R':set_tx_buf('AT24C02','g')send_cmd(client_socket)elif event == 'AT24C02_W':set_tx_buf('AT24C02','p'+values['txbuff'])window['txbuff'].update(value='')send_cmd(client_socket)# 处理其他事件...except Exception as e:window.close()print(f"An error occurred: {e}")return 0window.close()return 0 def main():# 创建GUI对象window = show.show_window('DefaultNoMoreNagging')# 尝试连接到服务器 client_socket = tcp.connect_to_server()if client_socket is not None: event_handle(window, client_socket)if __name__ == '__main__':main()
3、 修改server
case 7:printf("AT24C02!!!\n");if ('g' == cmd[4]){direction = direction_get;}else{direction = direction_put;}ret = at24c02_handle(direction, buf);if (NOERROR == ret){tx_buffer = buf;}else{tx_buffer = "@007e";}printf(">>>>>>%s\n",tx_buffer);if (send(acceptfd, tx_buffer, strlen(tx_buffer), 0) < 0){perror("send failed"); }break;
4、 修改driver_handele
/*
*author : xintianyu
*function : Handle at24c02 Settings
*date : 2024-4-22
-----------------------
author date modify*/
int at24c02_handle(DIRECTION direction, char *data)
{/*传入参数后面要做通用处理使用空指针*//*路径宏定义*/char *device = "/dev/cebss_at24c02";int ret = NOERROR;static int fd;char buf[100];/* 打开文件 */fd = open(device, O_RDWR);if (fd == -1){printf("can not open file %s\n", device);return ERROR;}if (direction_put == direction){ret = write(fd, data, strlen(data) + 1);if (ret > 0){sprintf(data,"@007p");ret = NOERROR;}else{sprintf(data,"@007e");printf("at24c02: PUT ERROR!!!\n");ret = ERROR;}}else if (direction_get == direction){ret = read(fd, buf, 100);if (ret > 0){sprintf(data,"@007g%s", buf);printf("data: %s\n", data);ret = NOERROR;}else{sprintf(data,"@007e");printf("at24c02: GET ERROR!!!\n");ret = ERROR;}}else{sprintf(data,"@007e");printf("at24c02: CMD ERROR!!!\n");}return ret;
}
5、 测试和提交
非易失性名不虚传。我上次写的world这次还在
写入也很完美。
git status
git add server/
git add driver/
git add client/client.py
git add client/global_var.py
git add client/show.py
git commit -m "【1】【duplicate】034——从GUI->Client->Server->driver实现读写EEPROM"
git push origin xintianyu