Serial interface in Windows working

master
U-INFINEON\jahnst 3 years ago
parent b4cbaa7bf8
commit 0a504ba742

@ -5,7 +5,7 @@
# #
USE_SVBONY = 1 USE_SVBONY = 1
USE_DNG = 1 USE_DNG = 1
USE_SER = 0 USE_SER = 1
DEBUG_ANGLES = 1 DEBUG_ANGLES = 1
DEBUG_POSCTL = 1 DEBUG_POSCTL = 1

@ -186,7 +186,7 @@ inline unsigned char clamp (double x) {
inline void convert2rgb (unsigned char Y1, unsigned char Cb, unsigned char Cr, inline void convert2rgb (unsigned char Y1, unsigned char Cb, unsigned char Cr,
unsigned char *ER, unsigned char *EB, unsigned char *EG) { unsigned char *ER, unsigned char *EB, unsigned char *EG) {
register int y1, pb, pr; int y1, pb, pr;
y1 = Y1 - 16; y1 = Y1 - 16;
pb = Cb - 128; pb = Cb - 128;

@ -55,7 +55,11 @@ class PosCtl {
PID pid_axis[2]; PID pid_axis[2];
std::string device; std::string device;
int device_type; int device_type;
#ifdef BUILD_WINDOWS
HANDLE device_fd;
#else
int device_fd; int device_fd;
#endif
struct timeval calib_timestamp; struct timeval calib_timestamp;
int calib_mode; int calib_mode;
position_2d calib_pos; position_2d calib_pos;

@ -80,13 +80,13 @@ void errorexit (char *fmt,...) {
printf ("***************************\n"); printf ("***************************\n");
printf ("%s", text); printf ("%s", text);
#ifndef BUILD_WINDOWS
// //
// backtrace // backtrace
int j, nptrs; int j, nptrs;
void *buffer[SIZE]; void *buffer[SIZE];
char **s; char **s;
#ifndef BUILD_WINDOWS
nptrs = backtrace(buffer, SIZE); nptrs = backtrace(buffer, SIZE);
if ((s = (char**) backtrace_symbols(buffer, nptrs)) == NULL) { if ((s = (char**) backtrace_symbols(buffer, nptrs)) == NULL) {
for (j = 0; j < nptrs; j++) printf ("%-5d %p\n", j, buffer[j]); for (j = 0; j < nptrs; j++) printf ("%-5d %p\n", j, buffer[j]);

@ -484,59 +484,59 @@ void posctl_gui_update() {
gtk_widget_set_sensitive(e_posdevice, false); 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); 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); 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); 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); 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); 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); 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); 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_label_set_text (GTK_LABEL(lb_axisaxis), txt);
gtk_entry_set_text (GTK_ENTRY(e_posdevice), posctl.GetDevice().c_str()); 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); 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); 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); 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); gtk_label_set_text(GTK_LABEL(a2_pv), txt);
posctl.GetAxisParam(0, &mi, &ma, &kp, &ki, &kd); 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); 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); 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); 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); 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); gtk_entry_set_text_nofocus (a1_kd, txt);
posctl.GetAxisParam(1, &mi, &ma, &kp, &ki, &kd); 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); 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); 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); 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); 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); gtk_entry_set_text_nofocus (a2_kd, txt);
const gchar *txtptr = gtk_entry_get_text(GTK_ENTRY(e_posdevice)); const gchar *txtptr = gtk_entry_get_text(GTK_ENTRY(e_posdevice));
@ -575,7 +575,11 @@ PosCtl::PosCtl() {
#endif #endif
calib_mode = POSCTL_CALIB_MODE_OFF; calib_mode = POSCTL_CALIB_MODE_OFF;
#if BUILD_WINDOWS
device_fd = INVALID_HANDLE_VALUE;
#else
device_fd = -1; device_fd = -1;
#endif
device = ""; device = "";
device_type = POSCTL_DEVTYPE_TTY; device_type = POSCTL_DEVTYPE_TTY;
}; };
@ -835,17 +839,42 @@ void PosCtl::SetDevice (std::string d) {
int PosCtl::OutputClose() { int PosCtl::OutputClose() {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); 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); if (device_fd > 0) close(device_fd);
device_fd = -1; device_fd = -1;
#endif
return 0; return 0;
}; };
int PosCtl::OutputOpen() { int PosCtl::OutputOpen() {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__); 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; 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); device_fd = open (device.c_str(), O_RDWR);
if (device_fd < 0) { if (device_fd < 0) {
printf ("%s:%d could not open device:%s Error:%s\n", __FILE__, __LINE__, 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)); (char*)"%s:%d could not open device:%s Error:%s\n", __FILE__, __LINE__, device.c_str(), strerror(errno));
return -1; return -1;
} }
#endif
return 0; return 0;
}; };
int PosCtl::WriteTTY (char * outbuf) { int PosCtl::WriteTTY (char * outbuf) {
ssize_t len;
printf ("%s:%d %s send: '%s'\n", __FILE__, __LINE__, __FUNCTION__, outbuf); 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)); len = write (device_fd, outbuf, strlen(outbuf));
if ((size_t) len != strlen(outbuf) || len < 0) { if ((size_t) len != strlen(outbuf) || len < 0) {
printf ("%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__, 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__, (char *) "%s:%d could not write data to device:%s Error:%s\n", __FILE__, __LINE__,
device.c_str(), strerror(errno)); device.c_str(), strerror(errno));
} }
#endif
return 0; return 0;
} }
int PosCtl::ReadTTY (char * inbuf, int length) { 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; ssize_t len;
// FIXME: how to use the com/usb-com port on windows.
#ifndef BUILD_WINDOWS
// make device non-blocking // make device non-blocking
fcntl(device_fd, F_SETFL, fcntl(device_fd, F_GETFL) | O_NONBLOCK); 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); simulation.AxisSetValue(axis, value);
return 0; 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 // save language setting and set to plain C
std::string s = setlocale(LC_ALL, NULL); std::string s = setlocale(LC_ALL, NULL);
@ -933,7 +992,7 @@ int PosCtl::OutputWriteValue (int axis, double value) {
WriteTTY(outbuf); WriteTTY(outbuf);
ReadTTY(outbuf, sizeof(outbuf) - 1); ReadTTY(outbuf, sizeof(outbuf) - 1);
#endif
return 0; return 0;
}; };
@ -948,7 +1007,11 @@ int PosCtl::OutputWriteStop () {
} }
snprintf (outbuf, 255, ":Q#"); 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; if (device_fd <= 0) if (OutputOpen() != 0) return -1;
#endif
WriteTTY(outbuf); WriteTTY(outbuf);
return 0; return 0;

@ -439,7 +439,7 @@ void videoctrl_grid_build () {
gtk_range_set_value(GTK_RANGE(scale),value); gtk_range_set_value(GTK_RANGE(scale),value);
gtk_widget_set_hexpand (scale,true); gtk_widget_set_hexpand (scale,true);
gtk_scale_set_draw_value(GTK_SCALE(scale), false); 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 { else {
printf ("%s:%d control %s check not valid min[%d] < max[%d]\n", __FILE__, __LINE__, iter->c_str(), min, max); 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 // entry field
GtkWidget *entry = gtk_entry_new(); GtkWidget *entry = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry), std::to_string(value).c_str()); 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); gtk_grid_insert_row(GTK_GRID(grid), i);
if (i == 0) { if (i == 0) {

Loading…
Cancel
Save