請問高手們:為什麼我的write會一直無法寫入我指定的文件裡面呢?? 執行時會一直顯示 -1(錯誤訊息)
我是在LINUX系統下的操作~(我的serial: ttyS0,ttyS1 權限都已開通)
以下是我的程式,謝謝@@:
我還是不懂為何會無法寫入-.-" (程式執行時,接收端 會print:write to baseband.txt failed)->結果777.txt內容無法寫入baseband.txt裡面,接收確定有收到從傳送端的內容,因為我有print出來傳送端txt裡面的內文~
/********************************************
* send data
* fdcom: data: datalen:
*********************************************/
int PortSend(int fdcom, char *data, int datalen)
{
int len = 0;
len = write(fdcom, data, datalen);
if(len == datalen){
return (len);
}
else{
tcflush(fdcom, TCOFLUSH);
return -1;
}
}
/*******************************************
receive data
********************************************/
int PortRecv(int fdcom, char *data, int datalen, int baudrate)
{
int readlen, fs_sel;
fd_set fs_read;
struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fdcom, &fs_read);
tv_timeout.tv_sec = TIMEOUT_SEC(datalen, baudrate);
tv_timeout.tv_usec = TIMEOUT_USEC;
fs_sel = select(fdcom+1, &fs_read, NULL, NULL, &tv_timeout);
if(fs_sel){
readlen = read(fdcom, data, datalen);
return(readlen);
}
else{
return(-1);
}
return (readlen);
}
//*******************Test*************************//
int main(int argc, char *argv[])
{
int fdcom,fd, SendLen, RecvLen,fs,res,c,nread;
struct termios termios_cur;
char buffer[1024];
char buff[1024];
portinfo_t portinfo ={
'0', // print prompt after receiving
38400, // baudrate:38400
'8', // databit: 8
'0', // debug: on
'0', // echo: on
'2', // flow control:
'1', // default tty:
'0', // parity: none
'1', // stopbit: 1
0 // reserved
};
fdcom = PortOpen(&portinfo); /*open serial :ttyS1 */
if(fdcom>0)
{
printf("open serial port: /dev/ttyS1 OK!\n");
}
PortSet(fdcom,&portinfo);
if(atoi(argv[1]) == 0)
{
//send data TX
fd = open("/mnt/777.txt", O_RDWR | O_SYNC ); /*open serial port */
res=read(fd,buffer,1024);
SendLen = PortSend(fdcom,buffer,1024);
if(SendLen>0){
printf("send data is: %s. NO is:%d \n ",buffer, res);
}
else{
printf("error:send failed.\n");
}
PortClose(fdcom);
}
if(atoi(argv[1]) == 1)
{
//receive data RX
for(;;)
{
fs = open("/mnt/test/baseband.txt", O_RDWR | O_SYNC | O_CREAT , S_IRWXU | S_IRWXO );
RecvLen=PortRecv(fdcom,buff,1024,portinfo.baudrate);
c=write(fs,buff,strlen(buff));
close(fs);
if(c<0)
{
printf("write to baseband.txt failed.\n");
printf("erron is: %s\n",strerror);
}
if( RecvLen>0 )
{
printf("Receive data is: %s NO is:%d %d\n.", buff , RecvLen, c );
}
else{
printf("Receive error\n");
}
sleep(1);
}
}
close(fdcom); //close port ttyS1
exit(0);
}
煩請指導小妹 謝謝^^"