cpu芯片是ar9344,485芯片是sp485re,测试485模块读写,A发送一串数据指令,B接收到指令后,给A反馈一串数据指令,但是在485 引脚悬空的情况下却能收到一串乱码数据。485读写程序如下:
ucHostAddress=(unsigned char)atol(argv[1]);
ucSlaveAddress=(unsigned char)atol(argv[2]);
ucSensorAddress=(unsigned char)atol(argv[3]);
ucRegAddress=(unsigned char)atol(argv[4]);
uiRegNum=(unsigned int)atol(argv[5]);
uiTTLtime=(unsigned int)atol(argv[6]);
if((ucSensorAddress >= 0)&&(ucSensorAddress <= 180))
{
/*generate command package*/
cpCommandPackage[0] = (char)0xA5;
cpCommandPackage[1] = (char)ucHostAddress; //HostAddress
cpCommandPackage[2] = (char)uilength; //data length
cpCommandPackage[3] = (char)ucSlaveAddress; //SlaveAddress
cpCommandPackage[4] = (char)((uiTTLtime >> 24) & 0x00FF); //TTLtime
cpCommandPackage[5] = (char)((uiTTLtime >> 16) & 0x00FF);
cpCommandPackage[6] = (char)((uiTTLtime >> 8) & 0x00FF);
cpCommandPackage[7] = (char)(uiTTLtime & 0x00FF);
cpCommandPackage[8] = (char)ucSensorAddress; //SensorAddress
cpCommandPackage[9] = (char)0x03; //function code
cpCommandPackage[10] = (char)((ucRegAddress >> 8) & 0x00FF); //reg address high byte
cpCommandPackage[11] = (char)(ucRegAddress & 0x00FF); //reg address low byte
cpCommandPackage[12] = (char)((uiRegNum >> 8) & 0x00FF); //read reg number high byte
cpCommandPackage[13] = (char)(uiRegNum & 0x00FF); //read reg number low byte
/*get CRC16*/
wCRC16 = CRC16_2(&cpCommandPackage[8], 6);
cpCommandPackage[14] = (char)(wCRC16 & 0x00FF); //crc16 code low byte
cpCommandPackage[15] = (char)((wCRC16 >> 8) & 0x00FF); //crc16 code high byte
xor=xorCheck((char*)cpCommandPackage, 16);
cpCommandPackage[16] = (char) xor; //xor code
iFile = open("/dev/ttyATH0", O_RDWR | O_NOCTTY | O_NONBLOCK, 0);
if(-1 == iFile )
{
printf("open 485 port err...\n");
return -1;
}
else
{
if((ret=setport(iFile,4800,8,1,'N')) < 0)
{
printf("set port faild!\n");
return -1;
}
len = write(iFile, cpCommandPackage, uilength);
printf("\n** Getsensor -->Tx_commandpackage -- length:%d , sensoraddr:%d , Regaddr:%d.\n", len,(int)ucSensorAddress,(int)ucRegAddress);
printf("[");
for(i = 0;i< len ;i++)
printf("%02x,",cpCommandPackage[i]);
printf("]\n");
printf("\r\n");
if((cpCommandPackage[4] == 0)&&(cpCommandPackage[5] == 0)&&(cpCommandPackage[6] == 0)&&(cpCommandPackage[7] == 0))
{
sleep(2);
}
else
{
usleep(100000);
}
ret = read(iFile, cpAnswerPackage,15+uiRegNum*2);
// cpAnswerPackage[ret] = '\0';
close(iFile);
printf("\n** Getsensor -->Rx_ackpackage --length:%d , Sensoraddr:%d , Regaddr:%d.\n",ret,(int)ucSensorAddress,(int)ucRegAddress);
printf("[");
for(i = 0;i < ret;i++)
printf("%02x,",cpAnswerPackage[i]);
printf("]");
printf("\r\n");
想问问是程序的原因,还是硬件的原因?求解!