c语言串口通信编程
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框架具有SerialPort类以与串行端口进行通信。 作为一项工作要求,我需要处理许多临床仪器,以解析,提取患者测试结果并将其发送到数据库后端进行报告。 这意味着有许多串行端口连接到计算机。 我为每种仪器开发了不同的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上的项目提供。 在本文中,我将讨论其某些功能,并为您提供如何使用它的想法。
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.
正则表达式支持可轻松提取数据。
1- Main Application Window
1-主应用程序窗口
2- IDE
2- IDE
state Init
// commands here
end state
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
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 ">"
注意:控制字符必须用前缀“ <”和后缀“>”括起来
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语言串口通信编程