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

晕。。没人理

2007-10-30 08:22
bigdogme2003
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-6-23
收藏
得分:0 
看不懂啊
我也想学下通信口编程啊
2007-10-30 18:48
快速回复:串口通信接收字符串不对
数据加载中...
 
   



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

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