最近研究串口通信,小弟刚学,有些问题不懂,特来请教。
把下面的代码放在“控制台应用程序里“,就可以了
问题:
就是发送出去的字符串,我接收回来的不对。。超过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);
}
}
}
在线等待,急,希望各位大哥帮忙看看。