| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3416 人关注过本帖, 1 人收藏
标题:串口通信接收字符串不对
取消只看楼主 加入收藏
wzg0319
Rank: 5Rank: 5
等 级:职业侠客
帖 子:68
专家分:305
注 册:2007-9-12
收藏(1)
 问题点数:0 回复次数:2 
串口通信接收字符串不对

最近研究串口通信,小弟刚学,有些问题不懂,特来请教。
把下面的代码放在“控制台应用程序里“,就可以了
问题:
就是发送出去的字符串,我接收回来的不对。。超过10个字符就不行了,请问问师出在哪。。。
以下是我的代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace 调试
{
public class classcommport
{
public classcommport()
{
}
public string portnum="COM1";
public int baudrate=9600;
public byte bytesize=8;
public byte parity=0;
public byte stopbits=1;
public int readtimeout=1000;
private int hcomm = -1;
public bool opened = false;
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
{
public int dcblenght;
public int baudrate;
public int fbinary;
public int fparity;
public int foutxctsflow;
public int foutxdsrflw;
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 flag;
public uint flag1;
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 interna1;
public int internalhigh;
public int offset;
public int offsethight;
public int hevent;
}


[DllImport("kernel32.dll")]
private static extern int CreateFile(
string ipfilename,
uint dwdesiredaccess,
int dwsharemode,
int ipsecurityattributes,
int dwcreationdisposition,
int dwflagsandattributes,
int htemplatefile
);
[DllImport("kernel32.dll")]
private static extern bool GetCommState(
int hfile,
ref dcb ipdcb
);
[DllImport("kernel32.dll")]
private static extern bool BuildCommDCB(
int hfile,
ref dcb ipdcb
);
[DllImport("kernel32.dll")]
private static extern bool SetCommState(
int hfile,
ref dcb ipdcb
);
[DllImport("kernel32.dll")]
private static extern bool GetCommTimeouts(
int hfile,
ref commtimeouts ipcommtimeouts
);
[DllImport("kernel32.dll")]
private static extern bool SetCommTimeouts(
int hfile,
ref commtimeouts ipcommtimeouts
);
[DllImport("kernel32.dll")]
private static extern bool ReadFile(
int hfile,
byte[] ipbuffer,
int nnumberofbytestoread,
ref int ipnumberofbytesread,
ref overlapped ipoverlapped
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hfile,
byte[] ipbuffer,
int nnumberofbytestowrite,
ref int ipnumberofbyteswritten,
ref overlapped ipoverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hobject
);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
public void open()
{
dcb dcbcommport = new dcb();
commtimeouts ctocommport = new commtimeouts();
hcomm = CreateFile(portnum, generic_read|generic_write, 0, 0, open_existing, 0, 0);
if (hcomm == invalid_handle_value)
{
throw (new ApplicationException("非法操作,不能打开串口"));
}
//设置串口通信超时
GetCommTimeouts(hcomm, ref ctocommport);
ctocommport.readtotaltimeoutconstant = readtimeout;
ctocommport.readtotaltimeoutmultiplier = 0;
ctocommport.writetotaltimeoutconstant = 0;
ctocommport.writetotaltimeoutmultiplier = 0;
SetCommTimeouts(hcomm, ref ctocommport);
//设置串口
GetCommState(hcomm, ref dcbcommport);
dcbcommport.baudrate = baudrate;
dcbcommport.flag = 0;
dcbcommport.flag1 = 1;
if (parity > 0)
{
dcbcommport.flag1 = 2;
}
dcbcommport.parity = parity;
dcbcommport.bytesize = bytesize;
dcbcommport.stopbits = stopbits;
dcbcommport.foutxctsflow = 524800;
if (!SetCommState(hcomm, ref dcbcommport))
{
CloseHandle(hcomm);
throw (new ApplicationException("非法操作,不能打开串口"));
}
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)
{
throw (new ApplicationException("串口未打开"));
}
overlapped ovlcommport = new overlapped();
int bytesread = 0;
ReadFile(hcomm, bufbytes, numbytes, ref bytesread, ref ovlcommport);//如果我在这里把numbytes改成20或40(就是自己指定接收的字符个数),就好像可以。但是这是治标不治本的方法。我想判断实际的字符是不是等于接收的字符,请问这里该怎么改
outbytes = new byte[bytesread];
Array.Copy(bufbytes, outbytes, bytesread);
return outbytes;

}
public void write(byte[] writebytes)
{
if (hcomm == invalid_handle_value)
{
throw (new ApplicationException("串口未打开"));
}
overlapped ovlcommport = new overlapped();
int byteswritten = 0;
WriteFile(hcomm, writebytes, writebytes.Length, ref byteswritten, ref ovlcommport);
}

static void Main(string[] args)
{
classcommport bb = new classcommport();
bb.open();
Console.WriteLine("请输入发送的字符串");
string aa=Console.ReadLine();
bb.write(Encoding.Default.GetBytes(aa));
Console.WriteLine("接收回来的字符串为");
string cc=Encoding.Default.GetString(bb.read(aa.Length));
Console.WriteLine("{0}",cc);
}
}
}

搜索更多相关主题的帖子: 串口通信 字符 using System public 
2007-10-30 08:36
wzg0319
Rank: 5Rank: 5
等 级:职业侠客
帖 子:68
专家分:305
注 册:2007-9-12
收藏
得分:0 

如果有问题的话,我想就是出在read方法那里。。
现在我在read方法中加了一个循环,好像能解决少数字符的问题,但是遇到多的字符时,字符的顺序就变了,大家都帮忙瞅瞅
public byte[] read(int numbytes)
{

byte[] re =new byte[1000];
byte[] bufbytes;
byte[] outbytes;
bufbytes = new byte[numbytes];
if (hcomm == invalid_handle_value)
{
throw (new ApplicationException("串口未打开"));
}
overlapped ovlcommport = new overlapped();
int bytesread = 0;
int bytesread1=0;
int sum = 0;
ReadFile(hcomm, bufbytes, numbytes, ref bytesread, ref ovlcommport);
Console.WriteLine(bytesread);
Console.WriteLine(numbytes);
while (bytesread!= numbytes)
{
ReadFile(hcomm, bufbytes, numbytes, ref bytesread1, ref ovlcommport);
Console.WriteLine(bytesread1);
sum =sum+bytesread1;
bytesread =sum+bytesread;
}
Console.WriteLine(bytesread);
outbytes = new byte[bytesread];
Array.Copy(bufbytes, outbytes, bytesread);
return outbytes;
}

2007-10-30 14:58
wzg0319
Rank: 5Rank: 5
等 级:职业侠客
帖 子:68
专家分:305
注 册:2007-9-12
收藏
得分:0 

哇。。。高兴,好兴奋呀!!!!!终于被我想出来了怎么解决了。。。呵呵。。
太高兴了,不知道怎么形容。。。呵呵。。。爽
我把我改的read方法代码贴出来,希望对有需要的朋友有帮助。。。爽~~~~~~~~~~~~
public byte[] read(int numbytes)
{

byte[] re = new byte[1000];
byte[] bufbytes;
byte[] outbytes;

bufbytes = new byte[numbytes];
if (hcomm == invalid_handle_value)
{
throw (new ApplicationException("串口未打开"));
}
overlapped ovlcommport = new overlapped();
int bytesread = 0;
int bytesread1 = 0;
int sum = 0;
string aa = "", temp = "";
while (bytesread != numbytes)
{
ReadFile(hcomm, bufbytes, numbytes, ref bytesread1, ref ovlcommport);
outbytes = new byte[bytesread1];
Array.Copy(bufbytes, outbytes, bytesread1);
aa = Encoding.Default.GetString(outbytes);
temp = temp + aa;
sum = sum + bytesread1;
bytesread = sum + bytesread;
sum = 0;
}
return System.Text.Encoding.Default.GetBytes(temp);
}

2007-10-30 16:47
快速回复:串口通信接收字符串不对
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017731 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved