Linux下的串口通讯调试工具,程序代码示例

嵌入式领域,串口通讯是使用非常多的一种通讯协议了,所以一块串口通讯调试工具自然必不可少。

这里主要是针对的嵌入式Linux系统,ubuntu之类的网上有很多,就不需要看下面的了。

环境:Linux + c/c++

说明:启动后,选择read或者write模式,整个通讯是基于16进制解析的,不支持其他的哦。

write效果如下图:

read效果如下图:

核心代码:

if(read_write == "read" || read_write == "r")
		{
			while(1)
			{
				nread = read(fd, recvbuf, RECVBUF_SIZE);
				if(nread > 0)
				{
					printf("recv data %d byte(s): ", nread);
					for(i=0; i<nread; i++) 
                    {
						printf("%02x ", recvbuf[i]);
					}
					printf("\n");
				}
			}
		}
	
		if(read_write == "write" || read_write == "w")
		{
			while (1)
			{
				printf("input you data:");
				getline(cin,write_date);
				stringstream ss_write(write_date);
				int num_write = 0;
				unsigned char temp_char_write;
				string temp_string_write;
				while (getline(ss_write, temp_string_write, ' '))
				{
					temp_char_write = strtol(temp_string_write.c_str(),NULL,16);					
					sendbuf[num_write] = temp_char_write;
					// printf("%02x, ", sendbuf[num_write]);
					num_write++;
				}		
				write(fd, sendbuf, num_write);
			}
		}

需要完整代码的童鞋请留言邮箱。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章