From 0a504ba742b5ef3b7eb42084ccd0f3b940be610a Mon Sep 17 00:00:00 2001 From: "U-INFINEON\\jahnst" Date: Sat, 11 Feb 2023 23:14:49 +0100 Subject: [PATCH] Serial interface in Windows working --- Makefile.config | 2 +- convert.cc | 2 +- detect.h | 4 ++ error.cc | 2 +- posctl.cc | 127 ++++++++++++++++++++++++++++++++++++------------ video.cc | 4 +- 6 files changed, 104 insertions(+), 37 deletions(-) diff --git a/Makefile.config b/Makefile.config index 0100b89..6cfa34e 100644 --- a/Makefile.config +++ b/Makefile.config @@ -5,7 +5,7 @@ # USE_SVBONY = 1 USE_DNG = 1 -USE_SER = 0 +USE_SER = 1 DEBUG_ANGLES = 1 DEBUG_POSCTL = 1 diff --git a/convert.cc b/convert.cc index affed15..264d166 100644 --- a/convert.cc +++ b/convert.cc @@ -186,7 +186,7 @@ inline unsigned char clamp (double x) { inline void convert2rgb (unsigned char Y1, unsigned char Cb, unsigned char Cr, unsigned char *ER, unsigned char *EB, unsigned char *EG) { - register int y1, pb, pr; + int y1, pb, pr; y1 = Y1 - 16; pb = Cb - 128; diff --git a/detect.h b/detect.h index 0b151a2..ac4c43b 100644 --- a/detect.h +++ b/detect.h @@ -55,7 +55,11 @@ class PosCtl { PID pid_axis[2]; std::string device; int device_type; +#ifdef BUILD_WINDOWS + HANDLE device_fd; +#else int device_fd; +#endif struct timeval calib_timestamp; int calib_mode; position_2d calib_pos; diff --git a/error.cc b/error.cc index 90a7e97..c4b94b2 100644 --- a/error.cc +++ b/error.cc @@ -80,13 +80,13 @@ void errorexit (char *fmt,...) { printf ("***************************\n"); printf ("%s", text); +#ifndef BUILD_WINDOWS // // backtrace int j, nptrs; void *buffer[SIZE]; char **s; -#ifndef BUILD_WINDOWS nptrs = backtrace(buffer, SIZE); if ((s = (char**) backtrace_symbols(buffer, nptrs)) == NULL) { for (j = 0; j < nptrs; j++) printf ("%-5d %p\n", j, buffer[j]); diff --git a/posctl.cc b/posctl.cc index c566a43..997f40e 100644 --- a/posctl.cc +++ b/posctl.cc @@ -484,59 +484,59 @@ void posctl_gui_update() { gtk_widget_set_sensitive(e_posdevice, false); } - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot.a); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_rot.a); gtk_entry_set_text (GTK_ENTRY(e_cal_rotangle), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot.l); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_rot.l); gtk_entry_set_text (GTK_ENTRY(e_cal_rotlen), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1.a); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_axis1.a); gtk_entry_set_text (GTK_ENTRY(e_cal_a1angle), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1.l); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_axis1.l); gtk_entry_set_text (GTK_ENTRY(e_cal_a1len), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2.a); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_axis2.a); gtk_entry_set_text (GTK_ENTRY(e_cal_a2angle), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis2.l); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_axis2.l); gtk_entry_set_text (GTK_ENTRY(e_cal_a2len), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_rot.a - posctl.calib_axis1.a); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_rot.a - posctl.calib_axis1.a); gtk_label_set_text (GTK_LABEL(lb_earthaxis), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.calib_axis1.a - posctl.calib_axis2.a); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.calib_axis1.a - posctl.calib_axis2.a); gtk_label_set_text (GTK_LABEL(lb_axisaxis), txt); gtk_entry_set_text (GTK_ENTRY(e_posdevice), posctl.GetDevice().c_str()); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.axis_op[0]); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.axis_op[0]); gtk_label_set_text(GTK_LABEL(a1_out), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.axis_op[1]); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.axis_op[1]); gtk_label_set_text(GTK_LABEL(a2_out), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.axis_pv[0]); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.axis_pv[0]); gtk_label_set_text(GTK_LABEL(a1_pv), txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", posctl.axis_pv[1]); + strfromd (txt, sizeof(txt-1), (char *)"%f", posctl.axis_pv[1]); gtk_label_set_text(GTK_LABEL(a2_pv), txt); posctl.GetAxisParam(0, &mi, &ma, &kp, &ki, &kd); - strfromd (txt, sizeof(txt-1), (const char *)"%f", mi); + strfromd (txt, sizeof(txt-1), (char *)"%f", mi); gtk_entry_set_text_nofocus (a1_min, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", ma); + strfromd (txt, sizeof(txt-1), (char *)"%f", ma); gtk_entry_set_text_nofocus (a1_max, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", kp); + strfromd (txt, sizeof(txt-1), (char *)"%f", kp); gtk_entry_set_text_nofocus (a1_kp, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", ki); + strfromd (txt, sizeof(txt-1), (char *)"%f", ki); gtk_entry_set_text_nofocus (a1_ki, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", kd); + strfromd (txt, sizeof(txt-1), (char *)"%f", kd); gtk_entry_set_text_nofocus (a1_kd, txt); posctl.GetAxisParam(1, &mi, &ma, &kp, &ki, &kd); - strfromd (txt, sizeof(txt-1), (const char *)"%f", mi); + strfromd (txt, sizeof(txt-1), (char *)"%f", mi); gtk_entry_set_text_nofocus (a2_min, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", ma); + strfromd (txt, sizeof(txt-1), (char *)"%f", ma); gtk_entry_set_text_nofocus (a2_max, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", kp); + strfromd (txt, sizeof(txt-1), (char *)"%f", kp); gtk_entry_set_text_nofocus (a2_kp, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", ki); + strfromd (txt, sizeof(txt-1), (char *)"%f", ki); gtk_entry_set_text_nofocus (a2_ki, txt); - strfromd (txt, sizeof(txt-1), (const char *)"%f", kd); + strfromd (txt, sizeof(txt-1), (char *)"%f", kd); gtk_entry_set_text_nofocus (a2_kd, txt); const gchar *txtptr = gtk_entry_get_text(GTK_ENTRY(e_posdevice)); @@ -575,7 +575,11 @@ PosCtl::PosCtl() { #endif calib_mode = POSCTL_CALIB_MODE_OFF; +#if BUILD_WINDOWS + device_fd = INVALID_HANDLE_VALUE; +#else device_fd = -1; +#endif device = ""; device_type = POSCTL_DEVTYPE_TTY; }; @@ -835,17 +839,42 @@ void PosCtl::SetDevice (std::string d) { int PosCtl::OutputClose() { printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); +#ifdef BUILD_WINDOWS + if (device_fd != INVALID_HANDLE_VALUE) CloseHandle(device_fd); + device_fd = INVALID_HANDLE_VALUE; +#else if (device_fd > 0) close(device_fd); device_fd = -1; - +#endif return 0; }; int PosCtl::OutputOpen() { printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); +#ifdef BUILD_WINDOWS + if (device_fd != INVALID_HANDLE_VALUE) return 0; +#else if (device_fd > 0) return 0; +#endif +#ifdef BUILD_WINDOWS + device_fd = CreateFile(device.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); + if (device_fd == INVALID_HANDLE_VALUE) { + printf ("%s:%d could not open device:%s Error:%ld\n", __FILE__, __LINE__, + device.c_str(), GetLastError()); + errormessage_display ((char *)"window-posctl", (char *)"OutputOpen", + (char*)"%s:%d could not open device:%s Error:%ld\n", __FILE__, __LINE__, device.c_str(), GetLastError()); + return -1; + } + COMMTIMEOUTS comTimeOut; + comTimeOut.ReadIntervalTimeout = 100; + comTimeOut.ReadTotalTimeoutMultiplier = 1; + comTimeOut.ReadTotalTimeoutConstant = 100; + comTimeOut.WriteTotalTimeoutMultiplier = 0; + comTimeOut.WriteTotalTimeoutConstant = 0; + SetCommTimeouts(device_fd, &comTimeOut); +#else device_fd = open (device.c_str(), O_RDWR); if (device_fd < 0) { printf ("%s:%d could not open device:%s Error:%s\n", __FILE__, __LINE__, @@ -854,14 +883,26 @@ int PosCtl::OutputOpen() { (char*)"%s:%d could not open device:%s Error:%s\n", __FILE__, __LINE__, device.c_str(), strerror(errno)); return -1; } +#endif return 0; }; int PosCtl::WriteTTY (char * outbuf) { - ssize_t len; - printf ("%s:%d %s send: '%s'\n", __FILE__, __LINE__, __FUNCTION__, outbuf); +#ifdef BUILD_WINDOWS + DWORD len; + int res; + res = WriteFile(device_fd, outbuf, strlen(outbuf), &len, NULL); + if (res == 0 || len != strlen(outbuf)) { + printf ("%s:%d could not write data to device:%s Error:%ld\n", __FILE__, __LINE__, + device.c_str(), GetLastError()); + errormessage_display ((char *)"window-posctl", (char *)"WriteTTY", + (char *) "%s:%d could not write data to device:%s Error:%ld\n", __FILE__, __LINE__, + device.c_str(), GetLastError()); + } +#else + ssize_t len; len = write (device_fd, outbuf, strlen(outbuf)); if ((size_t) len != strlen(outbuf) || len < 0) { printf ("%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, @@ -870,14 +911,30 @@ int PosCtl::WriteTTY (char * outbuf) { (char *) "%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, device.c_str(), strerror(errno)); } - +#endif return 0; } int PosCtl::ReadTTY (char * inbuf, int length) { +#ifdef BUILD_WINDOWS + DWORD len; + int res; + res = ReadFile(device_fd, inbuf, length, &len, NULL); + + if (res == 0) { + printf ("%s:%d could not read data from device:%s Error:%ld\n", __FILE__, __LINE__, + device.c_str(), GetLastError()); + errormessage_display ((char *)"window-posctl", (char *)"ReadTTY", + (char *) "%s:%d could not read data from device:%s Error:%ld\n", __FILE__, __LINE__, + device.c_str(), GetLastError()); + inbuf[0] = 0; + } + else { + inbuf[len] = 0; + printf ("%s:%d %s receive: '%s'\n", __FILE__, __LINE__, __FUNCTION__, inbuf); + } +#else ssize_t len; - // FIXME: how to use the com/usb-com port on windows. -#ifndef BUILD_WINDOWS // make device non-blocking fcntl(device_fd, F_SETFL, fcntl(device_fd, F_GETFL) | O_NONBLOCK); @@ -916,10 +973,12 @@ int PosCtl::OutputWriteValue (int axis, double value) { simulation.AxisSetValue(axis, value); return 0; } - // FIXME: how to use the com/usb-com port on windows. -#ifndef BUILD_WINDOWS - if (device_fd <= 0) if (OutputOpen() != 0) return -1; +#ifdef BUILD_WINDOWS + if (device_fd == INVALID_HANDLE_VALUE) if (OutputOpen() != 0) return -1; +#else + if (device_fd <= 0) if (OutputOpen() != 0) return -1; +#endif // // save language setting and set to plain C std::string s = setlocale(LC_ALL, NULL); @@ -933,7 +992,7 @@ int PosCtl::OutputWriteValue (int axis, double value) { WriteTTY(outbuf); ReadTTY(outbuf, sizeof(outbuf) - 1); -#endif + return 0; }; @@ -948,7 +1007,11 @@ int PosCtl::OutputWriteStop () { } snprintf (outbuf, 255, ":Q#"); +#ifdef BUILD_WINDOWS + if (device_fd == INVALID_HANDLE_VALUE) if (OutputOpen() != 0) return -1; +#else if (device_fd <= 0) if (OutputOpen() != 0) return -1; +#endif WriteTTY(outbuf); return 0; diff --git a/video.cc b/video.cc index 523f6cc..a9fe8e4 100644 --- a/video.cc +++ b/video.cc @@ -439,7 +439,7 @@ void videoctrl_grid_build () { gtk_range_set_value(GTK_RANGE(scale),value); gtk_widget_set_hexpand (scale,true); gtk_scale_set_draw_value(GTK_SCALE(scale), false); - g_signal_connect (GTK_RANGE(scale), "value-changed", G_CALLBACK(cb_vidctrl_scale_change), (void*)(long int)i); + g_signal_connect (GTK_RANGE(scale), "value-changed", G_CALLBACK(cb_vidctrl_scale_change), (void*)(size_t)i); } else { printf ("%s:%d control %s check not valid min[%d] < max[%d]\n", __FILE__, __LINE__, iter->c_str(), min, max); @@ -449,7 +449,7 @@ void videoctrl_grid_build () { // entry field GtkWidget *entry = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(entry), std::to_string(value).c_str()); - g_signal_connect (entry, "activate", G_CALLBACK(cb_vidctrl_entry_change), (void*)(long int)i); + g_signal_connect (entry, "activate", G_CALLBACK(cb_vidctrl_entry_change), (void*)(size_t)i); gtk_grid_insert_row(GTK_GRID(grid), i); if (i == 0) {