前馈科技

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 852|回复: 0

Windows下如何不通过串口调试助手进行串口发送

[复制链接]

97

主题

97

帖子

539

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
539
发表于 2020-8-3 10:34:43 | 显示全部楼层 |阅读模式
        最近使用的一款处理器有点残疾,直接通过DSU和处理器建立连接会失败!100%!解决办法是连接前通过DSU向处理器发送一帧数据,之后再通过DSU和处理器建立连接。常规做法是先通过串口调试助手向DSU发数,收到回应后关闭串口调试助手,再通过DSU建立连接。遗憾的是这个处理器调试过程中很容易死机,而且死机之后必须要进行复位才能重新通过DSU建立连接。这就有点麻烦了,每次都需要关闭DSU—>打开串口调试助手—>发送数据帧—>关闭串口—>启动DSU接连连接。。。极其繁琐。能不能写段代码,在启动DSU时先自动执行这段代码向DSU发送数据帧呢?下面开启折腾旅程。
       由于连接DSU的grmon是编译好的工具,我们无法修改,所以首先考虑能否采用Windows下的批处理程序实现该功能。需要向DSU发送的数据帧为:0x55 0x55 0x55 0x55 0x55 0x55 0x80 0x81 0x00 0x00 0x00,现学现用写了一段代码:

  1. mode com4:115200,N,8,1
  2. set var=0x55 0x55 0x55 0x55 0x55 0x55 0x80 0x81 0x00 0x00 0x00
  3. for %%a in (%var%) do (
  4. echo %%a>com4
  5. )
  6. pause
复制代码

      用串口调试助手进行接收测试,16进制显示,收到的数据帧竟然如下:
35 35 20 35 35 20 35 35 20 35 35 20 35 35 20 35 35 20 38 30 20 38 31 20 30 30 20 30 30 20 30 30

      显然,16进制的数据帧0x55 0x55 0x55 0x55 0x55 0x55 0x80 0x81 0x00 0x00 0x00被当做字符串发送了!于是问题归结为如何不采用串口调试助手发送16进制数据帧?由于本次待发送的16进制数据帧是固定的,所以首先想到的是将16进制数据帧转换为ASCII码字符串,然后通过上述批处理代码发送字符串。
      尝试的结果让人很沮丧,因为16进制书0x81和0x00不在ASCII码表中,0x55 0x55 0x55 0x55 0x55 0x55 0x80 0x81 0x00 0x00 0x00转换的结果为UUUUUU€?,通过以上代码发送出去后收到的是0x55 0x55 0x55 0x55 0x55 0x55 0x80,反复折腾,最终得出一个结论:我无法用批处理程序实现16进制数发送!
      放弃那一切幻想,下面老老实用最原始的办法,通过C语言写一个数据帧发送的控制台应用程序,然后通过批处理程序先执行串口发送程序,再执行grmon连接DSU,最终间接实现一建连接DSU。
      使用熟悉的C就容易多了,下面直接上代码:
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include<iostream>
  4. #include<windows.h>
  5. #include<time.h>
  6. #include<stdlib.h>
  7. using namespace std;

  8. int com_tx(char *comport)
  9. {
  10.             int i = 0;
  11.         HANDLE hcom;      
  12.         hcom = CreateFile((comport), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);               

  13.         if (hcom == INVALID_HANDLE_VALUE)
  14.         {
  15.             fprintf(stderr, "串口打开失败\n");
  16.             exit(0);
  17.         }
  18.                 else
  19.                 {
  20.                         printf("串口打开成功\n");
  21.                 }
  22.         SetupComm(hcom, 100, 100);
  23.         DCB dcb;
  24.         GetCommState(hcom, &dcb);
  25.         dcb.BaudRate = 115200;
  26.         dcb.ByteSize = 8;
  27.         dcb.Parity = NOPARITY;
  28.         dcb.StopBits = 1;
  29.         SetCommState(hcom, &dcb);
  30.                 if (hcom)                // check if the port is opened
  31.                 {
  32.                         PurgeComm(hcom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
  33.                 }
  34.                 char read[4] = {0,0,0,0};
  35.                 DWORD dwReadLen = 0;
  36.         
  37.         char data[] = {0x55,0x55,0x55,0x55,0x55,0x55,0x80,0x81,0x00,0x00,0x00};
  38.                 DWORD dwWriteLen = 0;
  39.                 DWORD dwErrorFlags;
  40.                 COMSTAT ComStat;
  41.                 ClearCommError(hcom,&dwErrorFlags,&ComStat);
  42.                 // Clear buffer
  43.                 PurgeComm(hcom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
  44.                 Sleep(1000);
  45.         if (!WriteFile(hcom, data, 11, &dwWriteLen, NULL))
  46.         {
  47.           fprintf(stderr, "发送失败\n");
  48.         }
  49.                 else
  50.                 {
  51.                         printf("·发送成功\n");
  52.                 }
  53.                 Sleep(500);
  54.                 dwReadLen = 0;
  55.                 ReadFile(hcom, read, 4, &dwReadLen, NULL);
  56.                 for(i=0;i<dwReadLen;i++)
  57.                 {
  58.                         printf("%02X ",0xFF & read[i]);
  59.                 }
  60.                 printf("\n");

  61.         return 0;   
  62. }


  63. int main(int argc, char* argv[])
  64. {
  65.         char *com;
  66.   
  67.   if(argc==2)
  68.           {
  69.                   com = argv[1];
  70.           }
  71.   else
  72.           {
  73.                   printf("miss com options format\n");
  74.                   exit(0);
  75.           }
  76.   com_tx(com);

  77.         return 0;
  78. }
复制代码
     最终写一段批处理程序,并保存为.bat格式:
  1. cd /d %~dp0
  2. com com2
  3. echo batch cmd.bat | grmon -leon2 -ni -uart com2
复制代码
       双击.bat文件,cd /d %~dp0进入.bat所在目录,com com2向com2发送数据帧,echo batch cmd.bat | grmon -leon2 -ni -uart com2通过com2连接DSU,一键连接DSU终于完整实现。
        由于采用C语言实现,串口发送代码有点长,哪位大神知道如何使用批处理程序实现16进制数串口发送还请回帖告诉我啊!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|计算机控制

GMT+8, 2024-4-25 04:04 , Processed in 0.048722 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表