我正在C中从头开始在Raspberry Pi上构建气象站。我有一个包含用于获取温度的代码的文件,并且我还有另一个文件来调用这些功能。 我之前已经写过代码,但是代码的质量很差,我想重新编写更好的代码。
我认为我的问题是了解如何传递对指向传感器的文件描述符的引用。
请考虑我的软件中的此代码段。 (我确信此部分中存在错误。)
#include "readTemp.h"
#include <stdio.h>
int main() {
int fd;
fd = initGPIO(0,0x76);
getTemp(fd);
return(0);
}
和文件readTemp.C
int * initGPIO(int i2cBus,int i2cAddr){
int *addr = malloc(sizeof(int));
int fd;
//Initialise GPIO
if(gpioInitialise() < 0) {
perror("Initialisation failed\n");
exit(EXIT_FAILURE);
}
//Open the I2C bus
fd = i2cOpen(i2cBus,i2cAddr,0);
if(fd < 0) {
perror("Device failed to open\n");
exit(EXIT_FAILURE);
}
//Send Reset
if(i2cWriteByte(fd,CMD_RESET) != 0){
perror("Error sending reset---\n");
}
addr = &fd;
printf("3-%d\n",&addr);
return(addr);
}
/*
* Reads the calibration data from the PROMs on the sensor
* Parameters - fd - File Descriptor of the i2c device
* *proms - Pointer to an array of 8 unsigned 16-bit integers
*/
static int getcalibdata(int *fd,u_int16_t *proms) {
const int bytesToRead = 2;
char buf[2] = {0};
printf("2-%d\n",&fd);
// Populate for each prom (7)
for(int i = PROM_START; i < PROM_STOP; i = i + 2){
// Write PROM read commands
if(i2cWriteByte(*fd,i) != 0) {
perror("Error writing PROM command!!\n");
exit(EXIT_FAILURE);
}
// Read result from PROM
if(i2cReadDevice(*fd,buf,bytesToRead) <= 0) {
perror("Error reading from PROM\n");
exit(EXIT_FAILURE);
}
// Store result in array
proms[(i - PROM_START) / 2] = (buf[0] << 8) | (buf[1]);
}
return(0);
}
int getTemp(int *fd){
u_int16_t proms[8];
u_int32_t rawTemp = getRawTemp(fd);
printf("%d\n",rawTemp);
getcalibdata(fd,proms);
for(int i = 0; i < 8; i++){
printf("%d-%d\n",i,proms[i]);
}
int temp = calcTemp(rawTemp,proms);
printf("---%d\n",temp);
return 0;
}
代码可以编译,但是在运行时出现"Error writing PROM Command"
错误。
我认为我的错误可能出在代码的一小部分,其中我将initGPIO()
的结果分配给int
,而该结果应该是指针。但是,当我这样做时,我的代码段就会出错。