为什么write()函数会像终端输出些ascii码
本帖最后由 cxt492953150 于 2011-05-13 10:57 编辑
void roadway1DataSend(int fd )
{
int i;
unsigned int roadData;
int dirFlag1 = 0;
unsigned char high ,low;
unsigned char direct[]={0x10,0x12,0x11,0x13};
while(1){
if(dirFlag1 == 0){
for( i=0; i<4;i++)
{
pthread_mutex_lock( &mutex);
buff[j++] = direct;
roadData = 1+(unsigned int)(maxData * rand() /(RAND_MAX + 1.0));
high = roadData >> 8;
low = roadData;
if(i==0)
{
high = 0x00;
low = 0x00;
}
buff[j++] = high;
buff[j++] = low;
printf("the roadway1 send %d data: %x ,%x ,%x\n",i+1,buff[0],buff[1],buff[2]);
write(fd,buff,3);
j -= 3;
pthread_mutex_unlock( &mutex);
Delay(delay_time);
// sleep(sleep_time);
}
}
int main(int argc, char **argv)
{
int fd;
pthread_t road1_id,road2_id,road3_id;
int road1Thread,road2Thread,road3Thread;
pthread_mutex_init(&mutex ,NULL);
delay.tv_sec = 0;
delay.tv_nsec = 500000000;
if((fd=open_port(fd,4))<0){
perror("open_port Error"
return;
}
set_speed(fd,9600);
if (set_Parity(fd,8,1,'N') < 0) {
printf("Set Parity Error\n"
exit(1);
}
srand((int)time(0));
delay_time = 100000000 + (int)(50000000.0 * rand()/(RAND_MAX + 1.0));
printf(" \n\n********************start send data************************* \n\n"
road1Thread = pthread_create(&road1_id,NULL,(void*)roadway1DataSend,NULL);
if(road1Thread)
{
printf("create road1Thread error ! " ;
return 1;
}
road2Thread = pthread_create(&road2_id,NULL,(void*)roadway2DataSend,NULL);
if(road2Thread)
{
printf("create road2Thread error ! " ;
return 1;
}
road3Thread = pthread_create(&road3_id,NULL,(void*)roadway3DataSend,NULL);
if(road3Thread)
{
printf("create road3Thread error ! " ;
return 1;
}
//*/
pthread_join(road1_id,NULL);
close(fd);
exit(0);
}
请求哪位 大哥给点帮助……,用多线程的话:write()会给终端打印出将要输出的字符,但是不用线程的时候就不会打印,这是为什么??怎样解决,让他不打印,还要仍然是多线程
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本帖最后由 cxt492953150 于 2011-05-13 10:14 编辑
for( i=0; i<4;i++)
{
buff[j++] = direct;
roadData = 1+(unsigned int)(maxData * rand() /(RAND_MAX + 1.0));
high = roadData >> 8;
low = roadData;
if(i==0)
{
high = 0x00;
low = 0x00;
}
buff[j++] = high;
buff[j++] = low;
printf("the roadway1 send %d data: %x ,%x ,%x\n",i+1,buff[0],buff[1],buff[2]);
write(fd,buff,3);
j -= 3;
Delay(delay_time);
}
}
请求指点 感激不尽