这两个c程序 怎么改成java代码? java高手指教啊。急 .fst.h#ifndef fst_h
#define fst_h#include <stdio.h>
#include "std_c.h"
/*--------------------------------*/
/*                                */
/*--------------------------------*/
#define WAITTING_SHORT 5
#define WAITTING_LONG  10#define NAK     0x15
#define ACK     0x06
#define SOH     0x01
#define EOT     0x04
#define STX     0x02
#define YSTART  'C'#define ERROR_PARAMETERS  10
#define ERROR_OPENPORT    20
#define ERROR_OPENFILE    30
#define ERROR_READFILE    40
#define ERROR_CREATEFILE  50
#define ERROR_REPLY       60
#define ERROR_WAITTINGOUT 70
#define ERROR_THREETIMES  80
#define ERROR_LARGEFILE   90
#define ERROR_NODATA      92
#define ERROR_INVALIDECHO 93
/*-------------------------------*/
extern void   checkCRCYModem(u_char* strBuf,u_int nBytesCount,
     u_char* pCRC1,
     u_char* pCRC2);
extern u_char checkAdd(u_char *strBuf,u_int bytecnt);
extern int    GetReceiverResponse(u_char* inBuf,int nWaittingTime);
extern int    SendYModem(u_char *  sendFileName,int waitSec);
extern int    SendFileNameFrame(u_char * sendFileName);
extern int    SendEndFrame(void);
extern int DealYmodemInBuf(u_char *ps);
extern int ReceiveYModemDll(u_char * pReceiveFile,int waitSec);
extern int GetYModemSenderPage(u_char* pInBuf,int nWaittingTime);#endif

解决方案 »

  1.   

    2.  fst.c
    #include    <stdio.h>
    #include    "lib_usart.h"
    #include    "lib_power_save.h"
    #include    "lib_r40807.h"
    #include  "fst.h"
    u_char receivefile[7000];
    int nBetweenTime;
    int nBetweenTime1;
    /*------------------------------*/
    /* CRC校验函数                   */
    /*------------------------------*/
    void checkCRCYModem(u_char* strBuf,u_int nBytesCount,u_char* pCRC1,u_char* pCRC2)
    {
    u_int i,j;
    u_char aByte=0,tmp=0,crc1=0,crc2=0,extra[3];
    *pCRC1=0;
    *pCRC2=0;
    extra[0]=0;
    extra[1]=0;
    for(i=0;i<nBytesCount;i++){
    aByte=strBuf[i];
    for(j=0;j<=7;j++){
    tmp=crc1&0x80 ;
    crc1=crc1<<1;
    if((crc2&0x80)!=0) crc1=crc1|0x01;
    else crc1=crc1&0x0fe;
    crc2=crc2<<1;
    if((aByte&0x80)!=0) crc2=crc2|0x01;
    else crc2=crc2&0x0fe;
    aByte=aByte<<1;
    if(tmp!=0){
    crc1=crc1^0x10;
    crc2=crc2^0x21;
    }
    }
    }
    for(i=0;i<2;i++){
    aByte=extra[i];
    for(j=0;j<=7;j++){
    tmp=crc1&0x80 ;
    crc1=crc1<<1;
    if((crc2&0x80)!=0) crc1=crc1|0x01;
    else crc1=crc1&0x0fe;
    crc2=crc2<<1;
    if((aByte&0x80)!=0) crc2=crc2|0x01;
    else crc2=crc2&0x0fe;
    aByte=aByte<<1;
    if(tmp!=0){
    crc1=crc1^0x10;
    crc2=crc2^0x21;
    }
    }
    }
    *pCRC1=crc1;
    *pCRC2=crc2;
    }
    /*------------------------------*/
    /* CHECKSUM校验                 */
    /*------------------------------*/
    u_char checkAdd(u_char *strBuf,u_int bytecnt)
    {
    u_char cRet=0x00;
    u_int i;
    for ( i = 0; i < bytecnt; i++)
    cRet += *(strBuf++);
    return cRet;
    }
    /*------------------------------*/
    /* 发文件处理函数                   */
    /*------------------------------*/
    #if 0
    int GetReceiverResponse(u_char* inBuf,int nWaittingTime)
    {
        nBetweenTime=0;    while(nBetweenTime <= nWaittingTime){
    if((USART0_DESC.usart_base->US_CSR & US_RXRDY) !=0){
    inBuf[0] = USART0_DESC.usart_base->US_RHR;
    if ((inBuf[0] == ACK)||(inBuf[0] == NAK)||(inBuf[0] == YSTART)){
    return TRUE;
    }
    }
    }
    return FALSE;
    }
    int SendFileNameFrame(u_char* sendFileName)
    { int nFileNameLength;
    int i,j;
    int nRepeat,IsNull;
    u_char SendBuffer[150],inbuf[10],crc1,crc2,*pTem;
    for(i=0;i<132;i++) SendBuffer[i]=0x00; //filled with 0x00 pTem = sendFileName;
    while (*(pTem++) != 0);
    while ((*pTem) != 0x5c&&(pTem!=sendFileName)) pTem--; //0x5c='\'
    if ((*pTem) == 0x5c) pTem++;
    nFileNameLength = strlen((const char*)pTem);// - sendFile;
    strcpy((char *)(SendBuffer+3),(const char*)pTem); //file name
    SendBuffer[0] = SOH;
    SendBuffer[1] = 0;
    SendBuffer[2] = 255;
    SendBuffer[nFileNameLength+3] = 0; checkCRCYModem(SendBuffer+3,128,&crc1,&crc2);
    SendBuffer[131]=crc1;
    SendBuffer[132]=crc2;

    IsNull = TRUE;
        for (nRepeat = 0; nRepeat <= 3; nRepeat++) {
        at91_usart_send_frame(&USART0_DESC,SendBuffer,133);
    nBetweenTime = 0;
    while(nBetweenTime <= WAITTING_LONG){
    if((USART0_DESC.usart_base->US_CSR & US_RXRDY) !=0){
    inbuf[0] = USART0_DESC.usart_base->US_RHR;
    if ((IsNull == TRUE)&&(inbuf[0] == ACK)) {
    IsNull = FALSE;
    nBetweenTime = 0;
    }
    else if ((IsNull == FALSE)&&(inbuf[0] == YSTART)) return 0;
    else if (IsNull == FALSE) IsNull = TRUE;
    }
    }
    }
    return ERROR_INVALIDECHO;
    }
      

  2.   

    int SendEndFrame(void)
    {
    u_char SendShortBuffer[10]={0},SendBuffer[150]={0},inbuf[10],crc1,crc2;
    int bToSendEOT,bToEnd,bToSendNull;
    int nRepeat,IsNull; SendShortBuffer[0] = EOT;
    bToSendEOT = FALSE;
    bToSendNull = FALSE;
    IsNull = TRUE;

    for (nRepeat = 0; nRepeat <= 3; nRepeat++) {
    if (nRepeat == 3){
    return ERROR_THREETIMES;
    }
        at91_usart_write(&USART0_DESC,SendShortBuffer[0]);//,SendShortBuffer,1,&dwBytes,&ovlpWrite);
    nBetweenTime = 0;
    while(nBetweenTime <= WAITTING_LONG){
    if((USART0_DESC.usart_base->US_CSR & US_RXRDY) !=0){
    inbuf[0] = USART0_DESC.usart_base->US_RHR;
    if (inbuf[0] == NAK){
    bToSendEOT == TRUE;
    break;
    }
    if ((IsNull == TRUE)&&(inbuf[0] == ACK)) {
    IsNull = FALSE;
    nBetweenTime = 0;
    }
    else if ((IsNull == FALSE)&&(inbuf[0] == YSTART)){
    bToSendNull = TRUE;
    break;
    }
    else if (IsNull == FALSE) IsNull = TRUE;
    }
    }
    if (bToSendEOT == TRUE) continue;
    else if (bToSendNull == TRUE) break;   //to send the nullframe
    else return ERROR_WAITTINGOUT;
    }

    //get the ack c SendBuffer[0] = SOH;
    SendBuffer[1] = 0;
    SendBuffer[2] = 255; checkCRCYModem(SendBuffer+3,128,&crc1,&crc2);
    SendBuffer[131]=crc1;
    SendBuffer[132]=crc2; bToSendNull = FALSE;
    bToEnd = FALSE;
    for (nRepeat = 0; nRepeat <= 3; nRepeat++) {
    if (nRepeat == 3){
    return ERROR_THREETIMES;
    }
    at91_usart_send_frame(&USART0_DESC,SendBuffer,133);
    IsNull = TRUE;
    nBetweenTime = 0;
    while(nBetweenTime <= WAITTING_LONG){
    if((USART0_DESC.usart_base->US_CSR & US_RXRDY) !=0){
    inbuf[0]=USART0_DESC.usart_base->US_RHR;
    if (inbuf[0] == ACK){
    bToEnd = TRUE;
    break;
    }
    else if (inbuf[0] == NAK){
    bToSendNull = TRUE;
    break;
    }
    }
    }
    if (bToEnd == TRUE) break;   //to send the nullframe
    else if (bToSendNull == TRUE) continue;
    else return ERROR_WAITTINGOUT;
    }
    return 0;
    }
    int SendYModem(u_char *  sendFileName,int waitSec)
    {
        u_char cPage,SendBuffer[1500],instr[10],crc1,crc2;
    int nReadCount,nDataLength,nFrameLength,nReturnError;
    int bWillEnd, retryTime, nRepeat, i;
    int bRet;
    long nSendFileLen;
    u_char * pFile;

    bWillEnd=FALSE;

    if((waitSec < 1) || (waitSec > 30))
    return ERROR_PARAMETERS;
    pFile = receivefile; nSendFileLen = strlen((const char*)pFile);
    if (nSendFileLen > 1024*1024){
    return ERROR_LARGEFILE;
    } //file too large

    if (nSendFileLen == 0L) {
    return ERROR_NODATA;
    } //no data in file else if (nSendFileLen == -1L){
    return ERROR_READFILE;
    } //no data in file

    instr[0] = 0;
    if (!GetReceiverResponse(instr,waitSec)){
    return ERROR_WAITTINGOUT;
    }
    else if (instr[0] != YSTART){
    retryTime=0;
    while(retryTime<3)
    {
    if (!GetReceiverResponse(instr,5)){
    return ERROR_WAITTINGOUT;
    }
    else if(instr[0]==YSTART) break;
    retryTime++;
    }
    if(retryTime>=3){
    return ERROR_REPLY;
    }
    } nReturnError = SendFileNameFrame((u_char*)sendFileName); if (nReturnError != 0){
    return nReturnError;
    } cPage = 1; //first page
    pFile = receivefile; while(TRUE){
    nDataLength = 1024;
    for(i = 0; i <= 1028; i++){ 
    SendBuffer[i] = 0x1a; //filled with 0x1a
    }
    if (nSendFileLen >= 1024){
    for(i = 0; i<1024; i++){
    SendBuffer[i+3] = *pFile++;
    }
    nSendFileLen -= 1024;
    SendBuffer[0] = STX;
    bWillEnd = FALSE;
    }
    else if (nSendFileLen >= 128){
    for(i = 0; i<128; i++){
    SendBuffer[i+3] = *pFile++;
    }
    nSendFileLen -= 128;
    nDataLength = 128;
    SendBuffer[0] = SOH;
    bWillEnd = FALSE;
    }
    else {
    for (i =0; i < nSendFileLen; i++){
    SendBuffer[i+3] = *pFile++;
    }
    nDataLength = 128;
    SendBuffer[0] = SOH;
    bWillEnd = TRUE;
    }  
    nFrameLength = nDataLength + 5;
    SendBuffer[1] = cPage;
    SendBuffer[2] = ~cPage;

    checkCRCYModem(SendBuffer+3,nDataLength,&crc1,&crc2);
    SendBuffer[nFrameLength - 2] = crc1;
    SendBuffer[nFrameLength - 1] = crc2;

    for(nRepeat = 0; nRepeat <= 2; nRepeat++){
    // delay();//Sleep(5);

    at91_usart_send_frame(&USART0_DESC,SendBuffer,nFrameLength);
    instr[0] = 0;
    bRet = GetReceiverResponse(instr,WAITTING_LONG);
    if (bRet == FALSE){
    return ERROR_REPLY;
    }
    if (instr[0] == ACK){
    break; //continue to send next page
    }
    else if (instr[0] == NAK) {
    if (nRepeat == 2){
    return ERROR_THREETIMES;
    }
    }
    else continue;
    }
    if(bWillEnd == TRUE){ 
    nReturnError = SendEndFrame();
        if (nReturnError != 0){
    return nReturnError;
    }
    pFile = receivefile;
    for(i = 0; i<7000; i++){
    *pFile++ = 0;
    }

    return 0;
    }
    else{
    cPage++;
    }//next page
    }//while(1)
    }
    #endif