c - 运行 minicom 之前 UART 资源暂时不可用

标签 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 后重新运行该程序时,我的程序就可以运行了!这里可能有什么问题?谢谢!

最佳答案

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

由于 NetworkManager 在其他发行版中也很常见,因此您可以看看这个。

关于c - 运行 minicom 之前 UART 资源暂时不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307612/

相关文章:

c - STM32 UART中断溢出后不工作

c++ - VlFeat kdtree设置和查询

c - 使用 malloc 和 realloc 动态地将字符串添加到 char 指针

c# - 在 C# 中将大数转换为两个字节

c# - .NET 中的 SerialPort 是非托管资源吗?我的包装类是否正确?

Linux用户空间: how to put 16550-compatible UART in loopback mode persistently

c - Atmega32A 与 PC 上的 UART 通信

c - 确定汇编中函数的返回类型

Android NDK 未找到实现

java - 卡住了从 Arduino 以字节为单位获取图像,并使用 Java 将它们恢复为图像