#ifndef RS485_SAMPLE_SERIAL
#define RS485_SAMPLE_SERIAL "uart2"
#endif
#ifndef RS485_SAMPLE_BAUDRATE
#define RS485_SAMPLE_BAUDRATE 9600
#endif
#ifndef RS485_SAMPLE_PARITY
#define RS485_SAMPLE_PARITY 0
#endif
#ifndef RS485_SAMPLE_PIN
#define RS485_SAMPLE_PIN GET_PIN(D,7)
#endif
#ifndef RS485_SAMPLE_LVL
#define RS485_SAMPLE_LVL 1
#endif
rt_thread_t tid;
void rs_485()
{
const char send_cmd[] = "hello thread
";
rs485_inst_t * hinst = rs485_create(RS485_SAMPLE_SERIAL, RS485_SAMPLE_BAUDRATE,
RS485_SAMPLE_PARITY, RS485_SAMPLE_PIN, RS485_SAMPLE_LVL);
if(hinst != RT_NULL)
{
rs485_set_recv_tmo(hinst, 1000);
if (rs485_connect(hinst) != RT_EOK)
{
rs485_destory(hinst);
LOG_E("rs485 connect fail.");
return;
}
}
while(1)
{
int len = strlen(send_cmd);
len = rs485_send(hinst, &send_cmd,sizeof(send_cmd));
if(len<0)
{
LOG_E("rs485 send datas error.");
break;
}
if(len == 0)
{
LOG_D("rs485 send timeout.");
continue;
}
}
rs485_destory(hinst);
LOG_D("rs485 test end.");
}
int main()
{
tid = rt_thread_create ( "thread1",rs_485,RT_NULL,128,20,10);
if(tid == RT_NULL)
{
return RT_ERROR;
}
else
{
rt_thread_startup(tid);
LOG_I("rs485 sample master thread startup...");
return RT_EOK;
}
}
INIT_APP_EXPORT(main);
这个代码是按照下载的软件包写的,目前程序是死在rs485_create函数里面的rt_device_find函数内,显示找不到串口2设备,但是串口有在board.h文件里面进行相应定义[img]https://oss-club.rt-thread.org/uploads/20220412/3e6a0792a44263aaebbe5ba87b645fb6.png[/img]
之前有成功的时候,也是这个代码,成功获取到串口设备之后,485会一直向外发送数据,然后发送的数据有的时候是错乱的,希望有人可以帮忙解惑一下,谢谢。
#ifndef RS485_SAMPLE_SERIAL
#define RS485_SAMPLE_SERIAL "uart2"
#endif
#ifndef RS485_SAMPLE_BAUDRATE
#define RS485_SAMPLE_BAUDRATE 9600
#endif
#ifndef RS485_SAMPLE_PARITY
#define RS485_SAMPLE_PARITY 0
#endif
#ifndef RS485_SAMPLE_PIN
#define RS485_SAMPLE_PIN GET_PIN(D,7)
#endif
#ifndef RS485_SAMPLE_LVL
#define RS485_SAMPLE_LVL 1
#endif
rt_thread_t tid;
void rs_485()
{
const char send_cmd[] = "hello thread
";
rs485_inst_t * hinst = rs485_create(RS485_SAMPLE_SERIAL, RS485_SAMPLE_BAUDRATE,
RS485_SAMPLE_PARITY, RS485_SAMPLE_PIN, RS485_SAMPLE_LVL);
if(hinst != RT_NULL)
{
rs485_set_recv_tmo(hinst, 1000);
if (rs485_connect(hinst) != RT_EOK)
{
rs485_destory(hinst);
LOG_E("rs485 connect fail.");
return;
}
}
while(1)
{
int len = strlen(send_cmd);
len = rs485_send(hinst, &send_cmd,sizeof(send_cmd));
if(len<0)
{
LOG_E("rs485 send datas error.");
break;
}
if(len == 0)
{
LOG_D("rs485 send timeout.");
continue;
}
}
rs485_destory(hinst);
LOG_D("rs485 test end.");
}
int main()
{
tid = rt_thread_create ( "thread1",rs_485,RT_NULL,128,20,10);
if(tid == RT_NULL)
{
return RT_ERROR;
}
else
{
rt_thread_startup(tid);
LOG_I("rs485 sample master thread startup...");
return RT_EOK;
}
}
INIT_APP_EXPORT(main);
这个代码是按照下载的软件包写的,目前程序是死在rs485_create函数里面的rt_device_find函数内,显示找不到串口2设备,但是串口有在board.h文件里面进行相应定义[img]https://oss-club.rt-thread.org/uploads/20220412/3e6a0792a44263aaebbe5ba87b645fb6.png[/img]
之前有成功的时候,也是这个代码,成功获取到串口设备之后,485会一直向外发送数据,然后发送的数据有的时候是错乱的,希望有人可以帮忙解惑一下,谢谢。
举报