WEB开发网
开发学院操作系统Linux/Unix linux下串口控制 阅读

linux下串口控制

 2013-08-12 16:13:26 来源:WEB开发网   
核心提示:}else{return(-1);}return (readlen);}//*Test*int main(int argc, char *argv[]){int fdcom, i, SendLen, RecvLen;struct termios termios_cur;char RecvBuf[256];char wr
}
else{
return(-1);
}
return (readlen);
}
//*************************Test*********************************
int main(int argc, char *argv[])
{
int fdcom, i, SendLen, RecvLen;
struct termios termios_cur;
char RecvBuf[256];
char writeBuf[256];
portinfo_t portinfo ={
'0', // print prompt after receiving
9600, // baudrate: 9600
'8', // databit: 8
'0', // debug: off
'0', // echo: off
'2', // flow control: software
'0', // default tty: COM1
'0', // parity: none
'1', // stopbit: 1
0 // reserved
};
if(argc != 2){
printf("Usage: <type 0 -- send 1 -- receive>\n");
printf(" eg:");
printf(" MyPort 0");
exit(-1);
}
fdcom = PortOpen(&portinfo);
if(fdcom<0){
printf("Error: open serial port error.\n");
exit(1);
}
PortSet(fdcom, &portinfo);
while(1)
if(atoi(argv[1]) == 0)
{
//send data
//for(i=0; i<1000; i++)
{
//fgets(writeBuf,256,stdin);
scanf("%s",writeBuf);
SendLen = PortSend(fdcom, writeBuf, strlen(writeBuf));
if(SendLen>0)
{
//printf("send data %s", writeBuf);
}
else
{
printf("Error: send failed.\n");
}
if(strncmp(writeBuf,"exit",4)==0)
break;
sleep(1);
}
//PortClose(fdcom);
}
else
{
//for(;;)
{
RecvLen = PortRecv(fdcom, RecvBuf, 256, portinfo.baudrate);
if(RecvLen>0)
{
/*for(i=0; i<RecvLen; i++)
{
printf("Receive data No %d is %x.\n", i, RecvBuf[i]);
}//*/
RecvBuf[RecvLen] = '\0';
printf("read data %s\n",RecvBuf);
if(strncmp(RecvBuf,"exit",4)==0)
break;
}
else
{
//printf("Error: receive error.\n");
}
sleep(1);
}
}
PortClose(fdcom);
return 0;
}

上一页  1 2 3 

Tags:linux 串口 控制

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接