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 a lPort类以与串⾏端⼝进⾏通信。 作为⼀项⼯作要求,我需要处理许多临床仪器,以解析,提取患者测试结果并将其发送到数据库后端进⾏报告。 这意味着有许多串⾏端⼝连接到计算机。 我为每种仪器开发了不同的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.
启动/停⽌特定于应⽤程序的驱动程序以与串⾏端⼝通信并配置驱动程序。
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多种不同的基本命令,这些命令不仅使与串⾏端⼝通信变得容易,⽽且使执⾏数据解析和提取变得容易。 这些命令在此编辑器中编写,并编译成⼆进制⽂件。
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("<ACK>");
end state
NOTE: Control characters must be enclosed with prefix "<" and suffix ">" NOTE:
注意:控制字符必须⽤前缀“ <”和后缀“>”括起来
注意:
⽰例-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);
c语言编程入门指南pdfif("$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, "(?<WILLDELETE>.*?<STX>(?<DATA>.*?)<ETX>(?<CHECKSUM>[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 or the page.  The User Guide documentation is .有关更多信息,请访问或页⾯。 ⽤户指南⽂档在 。
c语⾔串⼝通信编程

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。