【发布时间】:2018-09-27 13:51:59
【问题描述】:
我正在为 Linux 操作系统编写 C 程序。
程序可以启动一个定时器:主程序和定时器都可以在串口上发送和接收字符。
我的尝试是通过一个全局结构中的互斥锁序列化串行端口访问,该结构在打开时初始化为:
if (pthread_mutex_init( &pED->lockSerial, NULL) != 0)
{
lwsl_err("lockSerial init failed\n");
}
我保护了所有在端口上发送数据的函数如下:
ssize_t cmdFirmwareVersion(EngineData *pED)
{
if (pED->fdSerialPort==-1)
return -1;
LOCK_SERIAL;
unsigned char cmd[] = { 0x00, 0x00, 0x7F };
write( pED->fdSerialPort, cmd, sizeof(cmd));
int rx = read ( pED->fdSerialPort, rxbuffer, sizeof rxbuffer);
dump( rxbuffer, rx);
UNLOCK_SERIAL;
return rx;
}
在哪里
#define LOCK_SERIAL if (0!=pthread_mutex_lock(&pED->lockSerial)) {printf("Err lock");return 0;}
#define UNLOCK_SERIAL pthread_mutex_unlock(&pED->lockSerial);
运行程序并启动计时器我看到请求是有规律的。当我以其他方式(从 rx websocket 函数)触发其中一个调用时,程序挂起,我需要杀死它。
为什么整个程序都停止了??
【问题讨论】:
标签: linux serial-port synchronization mutex