From 942deb2c9c76210011190bc0d38cb12059ed0363 Mon Sep 17 00:00:00 2001 From: Dennis Gunia Date: Sat, 13 Sep 2025 00:57:42 +0200 Subject: [PATCH] fix controller for ftdi485 --- software/pc_client/src/devicemgr.c | 20 ++++++++++--- software/pc_client/src/devicemgr.h | 4 ++- software/pc_client/src/{rs485.c => ftdi485.c} | 25 ++--------------- software/pc_client/src/{rs485.h => ftdi485.h} | 3 +- software/pc_client/src/main.c | 28 +++++++++++++++++-- software/pc_client/src/sfbus.c | 6 ++-- software/pc_client/src/sfbus.h | 2 +- 7 files changed, 51 insertions(+), 37 deletions(-) rename software/pc_client/src/{rs485.c => ftdi485.c} (74%) rename software/pc_client/src/{rs485.h => ftdi485.h} (82%) diff --git a/software/pc_client/src/devicemgr.c b/software/pc_client/src/devicemgr.c index 371c724..8188265 100644 --- a/software/pc_client/src/devicemgr.c +++ b/software/pc_client/src/devicemgr.c @@ -47,7 +47,7 @@ int deviceMap[SFDEVICE_MAX_X][SFDEVICE_MAX_Y]; struct SFDEVICE devices[SFDEVICE_MAXDEV]; -const char *symbols[] = {" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", +const char *symbols[45] = {" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Ä", "Ö", "Ü", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ".", "-", "?", "!"}; @@ -186,9 +186,18 @@ json_object *devicemgr_printDetailsAll() return root; } -void setSingle(int id, u_int8_t flap) +void setSingle(int id, char flap) { - sfbus_display_full(devices[id].rs485_descriptor, devices[id].address, flap); + // first convert char to flap id + char test_char = toupper(flap); + printf("find char %c\n", test_char); + for (int ix = 0; ix < 45; ix++){ + if (*symbols[ix] == test_char){ + printf("match char %i %i %i\n",test_char, *symbols[ix], ix); + sfbus_display_full(devices[id].rs485_descriptor, devices[id].address, ix); + break; + } + } devices[nextFreeSlot].current_flap = flap; } @@ -199,7 +208,10 @@ void printText(char *text, int x, int y) int this_id = deviceMap[x + i][y]; if (this_id >= 0) { - setSingle(devices[this_id].address, *(text + i)); + printf("print char %c to %i\n",*(text + i), devices[this_id].address); + + setSingle(this_id, *(text + i)); + usleep(5000); } } } diff --git a/software/pc_client/src/devicemgr.h b/software/pc_client/src/devicemgr.h index 28d5b81..6ef3311 100644 --- a/software/pc_client/src/devicemgr.h +++ b/software/pc_client/src/devicemgr.h @@ -11,9 +11,11 @@ #include #include #include +#include int devicemgr_readStatus(int device_id) ; json_object * devicemgr_printDetails(int device_id); json_object * devicemgr_printDetailsAll(); int devicemgr_register(int rs485_descriptor, u_int16_t address, int x,int y); -void devicemgr_init(); \ No newline at end of file +void devicemgr_init(); +int devicemgr_print(char* text); \ No newline at end of file diff --git a/software/pc_client/src/rs485.c b/software/pc_client/src/ftdi485.c similarity index 74% rename from software/pc_client/src/rs485.c rename to software/pc_client/src/ftdi485.c index a43e903..d70c9dd 100644 --- a/software/pc_client/src/rs485.c +++ b/software/pc_client/src/ftdi485.c @@ -1,4 +1,4 @@ -#include "rs485.h" +#include "ftdi485.h" /* * Open RS485 Interface (FT232RL) @@ -43,27 +43,6 @@ int rs485_init(char *device, int baud) { close(rs485_fd); return -1; } - rs485_trdir(rs485_fd, 1); + return rs485_fd; } - -/* -* Set RS485 Direction (FT232RL - RTS PIN) -*/ -int rs485_trdir(int rs485_fd, int level) { - int status; - - if (ioctl(rs485_fd, TIOCMGET, &status) == -1) { - perror("setRTS(): TIOCMGET"); - return 0; - } - if (level) - status |= TIOCM_DTR; - else - status &= ~TIOCM_DTR; - if (ioctl(rs485_fd, TIOCMSET, &status) == -1) { - perror("setRTS(): TIOCMSET"); - return 0; - } - return 1; -} \ No newline at end of file diff --git a/software/pc_client/src/rs485.h b/software/pc_client/src/ftdi485.h similarity index 82% rename from software/pc_client/src/rs485.h rename to software/pc_client/src/ftdi485.h index bdbfbec..8d388f1 100644 --- a/software/pc_client/src/rs485.h +++ b/software/pc_client/src/ftdi485.h @@ -11,5 +11,4 @@ #define RS485TX 0 #define RS485RX 1 -int rs485_init(char *device, int baud); -int rs485_trdir(int rs485_fd, int level); \ No newline at end of file +int rs485_init(char *device, int baud); \ No newline at end of file diff --git a/software/pc_client/src/main.c b/software/pc_client/src/main.c index 8530607..b376901 100644 --- a/software/pc_client/src/main.c +++ b/software/pc_client/src/main.c @@ -9,7 +9,7 @@ #include // Contains POSIX terminal control definitions #include // write(), read(), close() -#include "rs485.h" +#include "ftdi485.h" #include "sfbus.h" #include "devicemgr.h" @@ -67,14 +67,18 @@ int main(int argc, char *argv[]) { // test devicemgr_init(); - devicemgr_register(fd,0,0,0); - devicemgr_register(fd,1,1,0); + devicemgr_register(fd,1,0,0); + devicemgr_register(fd,2,1,0); + devicemgr_register(fd,3,2,0); + devicemgr_register(fd,4,3,0); devicemgr_printDetailsAll(); //exit(1); if (strcmp(command, "ping") == 0) { sfbus_ping(fd, addr_int); exit(0); + } else if (strcmp(command, "printf") == 0) { + printText(data,0,0); } else if (strcmp(command, "r_eeprom") == 0) { char *buffer = malloc(64); sfbus_read_eeprom(fd, addr_int, buffer); @@ -104,6 +108,24 @@ int main(int argc, char *argv[]) { exit(1); } + exit(0); + } else if (strcmp(command, "w_cal") == 0) { + int n_addr = strtol(data, NULL, 10); + // read current eeprom status + char *buffer_w = malloc(64); + char *buffer_r = malloc(64); + if (sfbus_read_eeprom(fd, addr_int, buffer_w) < 0) { + fprintf(stderr, "Error reading eeprom\n"); + exit(1); + } + // modify current addr + u_int16_t n_addr_16 = n_addr; + memcpy(buffer_w+2, &n_addr_16, 2); + if (sfbus_write_eeprom(fd, addr_int, buffer_w, buffer_r) < 0) { + fprintf(stderr, "Error writing eeprom\n"); + exit(1); + } + exit(0); } else if (strcmp(command, "status") == 0) { double voltage = 0; diff --git a/software/pc_client/src/sfbus.c b/software/pc_client/src/sfbus.c index 12df637..bfd3512 100644 --- a/software/pc_client/src/sfbus.c +++ b/software/pc_client/src/sfbus.c @@ -107,14 +107,14 @@ void sfbus_send_frame(int fd, u_int16_t address, u_int8_t length, } *frame = '$'; // startbyte - rs485_trdir(fd, 0); + //rs485_trdir(fd, 0); int result = write(fd, frame_ptr, frame_size_complete); print_bufferHexTx(frame_ptr + 5, frame_size_complete - 6, address); free(frame_ptr); // tcdrain(fd); - usleep(470 * (frame_size_complete + 1)); - rs485_trdir(fd, 1); + //usleep(470 * (frame_size_complete + 1)); + //rs485_trdir(fd, 1); } int sfbus_ping(int fd, u_int16_t address) { diff --git a/software/pc_client/src/sfbus.h b/software/pc_client/src/sfbus.h index 9f9624b..2d8a838 100644 --- a/software/pc_client/src/sfbus.h +++ b/software/pc_client/src/sfbus.h @@ -1,4 +1,4 @@ -#include "rs485.h" +#include "ftdi485.h" ssize_t sfbus_recv_frame(int fd, u_int16_t address, char *buffer); ssize_t sfbus_recv_frame_wait(int fd, u_int16_t address, char *buffer);