110,565
社区成员
发帖
与我相关
我的任务
分享
public void Open()
{
DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
// 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
if (hComm == INVALID_HANDLE_VALUE)
{
throw (new ApplicationException("非法操作,不能打开串口!"));
}
// 设置通信超时时间 SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, ref ctoCommPort);
// 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.fOutxCtsFlow = 524800;
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.flags = 0;
dcbCommPort.flags |= 1;
if (Parity > 0)
{
dcbCommPort.flags |= 2;
}
dcbCommPort.Parity = Parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
{
CloseHandle(hComm);
throw (new ApplicationException("非法操作,不能打开串口!"));
}
Opened = true;
}
ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"));//获取手机短信中心号
string centerNumber = Encoding.ASCII.GetString(ss_port.Read(128));
if (centerNumber.Length > 0)
{
centerNumber = centerNumber.Substring(20, 13);
}
string decodedSMS = sms.smsDecodedsms(centerNumber, phoneNumber, content);
byte[] buf = Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r", sms.nLength));
ss_port.Write(buf);
string response = Encoding.ASCII.GetString(ss_port.Read(128));
if (response.Length > 0 && response.EndsWith("> "))
{
ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a", decodedSMS)));
SendState = true;
}