c语言串口通信编程_串口编程语言

c语言串口通信编程

介绍 (Introduction)

尽管这是一项旧技术,但许多硬件制造商仍在使用串行端口。

If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to work on many clinical instruments as a job requirement to parse, extract patient test results and send them to a database backend for reporting. This means there are many serial ports attached to the computer.  I developed different C# applications for each instrument. However, whenever I needed to make any changes, I had to open source code of the application, compile and deploy over and over again. It was very time consuming and a headache.

如果您使用C#开发应用程序,则Microsoft .NET框架具有Seri​​alPort类以与串行端口进行通信。 作为一项工作要求,我需要处理许多临床仪器,以解析,提取患者测试结果并将其发送到数据库后端进行报告。 这意味着有许多串行端口连接到计算机。 我为每种仪器开发了不同的C#应用​​程序。 但是,每当需要进行任何更改时,都必须打开应用程序的源代码,反复编译和部署。 这非常耗时并且令人头疼。

This is why I developed a serial port programming language:  To reduce development time.  I have made the tool available as project on SourceForge.  In this article, I'll discuss some of its features and give you an idea of how to use it.

这就是为什么我开发串行端口编程语言的原因:减少开发时间。 我已经将该工具作为SourceForge上的项目提供。 在本文中,我将讨论其某些功能,并为您提供如何使用它的想法。

应用功能
(Application Features
)

这是一个方便,简单的应用程序,专门设计用于简化基于RS232的应用程序的开发。 它不仅使与串行端口通信变得容易,而且使数据解析和提取变得容易。

It requires .NET Framework 3.5 or above and supports all major Windows platforms including Windows XP, Windows Vista and Windows 7.

它需要.NET Framework 3.5或更高版本,并支持所有主要的Windows平台,包括Windows XP,Windows Vista和Windows 7。

Key Features

主要特点

Able to log multiple serial ports at the same time. The data logger has the capability to log multiple ports simultaneously so that multiple external serial devices can be logged.

能够同时记录多个串行端口。 数据记录器具有同时记录多个端口的功能,因此可以记录多个外部串行设备。

Integrated Development Environment (IDE). You do NOT have to install any other development tools.

集成开发环境(IDE)。 您不必安装任何其他开发工具。

Built-in debugger makes it easy to find and fix errors. It saves a lot of time.

内置调试器使查找和修复错误变得容易。 这样可以节省大量时间。

Regular expression support for easy data extraction.

正则表达式支持可轻松提取数据。

入门
(Getting Started
)

应用程序有两个主窗口。

1- Main Application Window

1-主应用程序窗口

Starts/stops the application specific driver to communicate with the serial port and configures the driver.
启动/停止特定于应用程序的驱动程序以与串行端口通信并配置驱动程序。
main.png main.png
The Driver works similar to a state machine. Every state runs continually until it is jumped to another state or the driver stops.
驱动程序的工作方式类似于状态机。 每个状态都会持续运行,直到跳转到另一个状态或驱动程序停止为止。

2- IDE

2- IDE

The application has over 50 different basic commands which make it easy not only to communicate with the serial port but also to perform data parsing and extraction. These commands are written in this editor and compiled into a binary file.
该应用程序具有50多种不同的基本命令,这些命令不仅使与串行端口通信变得容易,而且使执行数据解析和提取变得容易。 这些命令在此编辑器中编写,并编译成二进制文件。
ide-main.png ide-main.png
Every driver must have an initialization state called "Init". For example;
每个驱动程序必须具有一个称为“ Init”的初始化状态。 例如;
state Init
  // commands here
end state
ASCII Control characters (aka non-printing characters, 0x00-0x1F and 0x7F ) are predefined in the core module as below.
如下所示,在核心模块中预定义了ASCII控制字符(又名非打印字符0x00-0x1F和0x7F)。
Char  Hex   Meaning
====  ====  =======
NUL   0x00  Ctrl-@ | NULL
SOH   0x01  Ctrl-A | START OF HEADING
STX   0x02  Ctrl-B | START OF TEXT
ETX   0x03  Ctrl-C | END OF TEXT
EOT   0x04  Ctrl-D | END OF TRANSMISSION
ENQ   0x05  Ctrl-E | ENQUIRY
ACK   0x06  Ctrl-F | ACKNOWLEDGE
BEL   0x07  Ctrl-G | BELL
BS    0x08  Ctrl-H | BACKSPACE
HT    0x09  Ctrl-I | HORIZONTAL TABULATION
LF    0x0A  Ctrl-J | LINE FEED
VT    0x0B  Ctrl-K | VERTICAL TABULATION
FF    0x0C  Ctrl-L | FORM FEED
CR    0x0D  Ctrl-M | CARRIAGE RETURN
SO    0x0E  Ctrl-N | SHIFT OUT
SI    0x0F  Ctrl-O | SHIFT IN
DLE   0x10  Ctrl-P | DATA LINK ESCAPE
DC1   0x11  Ctrl-Q | DEVICE CONTROL 1
DC2   0x12  Ctrl-R | DEVICE CONTROL 2
DC3   0x13  Ctrl-S | DEVICE CONTROL 3
DC4   0x14  Ctrl-T | DEVICE CONTROL 4
NAK   0x15  Ctrl-U | NEGATIVE ACKNOWLEDGE
SYN   0x16  Ctrl-V | SYNCHRONOUS IDLE
ETB   0x17  Ctrl-W | END OF TRANSMISSION BLOCK
CAN   0x18  Ctrl-X | CANCEL
EM    0x19  Ctrl-Y | END OF MEDIUM
SUB   0x1A  Ctrl-Z | SUBSTITUTE
ESC   0x1B  Ctrl-[ | ESCAPE
FS    0x1C  Ctrl-\ | FILE SEPARATOR
GS    0x1D  Ctrl-] | GROUP SEPARATOR
RS    0x1E  Ctrl-^ | RECORD SEPARATOR
US    0x1F  Ctrl-_ | UNIT SEPARATOR
DEL   0x7F  Ctrl-? | DELETE

示例-1
(Example - 1
)

两个串行端口命令(recv和send)将允许您与串行端口进行交互。

For example; if you want to send an ACK to the serial port;

例如; 如果要向串行端口发送ACK;

state Init
  send("");
end state

NOTE: Control characters must be enclosed with prefix "<" and suffix ">"

注意:控制字符必须用前缀“ <”和后缀“>”括起来

示例-2
(Example - 2
)

由于字节可能随时进入,因此缓冲传入的数据至关重要。 解决这个问题;

   1. Buffer the incoming data.

1.缓冲传入的数据。

   2. Scan your buffer to find complete data.

2.扫描缓冲区以查找完整的数据。

   3. remove the used data from the buffer.

3.从缓冲区中删除使用的数据。

The application program has a solution in order to buffer the incoming data.

应用程序具有解决方案,以便缓冲传入的数据。

state Init
  // define a global variable
  our $BUFFER = "";
  jump(Receive);
end state

state Receive
  recv();
  $len = length($DATA_PACKET);
  if("$len > 0") {
     
    // 1. buffer the incoming data
    $BUFFER += $DATA_PACKET;
    call(Parser);
  }
end state

state Parser
  // 2. scan your buffer to find complete data
  // command "match" will check if buffer matches a regular expression
  if(match($BUFFER, "(?.*?(?.*?)(?[0-9A-F]{2}))")) {
     
    // Received complete data
    // 3. remove the used data from the buffer
    $lenData = length($WILLDELETE);
    $BUFFER = remove($BUFFER, 0, $lenData);

    // Do operations with the other parsed fields. $DATA and $CHECKSUM in this example.
  }
end state

For more information, please visit the project homepage or the SourceForge project page.   The User Guide documentation is here.

有关更多信息,请访问项目主页或SourceForge项目页面。 用户指南文档在此处 。

翻译自: https://www.experts-exchange.com/articles/4023/Serial-Port-Programming-Language.html

c语言串口通信编程

你可能感兴趣的:(编程语言,java,linux,python,大数据)