int CSerialPort::InitPort(CString strComName) { hSerialPort = OpenPort(strComName, CBR_9600,NOPARITY,8, ONESTOPBIT); if(hSerialPort==NULL) { //MessageBox(NULL, "打开指定串口失败,请重新选择串口。","失败", MB_OK|MB_ICONWARNING); return -1; } CString cmd = "AT+CSCA?\r"; char ans[128] ; WritePort(cmd.GetBuffer(0),strlen(cmd)); Sleep(ShortSleepTime); ReadPort(ans,128); CString ansStr = ans; int cou; if((cou=ansStr.Find("+86")) != -1) { m_SmsCenterNum = ansStr.Mid(cou+1,13); } else { return -2; } return 0; }
void SerialPort::Flush() { int a; do { a = ReadPort(); }while((a & 1) == 1 && bytesRead>0); }
/*! @brief get a char from driver, blocking mode. */ unsigned char GetChar_proc(struct _SerialPort *thisport) { #ifndef WIN32 unsigned char byte; int res, tmp; while ( (res = ReadPort(thisport->m_fd, &byte, 1, &tmp)) != SUCCESS ) ; return byte; #else return thisport->debugbuf[thisport->debugii++]; #endif }
ssize_t readLine(struct sserial_props *pProps, char *pBuff, ssize_t nLength) { ssize_t nReadTotal = 0; if (nLength < 2) { LOGE("[%s] Buffer too short (%ld)", __func__, nLength); return -1; } // Reset first two symbols to avoid problems on the first pass of "while" loop memset(pBuff, 0, 2); while (nReadTotal < 2 || strncmp(pBuff + (nReadTotal - 2), "\x0a\x0a", 2) != 0) { if (nReadTotal == nLength) { LOGE("[%s] Buffer too short for this line(%ld) - CRLF not find", __func__, nLength); return -1; } ssize_t nRead = ReadPort(pProps, (void*)(pBuff + nReadTotal), 1); if (nRead == -1) { LOGE("[%s] Reading from port failed (nReadTotal = %ld)", __func__, nReadTotal); return -1; } #ifdef DEBUG static int b = 0; if (b != 0 && nRead == 0) { LOGW(">> Nothing to read"); b = 1; } LOGV(">> 0x%.2X", pBuff[nReadTotal]); #else if (nRead == 0) { LOGW(">> Nothing to read"); return -1; } #endif nReadTotal += nRead; } #ifdef DEBUG LOGV(">> Done = %s", pBuff); #endif return nReadTotal; }
///////////////////////////////////////////////////////////////////////////////// // 函数:gsmReadMessage // // 说明:读取短消息,用+CMGL代替+CMGR,可一次性读出全部短消息 // // 参数: // // pMsg: 短消息缓冲区,必须足够大 // // 返回: 短消息条数 // ///////////////////////////////////////////////////////////////////////////////// int CSerialPort::gsmReadMessage(SM_PARAM* pMsg) { int nLength; // 串口收到的数据长度 int nMsg; // 短消息计数值 char* ptr; // 内部用的数据指针 char *ptr1; char cmd[16]; // 命令串 char ans[1024]; // 应答串 nMsg = 0; ptr = ans; sprintf(cmd, "AT+CMGR=1\r"); // 生成命令 WritePort(cmd, strlen(cmd)); // 输出命令串 Sleep(ShortSleepTime); nLength = ReadPort(ans, 1024); // 读应答数据 // 根据能否找到"+CMS ERROR"决定成功与否 ans[nLength]='\0'; if(nLength > 0 && strncmp(ans, "+CMGR:", 6) != 0) { // 循环读取每一条短消息, 以"+CMGL:"开头 pMsg->index=1; ptr = strstr(ans, "\r\n089168"); ptr += 2; // 跳过"\r\n" ptr1=ptr; for(int ii=0;;ii++) { ptr1=ptr1+1; if(*ptr1=='\r'&&*(ptr1+1)=='\n'&&*(ptr1+2)=='\r'&&*(ptr1+3)=='\n') { *ptr1=0x0; break; } } gsmDecodePdu(ptr, pMsg); // PDU串解码 nMsg++; // 短消息计数加1 } sprintf(cmd, "AT+CMGD=1,4\r"); // 生成命令 WritePort(cmd, strlen(cmd)); // 输出命令串 Sleep(ShortSleepTime); return nMsg; }
///////////////////////////////////////////////////////////////////////////////// // 函数:gsmSendMessage // // 说明:发送短消息 // // 参数: // // pSrc: 源PDU参数指针 // ///////////////////////////////////////////////////////////////////////////////// bool CSerialPort::gsmSendMessage(const SM_PARAM* pSrc) { int nPduLength; // PDU串长度 unsigned char nSmscLength; // SMSC串长度 int nLength; // 串口收到的数据长度 char cmd[16]; // 命令串 char pdu[512]; // PDU串 char ans[128]; // 应答串 nPduLength = gsmEncodePdu(pSrc, pdu); // 根据PDU参数,编码PDU串 strcat(pdu, "\x01a"); // 以Ctrl-Z结束 gsmString2Bytes(pdu, &nSmscLength, 2); // 取PDU串中的SMSC信息长度 nSmscLength++; // 加上长度字节本身 // 命令中的长度,不包括SMSC信息长度,以数据字节计 sprintf(cmd, "AT+CMGS=%d\r", nPduLength / 2 - nSmscLength); // 生成命令 printf(cmd); WritePort(cmd, strlen(cmd)); // 先输出命令串 Sleep(ShortSleepTime); WritePort(pdu, strlen(pdu)); WritePort("\x1A", 1); nLength = ReadPort(ans, 128); // 读应答数据 Sleep(ShortSleepTime); // 根据能否找到"\r\n> "决定成功与否 /* if(nLength == 4 && strncmp(ans, "\r\n> ", 4) == 0) { WritePort(pdu, strlen(pdu)); Sleep(ShortSleepTime);// 得到肯定回答,继续输出PDU串 nLength = ReadPort(ans, 128); // 读应答数据 // 根据能否找到"+CMS ERROR"决定成功与否 if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0) { nLength = ReadPort(ans, 128); // 读应答数据 return FALSE; } }*/ return TRUE; }
///////////////////////////////////////////////////////////////////////////////// // 函数:gsmDeleteMessage // // 说明:删除短消息 // // 参数: // // index: 短消息序号,从1开始 // ///////////////////////////////////////////////////////////////////////////////// bool CSerialPort::gsmDeleteMessage(const int index) { int nLength; // 串口收到的数据长度 char cmd[16]; // 命令串 char ans[128]; // 应答串 sprintf(cmd, "AT+CMGD=%d\r", index); // 生成命令 // 输出命令串 WritePort(cmd, strlen(cmd)); // 读应答数据 nLength = ReadPort(ans, 128); // 根据能否找到"+CMS ERROR"决定成功与否 if(nLength > 0 && strncmp(ans, "+CMS ERROR", 10) != 0) { return TRUE; } return FALSE; }
int main(int argc, char *argv[]) { // Command line arguments const char *optString = "p:b:n:c:h?"; const struct option longOpts[] = { { OPT_PORT_NAME, required_argument, NULL, 'p' }, { OPT_BAUDRATE, required_argument, NULL, 'b' }, { OPT_BT_NAME, required_argument, NULL, 'n' }, { OPT_BT_PIN, required_argument, NULL, 'c' }, { OPT_HELP, no_argument, NULL, 'h' }, { NULL, no_argument, NULL, 0 } }; // Port properties int nBaudrate = 0; const char *portName = NULL; // BT properties const char *btName = NULL; const char *btPin = NULL; int opt, longIndex; while ((opt = getopt_long( argc, argv, optString, longOpts, &longIndex)) != -1) { switch(opt) { case 'p': portName = optarg; break; case 'b': nBaudrate = atoi(optarg); break; case 'n': btName = optarg; break; case 'c': btPin = optarg; break; case 'h': usage(); exit(0); break; // Case for long names case 0: if (strcmp(OPT_PORT_NAME, longOpts[longIndex].name) == 0) { portName = optarg; } else if (strcmp(OPT_BAUDRATE, longOpts[longIndex].name) == 0) { nBaudrate = atoi(optarg); } else if (strcmp(OPT_BT_NAME, longOpts[longIndex].name) == 0) { btName = optarg; } else if (strcmp(OPT_BT_PIN, longOpts[longIndex].name) == 0) { btPin = optarg; } else if (strcmp(OPT_HELP, longOpts[longIndex].name) == 0) { usage(); exit(0); } break; default: usage(); exit(0); break; } } if (portName == NULL) { portName = DEFAULT_PORT_NAME; LOGW("Port name not defined and will be used default \"%s\"", portName); } if (nBaudrate == 0) { nBaudrate = DEFAULT_BAUDRATE; LOGW("Baudrate not defined and will be used default \"%d\"", nBaudrate); } if (btName == NULL) { btName = DEFAULT_BT_NAME; LOGW("Bluetooth name not defined and will be used default \"%s\"", btName); } if (btPin == NULL || strlen(btPin) > DEFAULT_BT_PIN_MAX) { if (btPin == NULL) { LOGW("Bluetooth pin code not defined and will be used default \"%s\"", DEFAULT_BT_PIN); } else { LOGW("Bluetooth pin code too long (%lu), than should be (%d) and will be used default \"%s\"", strlen(btPin), DEFAULT_BT_PIN_MAX, btPin); } btPin = DEFAULT_BT_PIN; } LOGV("Port name = %s", portName); LOGV("Baudrate = %d", nBaudrate); LOGV("Bluetooth name = %s", btName); LOGV("Bluetooth pin code = %s", btPin); struct sserial_props *pProps = OpenPort(portName, nBaudrate); if (pProps == NULL) { LOGE("Port init failed"); return 1; } if (!initBluetooth(pProps, btName, btPin)) { LOGE("[%s] Bluetooth init failed", __func__); goto exit; } char ch='\0'; // print all until "a" will be be recieved while (ch!='a') { int bDisconnected = GetCts(pProps); // It will be true until someone connected if (bDisconnected != 0) { continue; } // skip empty and line endings if (ReadPort(pProps, &ch, 1) <= 0 || ch == '\r' || ch == '\n') continue; LOGV(">> %c", ch); } exit: ClosePort(pProps); return 0; }
///////////////////////////////////////////////////////////////////////////////// // 函数:SendMsg // // 说明:通过指定端口,发送短信 // // 参数: // // strRecvPhone,接收手机号码 // // strMsgContent,短信内容 // // strPortName,串行端口的名称 // ///////////////////////////////////////////////////////////////////////////////// int CSerialPort::SendMsg(CString strRecvPhone , CString strMsgContent) { m_strRecvPhone = strRecvPhone; m_strMsgContent = strMsgContent; int i=0 , t=0; char ans[128]; // 应答串 //char SCA[16]; // 短消息服务中心号码(SMSC地址) char TPA[16]; // 目标号码或回复号码(TP-DA或TP-RA) //char TP_PID; // 用户信息协议标识(TP-PID) //char TP_DCS; // 用户信息编码方式(TP-DCS) // char TP_UD[161]; // 原始用户信息(编码前或解码后的TP-UD) char cmd[20]; MsgList *msglist = new MsgList; int iPage = page(strMsgContent.GetBuffer(strMsgContent.GetLength()),msglist); t= m_strRecvPhone.GetLength()+2; for(i=2;i<t;i++) { TPA[i]=m_strRecvPhone.GetAt(i-2); } TPA[0]='8'; TPA[1]='6'; TPA[t]='\0'; for(int iIndex=0 ;iIndex < iPage;iIndex++) { sprintf(cmd , "AT\r"); WritePort(cmd, strlen(cmd)); Sleep(ShortSleepTime); int nLength = ReadPort(ans, 128); ans[nLength] = '\0'; sprintf(cmd, "ATE0\r"); WritePort(cmd, strlen(cmd)); Sleep(ShortSleepTime); nLength = ReadPort(ans, 128); ans[nLength] = '\0'; sprintf(cmd, "AT+CSMS=0\r"); WritePort(cmd, strlen(cmd)); Sleep(ShortSleepTime); nLength = ReadPort(ans, 128); ans[nLength] = '\0'; sprintf(cmd, "AT+CMGF=0\r"); WritePort(cmd, strlen(cmd)); Sleep(ShortSleepTime); nLength = ReadPort(ans, 128); ans[nLength] = '\0'; sm_param_temp= new SM_PARAM; strcpy(sm_param_temp->SCA,m_SmsCenterNum); sm_param_temp->TP_DCS=0x8; sm_param_temp->TP_PID=0x0; strcpy(sm_param_temp->TPA,TPA); strcpy(sm_param_temp->TP_UD,msglist->chMsg); if(iPage > 1) sprintf(sm_param_temp->TP_UD,"%d/%d %s" ,iIndex+1,iPage,msglist->chMsg); //printf("Msg :%s\n" ,sm_param_temp->TP_UD); msglist = msglist->pNext; if(!gsmSendMessage(sm_param_temp))//发送短信 { printf("Send SMS Failed\n"); return -1; } Sleep(5000); } return 0; }
u32 mahaf_ReadPort32(u16 port) { const u32 value = ReadPort(port, 4); return value; }
u16 mahaf_ReadPort16(u16 port) { const u32 value = ReadPort(port, 2); ENSURE(value <= 0xFFFF); return (u16)(value & 0xFFFF); }
u8 mahaf_ReadPort8(u16 port) { const u32 value = ReadPort(port, 1); ENSURE(value <= 0xFF); return (u8)(value & 0xFF); }
void RxPacket1(int port_num) { UINT8_T _idx, _s; int _i; packetData[port_num].communication_result_ = COMM_TX_FAIL; UINT8_T _checksum = 0; UINT8_T _rx_length = 0; UINT8_T _wait_length = 6; // minimum length ( HEADER0 HEADER1 ID LENGTH ERROR CHKSUM ) while (true) { _rx_length += ReadPort(port_num, &packetData[port_num].rxpacket_[_rx_length], _wait_length - _rx_length); if (_rx_length >= _wait_length) { _idx = 0; // find packet header for (_idx = 0; _idx < (_rx_length - 1); _idx++) { if (packetData[port_num].rxpacket_[_idx] == 0xFF && packetData[port_num].rxpacket_[_idx + 1] == 0xFF) break; } if (_idx == 0) // found at the beginning of the packet { if (packetData[port_num].rxpacket_[PKT_ID] > 0xFD || // unavailable ID packetData[port_num].rxpacket_[PKT_LENGTH] > RXPACKET_MAX_LEN || // unavailable Length packetData[port_num].rxpacket_[PKT_ERROR] >= 0x64) // unavailable Error { // remove the first byte in the packet for (_s = 0; _s < _rx_length - 1; _s++) packetData[port_num].rxpacket_[_s] = packetData[port_num].rxpacket_[1 + _s]; _rx_length -= 1; continue; } // re-calculate the exact length of the rx packet _wait_length = packetData[port_num].rxpacket_[PKT_LENGTH] + PKT_LENGTH + 1; if (_rx_length < _wait_length) { // check timeout if (IsPacketTimeout(port_num) == true) { if (_rx_length == 0) packetData[port_num].communication_result_ = COMM_RX_TIMEOUT; else packetData[port_num].communication_result_ = COMM_RX_CORRUPT; break; } else continue; } // calculate checksum for (_i = 2; _i < _wait_length - 1; _i++) // except header, checksum _checksum += packetData[port_num].rxpacket_[_i]; _checksum = ~_checksum; // verify checksum if (packetData[port_num].rxpacket_[_wait_length - 1] == _checksum) packetData[port_num].communication_result_ = COMM_SUCCESS; else packetData[port_num].communication_result_ = COMM_RX_CORRUPT; break; } else { // remove unnecessary packets for (_s = 0; _s < _rx_length - _idx; _s++) packetData[port_num].rxpacket_[_s] = packetData[port_num].rxpacket_[_idx + _s]; _rx_length -= _idx; } } else { // check timeout if (IsPacketTimeout(port_num) == true) { if (_rx_length == 0) packetData[port_num].communication_result_ = COMM_RX_TIMEOUT; else packetData[port_num].communication_result_ = COMM_RX_CORRUPT; break; } } } is_using_[port_num] = false; }