- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在努力移植现有的功能性 C# 应用程序,该应用程序使用 FTDI D2XX C# .NET wrapper在 Windows 上,转向 Linux 和 Mono。我已按照有关安装 Linux D2XX driver 的说明进行操作.当检测到我们在 Linux 上运行时,我们会在 C# .NET 包装器中查找并加载 Linux 驱动程序,而不是 Windows 驱动程序。
完成这项工作后,我们的大部分应用程序代码仍然没有问题,但是当使用 SetEventNotification 函数设置的事件在 FT_EVENT_RXCHAR 事件上触发时,应用程序似乎崩溃了。
C# 应用程序的流程是:
更多细节:
几个问题:
非常感谢您提供的任何见解。
编辑:
堆栈跟踪现在附在下面。
我尝试转向轮询方法,但似乎在我们执行 FTDI 读取时可能会有一个“帧”数据未完全写入,并且我们将在数据中出现间隙。
我已联系 FTDI 寻求有关此问题的一些指导。如果我收到可行的回复,将会更新。
# mono --debug ./CT4USB.exe
Native stacktrace:
mono() [0x49d5fc]
mono() [0x424a0e]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10330) [0x7f3253539330]
/lib/x86_64-linux-gnu/libpthread.so.0(pthread_mutex_lock+0x4) [0x7f3253533404]
/usr/local/lib/libftd2xx.so(+0x12f52) [0x7f324b9d1f52]
/usr/local/lib/libftd2xx.so(+0x134f1) [0x7f324b9d24f1]
/usr/local/lib/libftd2xx.so(processor_thread+0x21b) [0x7f324b9d29bd]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x8184) [0x7f3253531184]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d) [0x7f325325e37d]
Debug info from gdb:
[New LWP 5654]
[New LWP 5653]
[New LWP 5652]
[New LWP 5645]
[New LWP 5644]
[New LWP 5643]
[New LWP 5642]
[New LWP 5641]
[New LWP 5640]
[New LWP 5639]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007f3253538b9d in nanosleep () at ../sysdeps/unix/syscall-template.S:81
81 ../sysdeps/unix/syscall-template.S: No such file or directory.
Id Target Id Frame
11 Thread 0x7f32523ff700 (LWP 5639) "mono" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
10 Thread 0x7f3252a81700 (LWP 5640) "Finalizer" sem_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/sem_wait.S:85
9 Thread 0x7f3253efa700 (LWP 5641) "mono" __clock_nanosleep (clock_id=1, flags=1, req=0x7f3253ef9d80, rem=0x7f325326c974 <__clock_nanosleep+132>) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:49
8 Thread 0x7f324bfff700 (LWP 5642) "Threadpool work" pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
7 Thread 0x7f324bdfe700 (LWP 5643) "Threadpool work" pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
6 Thread 0x7f324b9be700 (LWP 5644) "Threadpool work" 0x00007f3253250fdd in poll () at ../sysdeps/unix/syscall-template.S:81
5 Thread 0x7f324b1bd700 (LWP 5645) "Threadpool work" 0x00007f3253250fdd in poll () at ../sysdeps/unix/syscall-template.S:81
4 Thread 0x7f324a7ff700 (LWP 5652) "Threadpool work" pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
3 Thread 0x7f3249ffe700 (LWP 5653) "Threadpool work" 0x00007f3253538ed9 in __libc_waitpid (pid=5655, stat_loc=0x7f3249ffce4c, options=0) at ../sysdeps/unix/sysv/linux/waitpid.c:40
2 Thread 0x7f32497fd700 (LWP 5654) "Threadpool work" pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
* 1 Thread 0x7f325405d7c0 (LWP 5638) "mono" 0x00007f3253538b9d in nanosleep () at ../sysdeps/unix/syscall-template.S:81
Thread 11 (Thread 0x7f32523ff700 (LWP 5639)):
#0 pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
#1 0x00000000005f9aec in ?? ()
#2 0x00007f3253531184 in start_thread (arg=0x7f32523ff700) at pthread_create.c:312
#3 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 10 (Thread 0x7f3252a81700 (LWP 5640)):
#0 sem_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/sem_wait.S:85
#1 0x000000000061de28 in mono_sem_wait ()
#2 0x00000000005a2076 in ?? ()
#3 0x00000000005843d3 in ?? ()
#4 0x0000000000624666 in ?? ()
#5 0x00007f3253531184 in start_thread (arg=0x7f3252a81700) at pthread_create.c:312
#6 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 9 (Thread 0x7f3253efa700 (LWP 5641)):
#0 __clock_nanosleep (clock_id=1, flags=1, req=0x7f3253ef9d80, rem=0x7f325326c974 <__clock_nanosleep+132>) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:49
#1 0x00000000006122c8 in ?? ()
#2 0x0000000000588244 in ?? ()
#3 0x00000000005843d3 in ?? ()
#4 0x0000000000624666 in ?? ()
#5 0x00007f3253531184 in start_thread (arg=0x7f3253efa700) at pthread_create.c:312
#6 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 8 (Thread 0x7f324bfff700 (LWP 5642)):
#0 pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1 0x00000000005fef90 in ?? ()
#2 0x00000000006117a2 in ?? ()
#3 0x00000000005840fd in ?? ()
#4 0x00000000005853e6 in ?? ()
#5 0x00000000418b9b0e in ?? ()
#6 0x0000000000000000 in ?? ()
Thread 7 (Thread 0x7f324bdfe700 (LWP 5643)):
#0 pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1 0x000000000061b982 in ?? ()
#2 0x0000000000586b58 in ?? ()
#3 0x00000000005843d3 in ?? ()
#4 0x0000000000624666 in ?? ()
#5 0x00007f3253531184 in start_thread (arg=0x7f324bdfe700) at pthread_create.c:312
#6 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 6 (Thread 0x7f324b9be700 (LWP 5644)):
#0 0x00007f3253250fdd in poll () at ../sysdeps/unix/syscall-template.S:81
#1 0x00007f324b9f2a52 in linux_netlink_event_thread_main () from /usr/local/lib/libftd2xx.so
#2 0x00007f3253531184 in start_thread (arg=0x7f324b9be700) at pthread_create.c:312
#3 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 5 (Thread 0x7f324b1bd700 (LWP 5645)):
#0 0x00007f3253250fdd in poll () at ../sysdeps/unix/syscall-template.S:81
#1 0x00007f324b9eb14e in handle_events () from /usr/local/lib/libftd2xx.so
#2 0x00007f324b9eb569 in libusb_handle_events_timeout_completed () from /usr/local/lib/libftd2xx.so
#3 0x00007f324b9eb674 in libusb_handle_events_timeout () from /usr/local/lib/libftd2xx.so
#4 0x00007f324b9c924f in poll_async_libusb () from /usr/local/lib/libftd2xx.so
#5 0x00007f3253531184 in start_thread (arg=0x7f324b1bd700) at pthread_create.c:312
#6 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 4 (Thread 0x7f324a7ff700 (LWP 5652)):
#0 pthread_cond_timedwait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_timedwait.S:238
#1 0x00007f324b9cf9fe in EventWait () from /usr/local/lib/libftd2xx.so
#2 0x00007f324b9d2651 in reader_thread () from /usr/local/lib/libftd2xx.so
#3 0x00007f3253531184 in start_thread (arg=0x7f324a7ff700) at pthread_create.c:312
#4 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 3 (Thread 0x7f3249ffe700 (LWP 5653)):
#0 0x00007f3253538ed9 in __libc_waitpid (pid=5655, stat_loc=0x7f3249ffce4c, options=0) at ../sysdeps/unix/sysv/linux/waitpid.c:40
#1 0x000000000049d689 in ?? ()
#2 0x0000000000424a0e in ?? ()
#3 <signal handler called>
#4 __GI___pthread_mutex_lock (mutex=0x0) at ../nptl/pthread_mutex_lock.c:66
#5 0x00007f324b9d1f52 in signalSomeEvents () from /usr/local/lib/libftd2xx.so
#6 0x00007f324b9d24f1 in ProcessBulkInData () from /usr/local/lib/libftd2xx.so
#7 0x00007f324b9d29bd in processor_thread () from /usr/local/lib/libftd2xx.so
#8 0x00007f3253531184 in start_thread (arg=0x7f3249ffe700) at pthread_create.c:312
#9 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 2 (Thread 0x7f32497fd700 (LWP 5654)):
#0 pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:185
#1 0x00007f324b9cf904 in EventWait () from /usr/local/lib/libftd2xx.so
#2 0x00007f324b9cbfd1 in write_thread () from /usr/local/lib/libftd2xx.so
#3 0x00007f3253531184 in start_thread (arg=0x7f32497fd700) at pthread_create.c:312
#4 0x00007f325325e37d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
Thread 1 (Thread 0x7f325405d7c0 (LWP 5638)):
#0 0x00007f3253538b9d in nanosleep () at ../sysdeps/unix/syscall-template.S:81
#1 0x00000000005fedfa in ?? ()
#2 0x000000000061101b in ?? ()
#3 0x000000000058415e in ?? ()
#4 0x0000000000585309 in ?? ()
#5 0x00000000418b3e8d in ?? ()
#6 0x00000000015de450 in ?? ()
#7 0x00007ffc9ba9b960 in ?? ()
#8 0x00007f3253fb8130 in ?? ()
#9 0x00007f3252400578 in ?? ()
#10 0x00007f3252400528 in ?? ()
#11 0x00000000015a41e0 in ?? ()
#12 0x00000000418b3e07 in ?? ()
#13 0x00007ffc9ba9b4d0 in ?? ()
#14 0x00007ffc9ba9b450 in ?? ()
/build/buildd/gdb-7.7.1/gdb/dwarf2-frame.c:692: internal-error: Unknown CFI encountered.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n) [answered Y; input not from terminal]
/build/buildd/gdb-7.7.1/gdb/dwarf2-frame.c:692: internal-error: Unknown CFI encountered.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Create a core file of GDB? (y or n) [answered Y; input not from terminal]
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
Aborted (core dumped)
编辑第二个:
Mono 的 SafeWaitHandle 和 SetEventNotification 的预期参数的表示完全不同。因为 FTDI D2XX 库试图将 SafeWaitHandle 视为 EVENT_HANDLE(在 ftd2xx.h header 中定义;基本上是 pthread_cond_t 和 pthread_mutex_t),我认为这就是我们发生崩溃的原因。
我想我可以通过在 FTDI D2XX C# .NET 包装器中使用 P/Invoke 和 libpthread 来继续执行 pthread 条件和互斥锁以及 Mono SafeWaitHandle 之间的必要转换。此时,我需要工作的 C 代码,它将在 FTDI D2XX 的 pthread_cond_wait 上循环和阻塞。我还没有得到该代码的工作。如果有人有建议,请在下面列出:
/*/*
gcc -o SetEventNotification_separate_thread SetEventNotification_separate_thread.c -lpthread -lftd2xx -Wl,-rpath /usr/local/lib
*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include "../ftd2xx.h"
static FT_HANDLE ft_handle;
// Seems like this sort of stuff should have been provided by FTDI
static const char *ft_errors[] =
{
[FT_OK] = "Success",
[FT_INVALID_HANDLE] = "Invalid device handle",
[FT_DEVICE_NOT_FOUND] = "Device not found",
[FT_DEVICE_NOT_OPENED] = "Device not opened",
[FT_IO_ERROR] = "Input/output error",
[FT_INSUFFICIENT_RESOURCES] = "Insufficient resources",
[FT_INVALID_PARAMETER] = "Invalid parameter",
[FT_INVALID_BAUD_RATE] = "Invalid baud rate",
[FT_DEVICE_NOT_OPENED_FOR_ERASE] = "Device not opened for erase",
[FT_DEVICE_NOT_OPENED_FOR_WRITE] = "Device not opened for write",
[FT_FAILED_TO_WRITE_DEVICE] = "Failed to write device",
[FT_EEPROM_READ_FAILED] = "EEPROM read failed",
[FT_EEPROM_WRITE_FAILED] = "EEPROM write failed",
[FT_EEPROM_ERASE_FAILED] = "EEPROM erase failed",
[FT_EEPROM_NOT_PRESENT] = "EEPROM not present",
[FT_EEPROM_NOT_PROGRAMMED] = "EEPROM not programmed",
[FT_INVALID_ARGS] = "Invalid argument",
[FT_NOT_SUPPORTED] = "Not supported",
[FT_OTHER_ERROR] = "Other error"
};
static const int num_ft_errors = sizeof(ft_errors) / sizeof(ft_errors[FT_OK]);
static const char *ft_strerror(FT_STATUS ft_status)
{
if (ft_status < 0 // Should be impossible because it is unsigned
|| ft_status >= num_ft_errors
|| !ft_errors[ft_status])
{
return "Unknown error";
}
return ft_errors[ft_status];
}
static void ft_error_exit(const char *str, FT_STATUS ft_status)
{
if (str && *str)
fprintf(stderr, "%s: %sn", str, ft_strerror(ft_status));
else
fprintf(stderr, "%sn", ft_strerror(ft_status));
exit(1);
}
static void pthread_error_exit(const char *str, int err)
{
if (str && *str)
fprintf(stderr, "%s: %sn", str, strerror(err));
else
fprintf(stderr, "%sn", strerror(err));
exit(1);
}
static void std_error_exit(const char *str)
{
perror(str);
exit(1);
}
// Reader thread function
static void *reader_func(void *arg)
{
FT_STATUS ft_status;
EVENT_HANDLE eh;
DWORD chars_in_q;
char buf[8192];
int pterr;
if ((pterr = pthread_mutex_init(&eh.eMutex, NULL)))
pthread_error_exit("pthread_mutex_init", pterr);
if ((pterr = pthread_cond_init(&eh.eCondVar, NULL)))
pthread_error_exit("pthread_cond_init", pterr);
if ((ft_status = FT_SetEventNotification(ft_handle, FT_EVENT_RXCHAR,
(PVOID)&eh)) != FT_OK)
ft_error_exit("FT_SetEventNotification", ft_status);
for (;;)
{
printf("at for: chars_in_q: %d\n", chars_in_q);
if ((pterr = pthread_mutex_lock(&eh.eMutex))){
pthread_error_exit("pthread_mutex_lock", pterr);
printf("pthread_mutex_lock failed...\n");
}
printf("pthread_mutex_lock\n");
if ((ft_status = FT_GetQueueStatus(ft_handle, &chars_in_q)) != FT_OK)
ft_error_exit("FT_GetQueueStatus", ft_status);
printf("before if (chars_in_q == 0): chars_in_q: %d\n", chars_in_q);
// while (chars_in_q == 0)
// {
printf("<<----------- SetEventNotification pthread_cond_wait --------------->>\n");
if ((pterr = pthread_cond_wait(&eh.eCondVar, &eh.eMutex)))
pthread_error_exit("pthread_cond_wait", pterr);
printf("pthread_cond_wait\n");
if ((ft_status = FT_GetQueueStatus(ft_handle,
&chars_in_q)) != FT_OK)
ft_error_exit("FT_GetQueueStatus", ft_status);
printf("before if (chars_in_q == 0): chars_in_q: %d\n", chars_in_q);
// }
if ((pterr = pthread_mutex_unlock(&eh.eMutex)))
pthread_error_exit("pthread_mutex_unlock", pterr);
printf("pthread_mutex_unlock\n");
while (chars_in_q)
{
printf("at while (chars_in_q): chars_in_q: %d\n", chars_in_q);
DWORD len_to_read, len_read;
ssize_t len_written;
len_to_read = (chars_in_q > sizeof(buf)) ? sizeof(buf) : chars_in_q;
if ((ft_status = FT_Read(ft_handle, (LPVOID)buf,
len_to_read, &len_read)) != FT_OK)
ft_error_exit("FT_Read", ft_status);
if (!len_read)
{
fprintf(stderr, "FT_Read returned no data.n");
exit(1);
}
len_written = printf("%s\n", buf);
printf("len_to_read: %d\n", len_to_read);
printf("len_read: %d\n", len_read);
chars_in_q -= len_read;
}
}
}
int main(int argc, char *argv[])
{
FT_STATUS ft_status;
DWORD devCount;
DWORD devIndex = 0; // first device
DWORD numDevices = 1;
char serialNumber[64]; // more than enough room!
const unsigned int baudrate = 8000000;
FT_DEVICE_LIST_INFO_NODE * devInfo;
devInfo = malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevices);
ft_status = FT_CreateDeviceInfoList(&devCount);
if( ft_status != FT_OK)
{
printf("No devices connected!\n");
exit(-1);
}
printf("Devices found: %d\n", devCount);
ft_status = FT_ListDevices((PVOID)devIndex, serialNumber, FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_NUMBER);
if ( ft_status == FT_OK)
{
// FT_ListDevices OK, serial number is in serialNumber
printf("Device Serial Number: %s\n", serialNumber);
}
else
{
// FT_ListDevices failed
}
ft_status = FT_OpenEx(serialNumber, FT_OPEN_BY_SERIAL_NUMBER, &ft_handle);
printf("after calling FT_OpenEx\n");
if ( ft_status == FT_OK)
{
// FT_OpenEx OK, ft_handle not null
printf("FT_OpenEx succeeded\n");
}
else
{
printf("FT_OpenEx failed\n");
}
ft_status = FT_SetBaudRate(ft_handle, baudrate);
if ( ft_status == FT_OK)
{
// FT_SetBaudRate OK
printf("FT_SetBaudRate succeeded\n");
}
else
{
printf("FT_SetBaudRate failed\n");
}
ft_status = FT_SetFlowControl(ft_handle, FT_FLOW_RTS_CTS, 0, 0);
if ( ft_status == FT_OK)
{
// FT_SetFlowControl OK
printf("FT_SetFlowControl succeeded\n");
}
else
{
printf("FT_SetFlowControl failed\n");
}
// Flush the FTDI's buffers
if ((ft_status = FT_Purge(ft_handle, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
ft_error_exit("FT_Purge", ft_status);
// Create reader and writer threads.
int pterr;
sigset_t sig_set;
(void)sigfillset(&sig_set);
pthread_attr_t ptattr;
if ((pterr = pthread_attr_init(&ptattr)))
pthread_error_exit("pthread_attr_init", pterr);
if ((pterr = pthread_attr_setdetachstate(&ptattr,
PTHREAD_CREATE_DETACHED)))
pthread_error_exit("pthread_attr_setdetachstate", pterr);
pthread_t reader_id, writer_id;
if ((pterr = pthread_create(&reader_id, &ptattr, reader_func, NULL)))
pthread_error_exit("pthread_create reader", pterr);
// Now wait for a signal to terminate us
int caught_sig;
if (sigwait(&sig_set, &caught_sig))
std_error_exit("sigwait");
exit(0);
}
gcc -o SetEventNotification_separate_thread SetEventNotification_separate_thread.c -lpthread -lftd2xx -Wl,-rpath /usr/local/lib
*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include "../ftd2xx.h"
static FT_HANDLE ft_handle;
// Seems like this sort of stuff should have been provided by FTDI
static const char *ft_errors[] =
{
[FT_OK] = "Success",
[FT_INVALID_HANDLE] = "Invalid device handle",
[FT_DEVICE_NOT_FOUND] = "Device not found",
[FT_DEVICE_NOT_OPENED] = "Device not opened",
[FT_IO_ERROR] = "Input/output error",
[FT_INSUFFICIENT_RESOURCES] = "Insufficient resources",
[FT_INVALID_PARAMETER] = "Invalid parameter",
[FT_INVALID_BAUD_RATE] = "Invalid baud rate",
[FT_DEVICE_NOT_OPENED_FOR_ERASE] = "Device not opened for erase",
[FT_DEVICE_NOT_OPENED_FOR_WRITE] = "Device not opened for write",
[FT_FAILED_TO_WRITE_DEVICE] = "Failed to write device",
[FT_EEPROM_READ_FAILED] = "EEPROM read failed",
[FT_EEPROM_WRITE_FAILED] = "EEPROM write failed",
[FT_EEPROM_ERASE_FAILED] = "EEPROM erase failed",
[FT_EEPROM_NOT_PRESENT] = "EEPROM not present",
[FT_EEPROM_NOT_PROGRAMMED] = "EEPROM not programmed",
[FT_INVALID_ARGS] = "Invalid argument",
[FT_NOT_SUPPORTED] = "Not supported",
[FT_OTHER_ERROR] = "Other error"
};
static const int num_ft_errors = sizeof(ft_errors) / sizeof(ft_errors[FT_OK]);
static const char *ft_strerror(FT_STATUS ft_status)
{
if (ft_status < 0 // Should be impossible because it is unsigned
|| ft_status >= num_ft_errors
|| !ft_errors[ft_status])
{
return "Unknown error";
}
return ft_errors[ft_status];
}
static void ft_error_exit(const char *str, FT_STATUS ft_status)
{
if (str && *str)
fprintf(stderr, "%s: %sn", str, ft_strerror(ft_status));
else
fprintf(stderr, "%sn", ft_strerror(ft_status));
exit(1);
}
static void pthread_error_exit(const char *str, int err)
{
if (str && *str)
fprintf(stderr, "%s: %sn", str, strerror(err));
else
fprintf(stderr, "%sn", strerror(err));
exit(1);
}
static void std_error_exit(const char *str)
{
perror(str);
exit(1);
}
// Reader thread function
static void *reader_func(void *arg)
{
FT_STATUS ft_status;
EVENT_HANDLE eh;
DWORD chars_in_q;
char buf[8192];
int pterr;
if ((pterr = pthread_mutex_init(&eh.eMutex, NULL)))
pthread_error_exit("pthread_mutex_init", pterr);
if ((pterr = pthread_cond_init(&eh.eCondVar, NULL)))
pthread_error_exit("pthread_cond_init", pterr);
if ((ft_status = FT_SetEventNotification(ft_handle, FT_EVENT_RXCHAR,
(PVOID)&eh)) != FT_OK)
ft_error_exit("FT_SetEventNotification", ft_status);
for (;;)
{
printf("at for: chars_in_q: %d\n", chars_in_q);
if ((pterr = pthread_mutex_lock(&eh.eMutex))){
pthread_error_exit("pthread_mutex_lock", pterr);
printf("pthread_mutex_lock failed...\n");
}
printf("pthread_mutex_lock\n");
if ((ft_status = FT_GetQueueStatus(ft_handle, &chars_in_q)) != FT_OK)
ft_error_exit("FT_GetQueueStatus", ft_status);
printf("before if (chars_in_q == 0): chars_in_q: %d\n", chars_in_q);
// while (chars_in_q == 0)
// {
printf("<<----------- SetEventNotification pthread_cond_wait --------------->>\n");
if ((pterr = pthread_cond_wait(&eh.eCondVar, &eh.eMutex)))
pthread_error_exit("pthread_cond_wait", pterr);
printf("pthread_cond_wait\n");
if ((ft_status = FT_GetQueueStatus(ft_handle,
&chars_in_q)) != FT_OK)
ft_error_exit("FT_GetQueueStatus", ft_status);
printf("before if (chars_in_q == 0): chars_in_q: %d\n", chars_in_q);
// }
if ((pterr = pthread_mutex_unlock(&eh.eMutex)))
pthread_error_exit("pthread_mutex_unlock", pterr);
printf("pthread_mutex_unlock\n");
while (chars_in_q)
{
printf("at while (chars_in_q): chars_in_q: %d\n", chars_in_q);
DWORD len_to_read, len_read;
ssize_t len_written;
len_to_read = (chars_in_q > sizeof(buf)) ? sizeof(buf) : chars_in_q;
if ((ft_status = FT_Read(ft_handle, (LPVOID)buf,
len_to_read, &len_read)) != FT_OK)
ft_error_exit("FT_Read", ft_status);
if (!len_read)
{
fprintf(stderr, "FT_Read returned no data.n");
exit(1);
}
len_written = printf("%s\n", buf);
printf("len_to_read: %d\n", len_to_read);
printf("len_read: %d\n", len_read);
chars_in_q -= len_read;
}
}
}
int main(int argc, char *argv[])
{
FT_STATUS ft_status;
DWORD devCount;
DWORD devIndex = 0; // first device
DWORD numDevices = 1;
char serialNumber[64]; // more than enough room!
const unsigned int baudrate = 8000000;
FT_DEVICE_LIST_INFO_NODE * devInfo;
devInfo = malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevices);
ft_status = FT_CreateDeviceInfoList(&devCount);
if( ft_status != FT_OK)
{
printf("No devices connected!\n");
exit(-1);
}
printf("Devices found: %d\n", devCount);
ft_status = FT_ListDevices((PVOID)devIndex, serialNumber, FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_NUMBER);
if ( ft_status == FT_OK)
{
// FT_ListDevices OK, serial number is in serialNumber
printf("Device Serial Number: %s\n", serialNumber);
}
else
{
// FT_ListDevices failed
}
ft_status = FT_OpenEx(serialNumber, FT_OPEN_BY_SERIAL_NUMBER, &ft_handle);
printf("after calling FT_OpenEx\n");
if ( ft_status == FT_OK)
{
// FT_OpenEx OK, ft_handle not null
printf("FT_OpenEx succeeded\n");
}
else
{
printf("FT_OpenEx failed\n");
}
ft_status = FT_SetBaudRate(ft_handle, baudrate);
if ( ft_status == FT_OK)
{
// FT_SetBaudRate OK
printf("FT_SetBaudRate succeeded\n");
}
else
{
printf("FT_SetBaudRate failed\n");
}
ft_status = FT_SetFlowControl(ft_handle, FT_FLOW_RTS_CTS, 0, 0);
if ( ft_status == FT_OK)
{
// FT_SetFlowControl OK
printf("FT_SetFlowControl succeeded\n");
}
else
{
printf("FT_SetFlowControl failed\n");
}
// Flush the FTDI's buffers
if ((ft_status = FT_Purge(ft_handle, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
ft_error_exit("FT_Purge", ft_status);
// Create reader and writer threads.
int pterr;
sigset_t sig_set;
(void)sigfillset(&sig_set);
pthread_attr_t ptattr;
if ((pterr = pthread_attr_init(&ptattr)))
pthread_error_exit("pthread_attr_init", pterr);
if ((pterr = pthread_attr_setdetachstate(&ptattr,
PTHREAD_CREATE_DETACHED)))
pthread_error_exit("pthread_attr_setdetachstate", pterr);
pthread_t reader_id, writer_id;
if ((pterr = pthread_create(&reader_id, &ptattr, reader_func, NULL)))
pthread_error_exit("pthread_create reader", pterr);
// Now wait for a signal to terminate us
int caught_sig;
if (sigwait(&sig_set, &caught_sig))
std_error_exit("sigwait");
exit(0);
}
它总是卡在 pthread_cond_wait 上。如有任何想法,我们将不胜感激。
最佳答案
再次没有答案,但我不知道 Mono 是如何定义的EventWaitHandle.SafeWaitHandle.
在 FTDI for Linux (c) 提供的示例中,这是定义
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
EVENT_HANDLE eh;
DWORD EventMask;
ftStatus = FT_Open(0, &ftHandle);
if(ftStatus != FT_OK) {
// FT_Open failed
return;
}
pthread_mutex_init(&eh.eMutex, NULL);
pthread_cond_init(&eh.eCondVar, NULL);
EventMask = FT_EVENT_RXCHAR | FT_EVENT_MODEM_STATUS;
ftStatus = FT_SetEventNotification(ftHandle, EventMask, (PVOID)&eh);
所以我想知道这是否与SafeWaitHandle的定义有关
关于c# - 在 Linux/Mono 上运行的 FTDI D2XX C# .NET Wrapper - SetEventNotification 被触发似乎使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37056602/
这个问题已经有答案了: jQuery trigger click vs click ()? (3 个回答) 已关闭 5 年前。 我无法区分 trigger('click')与 trigger('cli
我正在运行 VS 2008 和 .NET 3.5 SP1。 我想在 HttpModule 中实现命中跟踪在我的 ASP.NET 应用程序中。很简单,我想。然而,BeginRequest我的事件 Htt
这是一段代码,我收到以下错误 #1064 - You have an error in your SQL syntax; check the manual that corresponds to yo
有没有办法用任意增量触发滚轮事件。就像 jQuery 对“点击”所做的那样: $('#selector').trigger('click'); 我需要类似的东西,只需一个滚轮即可: $('#selec
我正在尝试在配音数据库中触发时间。我想检查一下在不出现角色的电影配音中不能对角色进行配音。这是PDM: 和CDM 我是SQL的初学者,但我知道表“DUBBES”中应该有一些触发器。我试图做这样的事情,
这个问题已经有答案了: jquery programmatically click on new dom element (3 个回答) 已关闭 6 年前。 我有一个 jQuery 事件定义如下: $
主菜单的点击代码适用于类更改,但不适用于子菜单...当单击食物或鞋子等子菜单项时,它不会触发警报命令...事实上,悬停非常适合子菜单但不是活跃的 HTML
问题非常简单: $('#btn1').click(function(event){ alert( "pageX: " + event.pageX + "\npa
我使用 Spring 的调度程序 (@EnableScheduling) 并具有以下 @Scheduled 方法,该方法每分钟调用一次: @Component public class Schedul
错误 SQL 查询:文档 CREATE TRIGGER `triggers_div` AFTER INSERT ON `produits` FOR EACH ROW BEGIN INSERT INTO
我想在插入另一个表时填充表中的一些列值,并为特定列设置条件。我使用触发器: CREATE TRIGGER inserttrigger AFTER INSERT ON table1 FOR EACH R
我可以在 5.6 MySQL 环境中使用一些关于触发器的指导。我想创建一个触发器,如果发现具有相同速度的电脑的价格较低,则该触发器会停止更新。 架构是产品(制造商、型号、类型)PC(型号、速度、内
背景:我们有一个 completed_flag,默认为 0,当有人完成调查时更新为 1。我想记录这次更新发生的时间戳 在编写了这个触发器/函数以在标志从 0 触发到 1 时更新时间戳后,我怀疑我这样做
数据库中有两个表 KistStatus和 LastKistStatus .后者将保存 KistStatus 的所有“最新”值。 . KistStatus有大约 174.000 条记录,LastKist
我正在开发一个使用 APNS 的 iPhone 应用程序。我很清楚实现 APNS、创 build 备 token 的过程,等等等等……我不知道如何通过 Web 服务从提供商端触发和启动 APNS。任何
我有这个 javascript,当数量更改时会触发 update_cart... jQuery('div.woocommerce').on('change', '.qty', function
当我单击任何按钮时,click 事件不会被触发。艰难的是,我使用 $("div").on("click", "button", function () { 让它工作,但我想看到它使用 .class 工
如何在我的代码中触发 Android onCreateOptionsMenu 函数,即无需用户单击手机上的选项菜单按钮? 最佳答案 Activity.openOptionsMenu(); 就可以了 关
我将表单包装在 中然后我设置 list android:windowSoftInputMode="adjustResize" (默认 react native )。现在,当我用手指触摸事件手动聚焦一
我有一个 Android 编程问题。使用下面的代码我想验证一个字符串匹配。它验证正常,但 LogCat 显示 TextWatcher 方法在每次击键时触发两次,我不明白为什么。我希望每次击键只触发一次
我是一名优秀的程序员,十分优秀!