You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
6.7 KiB
212 lines
6.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.IO.Ports;
|
|
using System.Threading;
|
|
|
|
namespace WcfControlMonitorLib
|
|
{
|
|
public partial class SMS : UserControl
|
|
{
|
|
public SMS()
|
|
{
|
|
InitializeComponent();
|
|
this.ReciveEvent += new dele_rev(SMS_ReciveEvent);
|
|
}
|
|
|
|
void SMS_ReciveEvent(string str)
|
|
{
|
|
//throw new NotImplementedException();
|
|
//收到就发下一个
|
|
//发送完毕就应该拿出去
|
|
if (messages.Count > 0)
|
|
{
|
|
PhoneMsgPair msg = messages.Dequeue();
|
|
BeginSendMessage(msg.Phone, msg.Msg);
|
|
}
|
|
}
|
|
|
|
public bool IsRuning;
|
|
public Encoding Encode;
|
|
|
|
private static object _thislock = new object();
|
|
|
|
//发送队列
|
|
private Queue<PhoneMsgPair> messages = new Queue<PhoneMsgPair>();
|
|
|
|
public event dele_rev ReciveEvent;
|
|
public void Start()
|
|
{
|
|
try
|
|
{
|
|
SerialPort1.Open();
|
|
IsRuning = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
IsRuning = false;
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
try
|
|
{
|
|
SerialPort1.Close();
|
|
IsRuning = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
|
|
public void AddMessage(string phone, string msg)
|
|
{
|
|
if (!IsRuning)
|
|
return;
|
|
//PhoneMsgPair pair = new PhoneMsgPair(phone, msg);
|
|
//messages.Enqueue(pair);
|
|
//if (messages.Count == 1) //如果第一个,驱动发送一下,先发一个假的
|
|
//{
|
|
// //messages.Dequeue();
|
|
// BeginSendMessage("", "");
|
|
// //BeginSendMessage(phone, msg);
|
|
//}
|
|
SendMessage(phone, msg);
|
|
|
|
}
|
|
|
|
public void BeginSendMessage(string phone, string message)
|
|
{
|
|
dele_sendmessage del = new dele_sendmessage(SendMessage);
|
|
del.BeginInvoke(phone, message, SendFinish, del);
|
|
}
|
|
|
|
//封装AT指令,从串口发出去,同步方式
|
|
public string SendMessage(string phone, string message)
|
|
{
|
|
if (phone == "") return "begin";
|
|
////string head = "AT+SMSD=" + phone + "," + "UCS2" + ",";
|
|
//string head = "AT+SMSEND=86" + phone + ",";
|
|
//byte[] headbytes = Encoding.ASCII.GetBytes(head);
|
|
|
|
//byte[] bytes = Encoding.Unicode.GetBytes(message);
|
|
////这里需要高低字节调换,短信才能认识
|
|
//for (int i = 0; i < bytes.Length / 2; i++)
|
|
//{
|
|
// int pos = i * 2;
|
|
// byte temp = bytes[pos + 1];
|
|
// bytes[pos + 1] = bytes[pos];
|
|
// bytes[pos] = temp;
|
|
//}
|
|
|
|
|
|
//byte[] tosend = new byte[headbytes.Length + bytes.Length];
|
|
|
|
//for (int i = 0; i < headbytes.Length; i++)
|
|
//{
|
|
// tosend[i] = headbytes[i];
|
|
//}
|
|
//for (int i = headbytes.Length; i < headbytes.Length + bytes.Length; i++)
|
|
//{
|
|
// tosend[i] = bytes[i - headbytes.Length];
|
|
//}
|
|
|
|
//SerialPort1.Write(tosend, 0, tosend.Length);
|
|
|
|
string messages = "";
|
|
//messages = "AT + ENTM";
|
|
//SerialPort1.Write(messages);
|
|
//Thread.Sleep(300);
|
|
//SerialPort1.Write(messages);
|
|
//Thread.Sleep(300);
|
|
//messages = "+++";
|
|
//SerialPort1.Write(messages);
|
|
Thread.Sleep(1000);
|
|
messages = "usr.cnAT+SMSEND=86" + phone + "," + message + "\r\n";
|
|
|
|
SerialPort1.Write(messages);
|
|
|
|
//41 54 2B 53 4D 53 45 4E 44 3D 38 36 31 37 33 30 32 34 36 32 37 36 33 2C
|
|
//53 54 52 55 43 54 A3 BA 33 37 30 30 32 65 72 72 6F 72 A3 AC 45 52 52 4F
|
|
//52 4D 45 53 53 41 47 45 A3 BA 47 52 45 41 53 45 54 49 4D 45 4F 55 54 0D 0A
|
|
//设备:37002故障,故障信息:注油操作超时
|
|
//STRUCT:37002error,ERRORMESSAGE:GREASETIMEOUT
|
|
|
|
//发出去阻塞等回
|
|
//Thread.Sleep(3000); //等待是为了保证dtu回复数据到缓冲区中
|
|
byte[] revbuffer = new byte[64];
|
|
int len = 0;
|
|
string str = "";
|
|
//try
|
|
//{
|
|
// while (str != "SHORT MESSAGE ERROR!" && str != "SHORT MESSAGE SEND OK!") //拼接字符串,一直到够回复信息的
|
|
// {
|
|
// len = SerialPort1.Read(revbuffer, 0, revbuffer.Length);
|
|
// str += Encoding.ASCII.GetString(revbuffer, 0, len);
|
|
// }
|
|
//}
|
|
//catch (Exception ex)
|
|
//{
|
|
//}
|
|
//将回复的ascll转成字符
|
|
|
|
return str;
|
|
}
|
|
|
|
private void SendFinish(IAsyncResult ar)
|
|
{
|
|
dele_sendmessage fun = (dele_sendmessage)ar.AsyncState;
|
|
string rev = "";
|
|
try
|
|
{
|
|
rev = fun.EndInvoke(ar);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
|
|
object[] param = new object[1];
|
|
param[0] = rev;
|
|
//收到回复回归主线程
|
|
this.BeginInvoke(new dele_rev(messagerev), param);
|
|
}
|
|
|
|
private void messagerev(string str)
|
|
{
|
|
ReciveEvent(str);
|
|
}
|
|
|
|
//串口参数,和发送的编码格式
|
|
public void SetSMS(string Portname, int BaudRate, string parity, int databits, string stopbits, Encoding code, int readtimeout)
|
|
{
|
|
SerialPort1.PortName = Portname;
|
|
SerialPort1.BaudRate = BaudRate;
|
|
SerialPort1.Parity = (Parity)Enum.Parse(SerialPort1.Parity.GetType(), parity);
|
|
SerialPort1.DataBits = databits;
|
|
SerialPort1.StopBits = (StopBits)Enum.Parse(SerialPort1.StopBits.GetType(), stopbits);
|
|
Encode = code;
|
|
SerialPort1.ReadTimeout = readtimeout;
|
|
}
|
|
|
|
//短信队列类,集成了发送队列,外部调用只需要扔队列
|
|
delegate string dele_sendmessage(string phone, string message);
|
|
public delegate void dele_rev(string str);
|
|
public class PhoneMsgPair
|
|
{
|
|
public PhoneMsgPair(string phone, string msg)
|
|
{
|
|
Phone = phone;
|
|
Msg = msg;
|
|
}
|
|
public string Phone;
|
|
public string Msg;
|
|
}
|
|
}
|
|
}
|