awtk开发工具封装了串口,可以方便的异步调用,就做个程序试一下吧
在deepinlinux20.9版本调试通过,开始第一步先给系统增加usb串口线驱动
https://download.csdn.net/download/qiaozhangchi/87463972
串口控件ide里没有,需要自己手动到界面文件里补充上即可
<serial name="serial"/>
刚开始发现串口消息回显到终端比较慢,差点要放弃的时候把结果往界面控件显示了一下,发现很快,还是值得用。
只要界面初始化控件时把接收函数回调注册一下,就可以在发送消息后回调函数接到反馈了,考虑到分包问题,所以回调函数里最好不要直接解析数据,我这是到另一个控件事件里接收数据,并追加到全局缓存,然后解析数据
serial_widget_t* serial ;
if (serial == NULL)serial= SERIAL_WIDGET(widget_lookup(win, "serial", TRUE));
widget_child_on(win, "serial", EVT_DATA, on_data, win);
// receive data
static ret_t on_data(void* ctx, event_t* e) {
char recdata[128]={"sendmsg"};
printf("data:%s\n", recdata);
widget_set_text_utf8(msg, recdata);
return RET_OK;
}
// serialname: /dev/ttyUSB0
static ret_t on_btnopen_click(void* ctx, event_t* e) {
char buf[128]={0};
widget_get_text_utf8(edit, buf, sizeof(buf)-1);
widget_t* serial1 = widget_lookup(WIDGET(ctx), "serial", TRUE);
serial_widget_set_device(serial1, buf);
return RET_OK;
}
static ret_t on_btnsend_click(void* ctx, event_t* e) {
log_debug("send......");
char text[] = {0x00, 0x00, 0xff, 0x00, 0x04, 0x00, 0x04, 0xff, 0x00, 0x05, 0xff, 0x03, 0x00};
int len=sizeof(text) / sizeof(text[0]);
printf("text len=%d strlen=%ld\n",len, strlen(text));
for(int i=0;i<len;i++){
printf(" %d",text[i]);
}
printf(" \n");
tk_ostream_write_len(serial->ostream, text, len,500);
memset(g_data,'\0',sizeof(g_data)/sizeof(char));
dataindex=0;
return RET_OK;
}
我这里得到串口数据事件通知后,我再设置文本框值,在文本框改变值事件里再去接收数据
void stringToHex(const char* str, char* hexStr,int len,int index) {
str+=index;
for (int k=0;k<len;k++) {
sprintf(hexStr+3*k, "%02X ", (unsigned char)*str);
str++;
}
}
void outstr(const char* str, int len) {
for (int k=0;k<len;k++) {
printf("%02X ", (unsigned char)*str);
str++;
}
}
static ret_t on_msg_value_changed(void* ctx, event_t* e) {
widget_t* target = WIDGET(e->target);
if (e->type == EVT_VALUE_CHANGED) {
char buf[256]={0};
widget_get_text_utf8(target, buf, sizeof(buf)-1);
printf("data = %s\n", buf);
if (tk_str_eq(buf, "sendmsg")) {
memset(g_receive,0,sizeof(g_receive));
int32_t len = tk_istream_read(serial->istream, g_receive, sizeof(g_receive)-1);
if(len > 0) {
g_receive[len] = '\0';
memcpy(g_data+dataindex, g_receive,len);
dataindex+=len;
outstr(g_data,dataindex);
printf("\nhexStr=%s \n buflen=%d datalen=%d\n",g_data,len,dataindex);
char showbuf[512]={0};
stringToHex(g_receive,showbuf,len,0);
printf("g_receive:%s\n", showbuf);
memset(showbuf,0,sizeof(showbuf));
stringToHex(g_data,showbuf,dataindex,0);
printf("g_data:%s\n", showbuf);
widget_set_text_utf8(mlresult, showbuf);
}
sprintf(buf,"receivemsg:%d",dataindex);
printf("data:%s\n", buf);
widget_set_text_utf8(msg, buf);
}
}
return RET_OK;
}
串口协议非文本字符,所以读到串口消息缓存时不能strcpy,也不能sprintf函数里用%s,最后自己写了一个数组追加函数,后来发现系统函数memcpy可以用,最后stringtohex函数其实可以照memcpy用法优化减少一个参数。
代码还是要多写写,几次调试就能增加某些细节印象,减少眼高手低结果次数。