没有合适的资源?快使用搜索试试~ 我知道了~
C#调用API串口通信C#调用API串口通信C#调用API串口通信C#调用API串口通信C#调用API串口通信C#调用API串口通信
资源推荐
资源详情
资源评论












c#
调
api
串口通
信
分类: C#
在调试 ICU 通信设备的时候,由于串口通信老出现故障,所以就怀疑 CF 实现的
SerialPort 类是否有问题,所以最后决定用纯 API 函数实现串口读写。
先从网上搜索相关代码(关键字:C# API 串口),发现网上相关的资料大约来源于一
个版本,那就是所谓的 msdn 提供的样例代码(msdn 的具体出处,我没有考证),其它的
代码大都是它的变种。
其实这个示例代码是有问题的,也就是说 DCB 结构体声明的有问题,虽然该代码可以
正常通信,不过如果你设置了奇偶校验的话,你会发现奇偶校验无效。
VC 中的 DCB 结构声明如下:
typedef struct _DCB {
DWORD DCBlength; /* sizeof(DCB) */
DWORD BaudRate; /* Baudrate at which running */
DWORD fBinary: 1; /* Binary Mode (skip EOF check) */
DWORD fParity: 1; /* Enable parity checking */
DWORD fOutxCtsFlow:1; /* CTS handshaking on output */
DWORD fOutxDsrFlow:1; /* DSR handshaking on output */
DWORD fDtrControl:2;/* DTR Flow control */
DWORD fDsrSensitivity:1; /* DSR Sensitivity */
DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */
DWORD fOutX: 1; /* Enable output X-ON/X-OFF */
DWORD fInX: 1; /* Enable input X-ON/X-OFF */
DWORD fErrorChar: 1;/* Enable Err Replacement */
DWORD fNull: 1; /* Enable Null stripping */
DWORD fRtsControl:2;/* Rts Flow control */
DWORD fAbortOnError:1; /* Abort all reads and writes on Error */
DWORD fDummy2:17; /* Reserved */
WORD wReserved; /* Not currently used */
WORD XonLim; /* Transmit X-ON threshold */
WORD XoffLim; /* Transmit X-OFF threshold */
BYTE ByteSize; /* Number of bits/byte, 4-8 */
BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */
BYTE StopBits; /* 0,1,2 = 1, 1.5, 2 */
char XonChar; /* Tx and Rx X-ON character */
char XoffChar; /* Tx and Rx X-OFF character */
char ErrorChar; /* Error replacement char */
char EofChar; /* End of Input character */
char EvtChar; /* Received Event character */
WORD wReserved1; /* Fill for now. */
} DCB, *LPDCB;
有问题的代码 DCB 结构声明如下:
[StructLayout(LayoutKind.Sequential)]

public struct DCB
{
public int DCBlength;
public int BaudRate;
public int fBinary;
public int fParity;
public int fOutxCtsFlow;
public int fOutxDsrFlow;
public int fDtrControl;
public int fDsrSensitivity;
public int fTXContinueOnXoff;
public int fOutX;
public int fInX;
public int fErrorChar;
public int fNull;
public int fRtsControl;
public int fAbortOnError;
public int fDummy2;
public uint flags;
public ushort wReserved;
public ushort XonLim;
public ushort XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public byte XonChar;
public byte XoffChar;
public byte ErrorChar;
public byte EofChar;
public byte EvtChar;
public ushort wReserved1;
}
对 C++比较熟悉网友应该知道,结构体中这种格式的声明,如 DWORD fBinary: 1;是
以位为单位进行变量设置的,DCB 中相关位一共占 4 个字节,也就是相当于 C#中的一个
int 变量所占的空间。很明显上面的 DCB 结构会有问题,实际上后面你设置的串口参数,
如奇偶校验由于偏移有问题,虽然你设置了,其实都没有设置成功。
其实也不是我说人家的 DCB 声明错了就错了,在 SerialPort 类中你就可以找到微软官
方自己的 DCB 声明(需要反编译 SerialPort 类),声明如下:
[StructLayout(LayoutKind.Sequential)]
public struct DCB
{
public int DCBlength;
public int BaudRate;
public uint Flags;

public ushort wReserved;
public ushort XonLim;
public ushort XoffLim;
public byte ByteSize;
public byte Parity;
public byte StopBits;
public byte XonChar;
public byte XoffChar;
public byte ErrorChar;
public byte EofChar;
public byte EvtChar;
public ushort wReserved1;
}
并且专门有一个设置位标志的函数,如下:
internal void SetDcbFlag(int whichFlag, int setting)
{
uint num;
setting = setting << whichFlag;
if ((whichFlag == 4) || (whichFlag == 12))
{
num = 3;
}
else if (whichFlag == 15)
{
num = 0x1ffff;
}
else
{
num = 1;
}
dcb.flags &= ~(num << whichFlag);
dcb.flags |= (uint)setting;
}
经过修改能正确运行的 API 代码如下(注意,由于我是在 WinCE 平台上运行,所以
DLL 的路径为"//windows//coredll.dll",你修改为"kernel32"后即可在 PC 机使用):
///<summary>
/// API 串口类 http://blog.csdn.net/yefanqiu
///</summary>
public class CommPort
{
///<summary>
///端口名称(COM1,COM2...COM4...)
///</summary>
public string Port = "COM1:";
剩余12页未读,继续阅读
资源评论


ljshell
- 粉丝: 0
- 资源: 52
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基础算法-python爬楼梯问题
- 某音Web端参数X-Bogus获取算法(逆向分析)
- 头歌答案 C语言程序设计实践 实验二 数据类型与基本操作(1)
- java高校实习生管理系统设计和实现springboot+vue毕业设计源码+数据库mysql代码.rar
- springboot+vue.js辽B代驾管理系统java毕业设计源码+数据库代码.rar
- OceanBase OBCA初级考试认证资料
- java可信捐赠管理系统的设计与开发springboot+vue毕业设计源码+数据库代码.rar
- vue基于Springboot的网上宠物店系统的设计与实现java毕业设计源码+数据库代码.rar
- OceanBase OBCA 部分题目
- vue基于springboot的七彩云南文化旅游网站的设计与实现java毕业设计源码代码+数据库.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
