在运行minicom之前,UART资源暂时不可用

时间:2015-05-18 15:47:42

标签: c serial-port uart

我在Linux上遇到UART问题。这是我的计划:

void open_IMU_UART_connection() {
	/* Description:
	 * This function opens the UART communication channel for the Razor IMU
	 */
	printf("Opening Razor IMU UART connection... ");
	write_to_file_custom(everything_log,"Opening Razor IMU UART connection... ",error_log);
	if ((RAZOR_UART=open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY))<0) {
		sprintf(ERROR_MESSAGE,"CRITICAL ERROR: Failed to open the Razor IMU connection on /dev/ttyUSB0");
		write_to_file_custom(everything_log,ERROR_MESSAGE,error_log);
		perror(ERROR_MESSAGE); fflush(stdout);
		exit(-2); // Exit with a critical failure
	}
	printf("opened.\n"); fflush(stdout);
}

void UART_config() {
	/* Description:
	 * This function sets up UART connection properties for the Razor IMU UART connection
	 */
	printf("Setting up Razor IMU UART connection properties... ");
	write_to_file_custom(everything_log,"Setting up Razor IMU UART connection properties... ",error_log);
	tcgetattr(RAZOR_UART,&UART_options);
	UART_options.c_cflag = B57600 | CS8 | CREAD | CLOCAL;
	UART_options.c_iflag = IGNPAR | ICRNL;
	tcflush(RAZOR_UART, TCIFLUSH);
	tcsetattr(RAZOR_UART,TCSANOW,&UART_options);
	printf("setup.\nWaiting for 5 seconds... "); fflush(stdout);
	write_to_file_custom(everything_log,"setup.\nWaiting for 5 seconds... ",error_log);
	usleep(5000000); // Sleep 5 seconds to be sure that the IMU UART connection has time to be configured
	printf("finished waiting!\n\nType [Calibrate] to begin calibrating IMU: ");
	write_to_file_custom(everything_log,"finished waiting!\n\nType [Calibrate] to begin calibrating IMU: ",error_log);
	Treat_reply("Calibrate");
}

void UART_IMU_write(FILE *error_log) {
	/* Description:
	 * This funtion writes #f to the Razor IMU, which tells the on-board Atmel microcontroller to send back to the Raspberry Pi
	 * the current Euler angles (an array of 12 bytes, which has 4 bytes for Pitch, 4 bytes for Yaw, 4 bytes for Roll in IEEE754
	 * float representation, backwards)
	 */
	if((byte_count=write(RAZOR_UART,IMU_TX,3))<0) { // Send Razor IMU command that "I want to know Euler angles"
		sprintf(ERROR_MESSAGE,"Failed to write to the Razor IMU UART connection.\n");
		write_to_file_custom(everything_log,ERROR_MESSAGE,error_log);
		perror(ERROR_MESSAGE); fflush(stdout);
		write_to_file_custom(error_log,ERROR_MESSAGE,error_log);
		exit(-2); // Exit with a critical failure
	}
}

void UART_IMU_receive(FILE *error_log) {
	/* Descrpition:
	 * This function loops until all 12 bytes have been received from the Razor IMU in response to the #f call by (UART_IMU_write)
	 * function.
	 */
	do {
		if((byte_count=read(RAZOR_UART,IMU_RX,MAX_BUFFER))<0) {
			sprintf(ERROR_MESSAGE,"Failed to read from the Razor IMU UART.\n");
			write_to_file_custom(everything_log,ERROR_MESSAGE,error_log);
			perror(ERROR_MESSAGE); fflush(stdout);
			write_to_file_custom(error_log,ERROR_MESSAGE,error_log);
			exit(-2); // Exit with a critical failure
		} else {
			memcpy(&read_string[ii_start],IMU_RX,byte_count*sizeof(unsigned char));
			ii_start=ii_start+byte_count;

			memset(IMU_RX,0,MAX_BUFFER);
			if (ii_start==12) {
				continue_collecting=0; // All has been read, exit reading loop
				//printf("%d : %s\n",ii_start,read_string);
			}
		}
	} while(continue_collecting);
}

int main() {
     open_IMU_UART_connection();
     UART_config();
     UART_IMU_write(error_log);
	 UART_IMU_receive(error_log);
}

我的问题:当我第一次连接串口设备后运行程序时,出现“资源暂时不可用”错误。但是,当我运行“sudo minicom ...”时,我可以与设备通信;然后,当我关闭minicom后重新运行程序时,我的程序正常工作!这可能是什么问题?谢谢!

1 个答案:

答案 0 :(得分:0)

我和Ubuntu有类似的问题。 ttyUSB偶尔会丢失他们的对等连接。在NetworkManager中停用宽带网络条目(GSM / UMTS-stick;这些模拟串行端口上的经典调制解调器)之后,问题就消失了。我怀疑其中一些进程试图自动检测BB调制解调器并干扰正常使用。

由于NetworkManager与其他发行版一样常见,您可能会看一下。