积极答复者
求求天上的神仙给个用c#写的能在wince下跑的串口类,实在是感激不尽

问题
答案
-
这个是串口类
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;namespace DeviceApplication15
{public partial class Serial
{
public SerialPort myport = null;
public Serial()
{
myport = new SerialPort("COM1:");
myport.BaudRate = 9600;
myport.DataBits = 8;
}public bool open()
{
try
{
if(myport!=null)
myport.Open();
return true;
}
catch (Exception e)
{
return false;
}
}public void close()
{
if (myport != null)
{
if (myport.IsOpen)
myport.Close();
}
}public string read()
{
try
{
string myString = "";
byte[] recvBuff = new byte[1024];
int count=0;
if (myport.IsOpen)
{
count = myport.Read(recvBuff, 0, 1024);
myString = Encoding.Unicode.GetString(recvBuff, 0, count);
}
return myString;
}
catch(Exception e)
{
return "";
}
}public bool write(string myString)
{
try
{
byte[] writeBuff = Encoding.Unicode.GetBytes(myString);
if (myport.IsOpen)
{
myport.Write(writeBuff, 0, writeBuff.Length);
return true;
}
return false ;
}
catch (Exception e)
{
return false;
}
}
}
}下面这个是页面上的操作
Serial mySerial = new Serial();
public Form1()
{
InitializeComponent();
mySerial.myport.DataReceived += new SerialDataReceivedEventHandler(receiveData);
}
private void receiveData(object sender, SerialDataReceivedEventArgs e)
{string mystring=mySerial.read();
}代码是现写的
没有CE串口平台
没有经过测试什么的
大概就是这样样子
你自己在真实机上测试调整下
全部回复
-
这个是串口类
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;namespace DeviceApplication15
{public partial class Serial
{
public SerialPort myport = null;
public Serial()
{
myport = new SerialPort("COM1:");
myport.BaudRate = 9600;
myport.DataBits = 8;
}public bool open()
{
try
{
if(myport!=null)
myport.Open();
return true;
}
catch (Exception e)
{
return false;
}
}public void close()
{
if (myport != null)
{
if (myport.IsOpen)
myport.Close();
}
}public string read()
{
try
{
string myString = "";
byte[] recvBuff = new byte[1024];
int count=0;
if (myport.IsOpen)
{
count = myport.Read(recvBuff, 0, 1024);
myString = Encoding.Unicode.GetString(recvBuff, 0, count);
}
return myString;
}
catch(Exception e)
{
return "";
}
}public bool write(string myString)
{
try
{
byte[] writeBuff = Encoding.Unicode.GetBytes(myString);
if (myport.IsOpen)
{
myport.Write(writeBuff, 0, writeBuff.Length);
return true;
}
return false ;
}
catch (Exception e)
{
return false;
}
}
}
}下面这个是页面上的操作
Serial mySerial = new Serial();
public Form1()
{
InitializeComponent();
mySerial.myport.DataReceived += new SerialDataReceivedEventHandler(receiveData);
}
private void receiveData(object sender, SerialDataReceivedEventArgs e)
{string mystring=mySerial.read();
}代码是现写的
没有CE串口平台
没有经过测试什么的
大概就是这样样子
你自己在真实机上测试调整下