public partial class Form1 : Form
{
private Conn conn = new Conn();
public Form1()
{
InitializeComponent();
}
private void ReadData()
{
while (true)
{
string msg = "";
if (conn.Receive(ref msg, 200))
{
if (0 < msg.Length)
{
textBoxMsg.AppendText(msg + "\r\n");
msg = "";
}
}
Thread.Sleep(100);
}
}
private void button_open_Click(object sender, EventArgs e)
{
conn.Init(1,115200,"8021");
if (conn.Connect())
{
textBoxMsg.AppendText("连接成功\r\n");
}
else
{
textBoxMsg.AppendText("连接失败\r\n");
}
}
private void button_close_Click(object sender, EventArgs e)
{
conn.Close();
textBoxMsg.AppendText("连接关闭\r\n");
}
private void button_auto_read_Click(object sender, EventArgs e)
{
Thread readThread = new Thread(new ThreadStart(ReadData));
readThread.IsBackground = true;
readThread.Start();
}
private void button_isConn_Click(object sender, EventArgs e)
{
if (conn.Connected())
{
MessageBox.Show("已连接上");
}
else
{
MessageBox.Show("正在连接...");
}
}
private void button_send_Click(object sender, EventArgs e)
{
if (conn.Send(textBoxSend.Text))
{
MessageBox.Show("发送成功");
}
else
{
MessageBox.Show("发送失败");
}
}
}
dll
public class Conn
{
private int port;
private int baudRate;
private bool isConnected = false;
private string tel;
private Thread stat = null;
#region C# com 串口通信
private mycom com = new mycom();
/// <summary>
/// 连接状态
/// </summary>
private void ConnStat()
{
while(true)
{
if (-1 < Encoding.ASCII.GetString(ComRead(200)).IndexOf("CONNECT"))
{
isConnected = true;
stat.Abort();
}
Thread.Sleep(50);
}
}
/// <summary>
/// 打开串口
/// </summary>
/// <returns></returns>
private bool OpenCom()
{
bool flag = false;
com.PortNum = port;
com.BaudRate = baudRate;
com.ByteSize = 8;
com.Parity = 0;
com.StopBits = 1;
com.ReadTimeout = 5000;
try
{
if (com.Opened)
{
com.Close();
com.Open();
}
else
{
com.Open();
}
flag = true;
}
catch
{
}
return flag;
}
/// <summary>
/// 关闭串口
/// </summary>
private void CloseCom()
{
com.Close();
}
/// <summary>
/// 向COM发送数据
/// </summary>
/// <param name="data"></param>
private void ComSend(byte[] data)
{
com.Write(data);
}
/// <summary>
/// 从COM读取数据
/// </summary>
/// <returns></returns>
private byte[] ComRead(int len)
{
return com.Read(len);
}
#endregion
/** 初始化通讯 */
public void Init(int port, int baudRate,string tel)
{
this.port = port;
this.baudRate = baudRate;
this.tel = tel;
}
/** 清理通讯 */
public void Clean()
{
}
/** 连接服务器 */
public bool Connect()
{
bool flag = false;
if (OpenCom())
{
Send("ATDT" + tel + "\r\n");
Thread.Sleep(20);
stat = new Thread(new ThreadStart(ConnStat));
stat.IsBackground = true;
stat.Start();
flag = true;
}
return flag;
}
/** 检测是否已经连接 */
public bool Connected()
{
return isConnected;
}
/** 断开连接 */
public void Close()
{
CloseCom();
}
/** 发送数据 */
public bool Send(string buffer)
{
bool flag = false;
try
{
ComSend(Encoding.ASCII.GetBytes(buffer+"\r\n"));
flag = true;
}
catch
{
throw (new ApplicationException("send data error"));
}
return flag;
}
/** 接收数据 */
public bool Receive(ref string buffer, int len)
{
bool flag = false;
try
{
buffer = Encoding.ASCII.GetString(ComRead(len));
flag = true;
}
catch
{
throw (new ApplicationException("read data error"));
}
return flag;
}
}
}
at指令控制modem
atdt 8888 拨号 8888
ath 挂断
ata 应答
ats0=2 设置自动应答
- 上一篇:C# TcpListener 通信源码
- 下一篇:vc++ 向串口发送数据源码