大家一定都有用过 JustinIO吧,最近做串口通信,发现怎么都不能实现协议,最后用串口调试工具检测一下串口发送的数据,发现发送来的数据有错误,
原本发送的数据
byte[] bs = new byte[]{
0x02,0x52,0x44,0x53,0x41,0x2A,0x0D};
结果在另一段收到的是
82 D2 44 53 41 AA 8D
数据的最高位出错了,检查了半天JustinIO类都没有发现错误在哪里,如果那位达人能够指明一二不胜感激。
注:端口号,波特率,数据位,停止位,校验位都是正确的!
原本发送的数据
byte[] bs = new byte[]{
0x02,0x52,0x44,0x53,0x41,0x2A,0x0D};
结果在另一段收到的是
82 D2 44 53 41 AA 8D
数据的最高位出错了,检查了半天JustinIO类都没有发现错误在哪里,如果那位达人能够指明一二不胜感激。
注:端口号,波特率,数据位,停止位,校验位都是正确的!
using System;
using System.Runtime.InteropServices;namespace JustinIO
{
public class CommPort
{ public string PortNum; //端口号
public int BaudRate;//波特率
public byte ByteSize;//字节大小
public byte Parity; // 0-4=no,odd,even,,space
public byte StopBits; // 0,1,2 = 1, 1.5, 2
public int ReadTimeout; //comm port win32 file handle
private int hComm = -1; public bool Opened = false; //win32 api constants
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1; [StructLayout(LayoutKind.Sequential)]
public struct DCB
{
//taken from c struct in platform sdk
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 char XonChar;
public char XoffChar;
public char ErrorChar;
public char EofChar;
public char EvtChar;
public ushort wReserved1;
} [StructLayout(LayoutKind.Sequential)]
private struct COMMTIMEOUTS
{
public int ReadIntervalTimeout;
public int ReadTotalTimeoutMultiplier;
public int ReadTotalTimeoutConstant;
public int WriteTotalTimeoutMultiplier;
public int WriteTotalTimeoutConstant;
} [StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
} [DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool GetCommState(
int hFile, //通信设备句柄
ref DCB lpDCB // 设备控制块DCB
);
[DllImport("kernel32.dll")]
private static extern bool BuildCommDCB(
string lpDef, // 设备控制字符串
ref DCB lpDCB // 设备控制块
);
[DllImport("kernel32.dll")]
private static extern bool SetCommState(
int hFile, // 通信设备句柄
ref DCB lpDCB // 设备控制块
);
[DllImport("kernel32.dll")]
private static extern bool GetCommTimeouts(
int hFile, // 通信设备句柄 handle to comm device
ref COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out values
);
[DllImport("kernel32.dll")]
private static extern bool SetCommTimeouts(
int hFile, // 通信设备句柄 handle to comm device
ref COMMTIMEOUTS lpCommTimeouts // 超时时间 time-out values
);
[DllImport("kernel32.dll")]
private static extern bool ReadFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject // handle to object
);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
public void Open()
{ DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); // 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
if (hComm == INVALID_HANDLE_VALUE)
{
throw (new ApplicationException("非法操作,不能打开串口!"));
} // 设置通信超时时间 SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, ref ctoCommPort); // 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.flags = 0;
//dcb.fBinary=1;
dcbCommPort.flags |= 1;
if (Parity > 0)
{
//dcb.fParity=1
dcbCommPort.flags |= 2;
}
dcbCommPort.Parity = Parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
{
//uint ErrorNum=GetLastError();
throw (new ApplicationException("非法操作,不能打开串口!"));
}
//unre to see if setting took correctly
//DCB dcbCommPort2 = new DCB();
//GetCommState(hComm, ref dcbCommPort2);
Opened = true;
} public void Close()
{
if (hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(hComm);
}
} public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes;
BufBytes = new byte[NumBytes];
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort);
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, OutBytes, BytesRead);
}
else
{
throw (new ApplicationException("串口未打开!"));
}
return OutBytes;
} public void Write(byte[] WriteBytes)
{
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
}
else
{
throw (new ApplicationException("串口未打开!"));
}
}
}
我也发现是最高位错了,但是在程序里没有改动他啊