
收到错误的数据:查看串口设置:波特率等。调试助手不一定解释TextBox的Unicode字符,发送ASCII字符试试。
发送16进制数,可以用字节数组,如果需要从界面接收用户输入:
byte[] bytes=new byte[textBox1.Text.Length/2]
for (int i = 0i <textBox1.Text.Lengthi += 2)
{
byte b = Convert.ToByte(textBox1.Text.Substring(i,2), 16)
bytes[i / 2] = b
}
然后发送bytes即可。
看不太懂vb但凭浅显之经验,我觉得假死主要是sleep导致的假死,进而导致下一次接收不到
vb下是否有多线程?用另一个线程启动一个定时器,替代原来的主线程sleep(2000)
C#串口读写程序方法具体如下:PortDataSend.cs 发送Class
using System
using System.Collections.Generic
using System.Text
namespace IOCPTest.BaseC
...{
public class PortDataSend
...{
Declare#region Declare
private long mCount = 0
private int mDataLength = 0
private byte[] BS = null
#endregion Declare
PortDataSend#region PortDataSend
public PortDataSend()
...{
}
public PortDataSend(byte[] pBS, int pDataLength)
...{
BS = pBS
mDataLength = pDataLength
}
#endregion PortDataSend
DataSend#region DataSend
public void DataSend()
...{
try
...{
if (BS == null)
...{
return
}
if (mDataLength == 0)
...{
mDataLength = BS.Length
}
if (mDataLength == 0)
...{
return
}
mDataLength = mDataLength + 2
byte[] BST = new byte[mDataLength]
BST[BST.Length - 2] = 0x00
BST[BST.Length - 1] = 0x03
for (int i = 0i <BS.Lengthi++)
...{
BST[i] = BS[i]
BST[BST.Length - 2] = (byte)(BST[BST.Length - 2] ^ BS[i])
}
IOCPTest.BaseC.GlobeValues.SP.Write(BST, 0, BST.Length)
mCount++
mDataLength = 0
}
catch
...{
}
}
#endregion DataSend
mCount#region mCount
public long GetCount
...{
get
...{
return mCount
}
}
#endregion mCount
DataSendTest3#region DataSendTest3
public void DataSendTest3()
...{
try
...{
byte[] BSTest
string[] mBSStr = Convert.ToString("01 02 03 01 00 3A 02 03 31 31 31 31 31 31 31 31 31 31 31 31 2C 00 01 2C 00 64 3B 32 32 32 32 32 32 32 32 32 32 32 32 2C 00 01 2C 00 64 3B 33 33 33 33 33 33 33 33 33 33 33 33 2C 00 01 2C 00 64 3B").Split(' ')
BSTest = new byte[mBSStr.Length]
for (int i = 0i <BSTest.Lengthi++)
...{
BSTest[i] = (byte)Convert.ToInt32(mBSStr[i], 16)
}
BS = BSTest
DataSend()
}
catch
...{
}
}
#endregion DataSendTest3
}
}
PortDataReceived.cs 接收Class
using System
using System.Collections.Generic
using System.Text
namespace IOCPTest.BaseC
...{
class PortDataReceived
...{
Declare#region Declare
private long mCount = 0
private int mDataEnd = 0
private byte[] BR = new byte[2048]
private byte[] BRTmp = new byte[2048]
private byte mSOH = 0x01 //通讯特殊字符定义:通讯字头
private byte mSTX = 0x02 //通讯特殊字符定义:数据开始
private byte mETX = 0x03 //通讯特殊字符定义:通讯结束
#endregion Declare
PortDataReceived#region PortDataReceived
public PortDataReceived()
...{
}
#endregion PortDataReceived
Listen#region Listen
public byte[] Listen()
...{
try
...{
byte[] BR = ReceiveData()
return BR
}
catch (NullReferenceException NullEx)
...{
throw NullEx
}
catch (Exception ex)
...{
throw ex
}
}
#endregion Listen
ReceiveData#region ReceiveData
public byte[] ReceiveData()
...{
try
...{
int mStartCount = 0
int mSOHPos = 0
int mSTXPos = 0
int mETXPos = 0
int mCmdLength = 0
System.DateTime DT = System.DateTime.Now
byte[] Data = null
BRTmp = new byte[2048]
while((System.DateTime.Now.Ticks - DT.Ticks) <IOCPTest.BaseC.GlobeValues.PortTimeOut)
...{
try
...{
mStartCount = 0
//System.Threading.Thread.Sleep(IOCPTest.BaseC.GlobeValues.PortDelay)
mStartCount = IOCPTest.BaseC.GlobeValues.SP.Read(BRTmp, 0, 2048)
for (int i = 0i <mStartCounti++)
...{
BR[i + mDataEnd] = BRTmp[i]
}
mDataEnd = mDataEnd + mStartCount
}
catch
...{
}
if (mStartCount >0)
...{
DT = System.DateTime.Now
}
//寻找并且校验SOH、STX、ETX的位置
for (int i = 0i <BR.Length - 6i++)
...{
if((BR[i] == mSOH) &&(BR[i+6] == mSTX))
...{
mSOHPos = i
mSTXPos = i+6
mCmdLength = (int)BR[i+4] * 256 + (int)BR[i+5]
mETXPos = mSTXPos + mCmdLength + 2
if (BR[mETXPos] == mETX)
...{
Data = new byte[mSTXPos + mCmdLength + 1 - mSOHPos]
for (int j = 0j <mSTXPos + mCmdLength + 1 - mSOHPosj++)
...{
Data[j] = BR[mSOHPos + j]
}
for (int j = 0j <(mDataEnd - (mSTXPos + mCmdLength + 1))j++)
...{
BR[j] = BR[(mSTXPos + mCmdLength + 1) + j]
}
for (int j = (mDataEnd - (mSTXPos + mCmdLength + 1))j <2048j++)
...{
BR[j] = 0x00
}
mDataEnd = mDataEnd - (mSTXPos + mCmdLength + 1)
IOCPTest.BaseC.GlobeValues.DataBuffer.Add(Data)
mCount++
return Data
}
}
}
}
return null
}
catch (NullReferenceException NullEx)
...{
throw NullEx
}
catch (Exception ex)
...{
throw ex
}
}
#endregion ReceiveData
ReceiveDataNonReturn#region ReceiveDataNonReturn
public void ReceiveDataNonReturn()
...{
try
...{
ReceiveData()
}
catch
...{
}
}
#endregion ReceiveData
ReceiveDataThread#region ReceiveDataThread
public void ReceiveDataThread()
...{
try
...{
while (true)
...{
try
...{
ReceiveData()
}
catch
...{
}
}
}
catch
...{
}
}
#endregion ReceiveData
mCount#region mCount
public long GetCount
...{
get
...{
return mCount
}
}
#endregion mCount
}
}
DataProcessingIOCP.cs 处理Class
using System
using System.Collections.Generic
using System.Text
namespace IOCPTest.BaseC
...{
public class DataProcessingIOCP
...{
Declare#region Declare
private long mCount = 0
#endregion Declare
DataProcessingIOCP#region DataProcessingIOCP
public DataProcessingIOCP()
...{
}
#endregion DataProcessingIOCP
DataProcessing#region DataProcessing
public void DataProcessing()
...{
try
...{
int mCurrentBuffer = 0
while (true)
...{
byte[] RD = null
提取正待处理的数据#region 提取正待处理的数据
if (IOCPTest.BaseC.GlobeValues.DataBuffer != null)
...{
if (IOCPTest.BaseC.GlobeValues.DataBuffer.Count >0)
...{
if (mCurrentBuffer <0)
...{
mCurrentBuffer = 0
}
RD = (byte[])IOCPTest.BaseC.GlobeValues.DataBuffer[mCurrentBuffer]
mCurrentBuffer++
}
else
...{
continue
}
}
else
...{
continue
}
#endregion 提取正待处理的数据
数据处理#region 数据处理
switch (RD[3]) //指令处理
...{
case 0x01:
ReadData_0X01(RD)
break
case 0x02:
ReadData_0X02(RD)
break
}
#endregion 数据处理
处理结束,删除已经处理了的数据#region 处理结束,删除已经处理了的数据
if (IOCPTest.BaseC.GlobeValues.DataBuffer != null)
...{
if (IOCPTest.BaseC.GlobeValues.DataBuffer.Count >0)
...{
IOCPTest.BaseC.GlobeValues.DataBuffer.Remove(RD)
}
}
mCurrentBuffer--
if (mCurrentBuffer <0)
...{
mCurrentBuffer = 0
}
#endregion 处理结束,删除已经处理了的数据
mCount++
}
}
catch
...{
}
}
#endregion DataProcessing
mCount#region mCount
public long GetCount
...{
get
...{
return mCount
}
}
#endregion mCount
ReadData_0X01#region ReadData_0X01
private void ReadData_0X01(byte[] pRD)
...{
try
...{
try
...{
if (pRD[3] != 0x01)
...{
return
}
string[] mSampleCode = new string[pRD[7]]
int[] mTesterType = new int[pRD[7]]
int[] mLoadCapacity = new int[pRD[7]]
for (int i = 0i <pRD[7]i++)
...{
for (int j = 0j <12j++)
...{
mSampleCode[i] = mSampleCode[i] + Convert.ToString(Convert.ToChar(pRD[8 + j + i * 19]))
}
mTesterType[i] = pRD[i * 19 + 21] * 255 + pRD[i * 19 + 22]
mLoadCapacity[i] = pRD[i * 19 + 24] * 255 + pRD[i * 19 + 25]
}
return
}
catch (Exception Ex)
...{
throw Ex
}
}
catch
...{
}
}
#endregion ReadData_0X01
ReadData_0X02#region ReadData_0X02
private void ReadData_0X02(byte[] pRD)
...{
try
...{
try
...{
if (pRD[3] != 0x02)
...{
return
}
int[] mData = new int[pRD[7]]
string mSampleCode = ""
for (int i = 0i <12i++)
...{
mSampleCode = mSampleCode + Convert.ToString(Convert.ToChar(pRD[8 + i]))
}
for (int i = 0i <mData.Lengthi++)
...{
mData[i] = pRD[i * 2 + 20] * 255 + pRD[i * 2 + 21]
}
}
catch (Exception Ex)
...{
throw Ex
}
}
catch
...{
}
}
#endregion ReadData_0X02
ReadData_0X02_Request#region ReadData_0X02_Request
private void ReadData_0X02_Request(byte[] pRD)
...{
try
...{
try
...{
if (pRD[3] != 0x02)
...{
return
}
int[] mData = new int[pRD[7]]
string mSampleCode = ""
for (int i = 0i <12i++)
...{
mSampleCode = mSampleCode + Convert.ToString(Convert.ToChar(pRD[8 + i]))
}
for (int i = 0i <mData.Lengthi++)
...{
mData[i] = pRD[i * 2 + 20] * 255 + pRD[i * 2 + 21]
}
}
catch (Exception Ex)
...{
throw Ex
}
}
catch
...{
}
}
#endregion ReadData_0X02_Request
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)