void* com_recv_upper(void *param)
{
uint8_t recv_buffer[COM_BUFFER_SIZE] = {0};
int32_t len = 0;
while(1)
{
if(upper_device_comfd > 0)
{
memset(recv_buffer,0,sizeof(recv_buffer));
len = read(upper_device_comfd,recv_buffer,sizeof(recv_buffer));
if(len > 0) printf("com recv thread : %s\n",recv_buffer);
}
else // 串口异常或是关闭了
{
break;
}
}
pthread_exit(0);
//用pthread_exit()来调用线程的返回值,用来退出线程,但是退出线程所占用的资源不会随着线程的终止而得到释放
return NULL;
}
/* 串口的初始化 */
int32_t com_init_upper(void *param)
{
int32_t ret = 0;
;
upper_device_comfd = open(UPPER_DEVICE_COM,O_RDWR | O_NOCTTY | O_NDELAY);
if(upper_device_comfd< 0)
{
ret = -COM_OPEN_ERROR;
}
else
{
if((ret = _com_set_upper()) == OPER_OK)
{
if((ret = pthread_create(&com_recv_threadid,NULL,com_recv_upper,NULL))!= 0)
ret = -THREAD_CREATE_ERROR;
}
}
return ret;
}
/* 串口的发送
* 返回值: 正常返回发送的字节数,0表示没有数据需要发送。
* -1表示异常,也可通过errno获取异常码.
*/
int32_t com_send_upper(uint8_t *send_buffer,int32_t buffer_len)
{
int32_t ret = 0;
// 疑问1:发送数据的长度是否需要判断有效值,有效值是多少??
ret = write(upper_device_comfd, send_buffer, buffer_len);
return ret;
}
/* 串口参数设置
* 波特率,停止位,校验位,流控制,异步同步等
*/
int32_t com_set_upper(void *param)
{
int32_t ret = 0;
return ret;
}
/* 串口设置参数生效 */
int32_t com_set_effect_upper(void *param)
{
int32_t ret = 0;
return ret;
}
/* 串口关闭*/
int32_t com_close_upper(void *param)
{
int32_t ret = 0;
if(upper_device_comfd > 0 )
{
close(upper_device_comfd);
upper_device_comfd = 0;
}
return ret;
}
/* 串口接收线程关闭 */
int32_t com_recv_thread_close(void *param)
{
int32_t ret = 0;
pthread_join(com_recv_threadid,NULL);
return ret;
}
/*
* upper,上位机
* }
*/ |