From bd9a12502114d8abe4b3d0502595632baf359bbe Mon Sep 17 00:00:00 2001 From: irekzielinski Date: Tue, 14 Jun 2016 21:55:51 +0100 Subject: [PATCH] Windows application can now connect both via COM and TCPO --- Libraries/PMax/pmax.cpp | 2 +- Libraries/PMax/pmax.h | 8 +- PowerMaxEsp8266/PowerMaxEsp8266.ino | 10 +-- PowerMaxWin/PowerMax.cpp | 44 +++++++++- PowerMaxWin/pmax_linux.cpp | 8 +- PowerMaxWin/pmax_windows.cpp | 132 ++++++++++++++-------------- 6 files changed, 121 insertions(+), 83 deletions(-) diff --git a/Libraries/PMax/pmax.cpp b/Libraries/PMax/pmax.cpp index 0201524..05e8781 100644 --- a/Libraries/PMax/pmax.cpp +++ b/Libraries/PMax/pmax.cpp @@ -1483,7 +1483,7 @@ bool PowerMaxAlarm::sendBuffer(const unsigned char * data, int bufferSize) writeBuffer.buffer[2+bufferSize]=0x0A; writeBuffer.size=bufferSize+3; - const int bytesWritten = os_serialPortWrite(writeBuffer.buffer,bufferSize+3); + const int bytesWritten = os_pmComPortWrite(writeBuffer.buffer,bufferSize+3); if(bytesWritten == bufferSize+3) { DEBUG(LOG_DEBUG,"serial write OK"); diff --git a/Libraries/PMax/pmax.h b/Libraries/PMax/pmax.h index abefbc6..e61ec20 100644 --- a/Libraries/PMax/pmax.h +++ b/Libraries/PMax/pmax.h @@ -321,10 +321,10 @@ class PowerMaxAlarm #define DEBUG_RAW(x,...) os_debugLog(x, true,__FUNCTION__,__LINE__,__VA_ARGS__); int log_console_setlogmask(int mask); -bool os_serialPortInit(const char* portName); -int os_serialPortRead(void* writePos, int bytesToRead); -int os_serialPortWrite(const void* dataToWrite, int bytesToWrite); -bool os_serialPortClose(); +bool os_pmComPortInit(const char* portName); +int os_pmComPortRead(void* writePos, int bytesToRead); +int os_pmComPortWrite(const void* dataToWrite, int bytesToWrite); +bool os_pmComPortClose(); void os_usleep(int microseconds); int os_cfg_getPacketTimeout(); diff --git a/PowerMaxEsp8266/PowerMaxEsp8266.ino b/PowerMaxEsp8266/PowerMaxEsp8266.ino index c28c71b..7a00de3 100644 --- a/PowerMaxEsp8266/PowerMaxEsp8266.ino +++ b/PowerMaxEsp8266/PowerMaxEsp8266.ino @@ -394,7 +394,7 @@ bool serialHandler(PowerMaxAlarm* pm) { memset(&commandBuffer, 0, sizeof(commandBuffer)); char oneByte = 0; - while ( (os_serialPortRead(&oneByte, 1) == 1) ) + while ( (os_pmComPortRead(&oneByte, 1) == 1) ) { if (commandBuffer.size<(MAX_BUFFER_SIZE-1)) { @@ -525,7 +525,7 @@ unsigned long os_getCurrentTimeSec() return (wrapCnt*4294967) + seconds; } -int os_serialPortRead(void* readBuff, int bytesToRead) +int os_pmComPortRead(void* readBuff, int bytesToRead) { int dwTotalRead = 0; while(bytesToRead > 0) @@ -553,18 +553,18 @@ int os_serialPortRead(void* readBuff, int bytesToRead) return dwTotalRead; } -int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) +int os_pmComPortWrite(const void* dataToWrite, int bytesToWrite) { Serial.write((const uint8_t*)dataToWrite, bytesToWrite); return bytesToWrite; } -bool os_serialPortClose() +bool os_pmComPortClose() { return true; } -bool os_serialPortInit(const char* portName) { +bool os_pmComPortInit(const char* portName) { return true; } diff --git a/PowerMaxWin/PowerMax.cpp b/PowerMaxWin/PowerMax.cpp index 889fbb0..e025965 100644 --- a/PowerMaxWin/PowerMax.cpp +++ b/PowerMaxWin/PowerMax.cpp @@ -4,6 +4,7 @@ #include "targetver.h" #include #include +#include #include #include @@ -17,7 +18,7 @@ bool serialHandler(PowerMaxAlarm* pm) { memset(&commandBuffer, 0, sizeof(commandBuffer)); char byte = 0; - while ( (os_serialPortRead(&byte, 1) == 1) ) { + while ( (os_pmComPortRead(&byte, 1) == 1) ) { if (commandBuffer.size<(MAX_BUFFER_SIZE-1)) { @@ -154,6 +155,40 @@ void loadMapToFile(const char* name, MemoryMap* map) CloseHandle(hDump); } +extern bool g_useComPort; + +void askUserForSettings(char* port, int portSize) +{ + char tmp[10] = ""; + std::cout << "This application can work in wired mode (using COM port) or via TCP/IP to ESP8266 running PMAX\r\n"; + + while(strcmp(tmp, "1") != 0 && strcmp(tmp, "2") != 0) + { + std::cout << "Press 1 for COM\r\nPress 2 for TCP/IP\r\n"; + std::cin.getline(tmp,10); + } + + if(strcmp(tmp, "1") == 0) + { + g_useComPort = true; + std::cout << "Specify COM port name to open, for example: COM3\r\n"; + } + else if(strcmp(tmp, "2") == 0) + { + g_useComPort = false; + std::cout << "Specify host to connect using IP or host name:\r\n"; + } + + std::cin.getline(port,portSize); + + //just some default for debugging + if(g_useComPort == false && strlen(port) == 0) + { + strcpy(port, "192.168.0.119"); + } + + std::cout << "After connection is established and handshake complete press ? for list of commands.\r\n"; +} int _tmain(int argc, _TCHAR* argv[]) { @@ -163,7 +198,10 @@ int _tmain(int argc, _TCHAR* argv[]) } return 0;*/ - if(os_serialPortInit("COM3") == false) + char szPort[512]; + askUserForSettings(szPort, sizeof(szPort)); + + if(os_pmComPortInit(szPort) == false) { return 1; } @@ -197,7 +235,7 @@ int _tmain(int argc, _TCHAR* argv[]) }*/ } - os_serialPortClose(); + os_pmComPortClose(); return 0; } diff --git a/PowerMaxWin/pmax_linux.cpp b/PowerMaxWin/pmax_linux.cpp index 79e323b..3c08b19 100644 --- a/PowerMaxWin/pmax_linux.cpp +++ b/PowerMaxWin/pmax_linux.cpp @@ -106,7 +106,7 @@ unsigned long os_getCurrentTimeSec() return (unsigned long) time(NULL); } -int os_serialPortRead(void* readBuff, int bytesToRead) +int os_pmComPortRead(void* readBuff, int bytesToRead) { if(fd == 0) { @@ -117,7 +117,7 @@ int os_serialPortRead(void* readBuff, int bytesToRead) return read(fd, readBuff, bytesToRead); } -int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) +int os_pmComPortWrite(const void* dataToWrite, int bytesToWrite) { if(fd == 0) { @@ -128,7 +128,7 @@ int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) return write(fd, dataToWrite, bytesToWrite); } -bool os_serialPortClose() +bool os_pmComPortClose() { if(fd == 0) { @@ -142,7 +142,7 @@ bool os_serialPortClose() return true; } -bool os_serialPortInit(const char* portName) { +bool os_pmComPortInit(const char* portName) { struct termios options; fd = open(portName, O_RDWR | O_NOCTTY); diff --git a/PowerMaxWin/pmax_windows.cpp b/PowerMaxWin/pmax_windows.cpp index 3fff228..0ff065f 100644 --- a/PowerMaxWin/pmax_windows.cpp +++ b/PowerMaxWin/pmax_windows.cpp @@ -4,12 +4,18 @@ #include /* POSIX terminal control definitions */ #include +//Don't forget to link libws2_32.a to your program as well +#include + //This file contains OS specific implementation for Windows //If you build for other platrorms (like Linux, don't include this file, but provide your own) -#ifdef USE_SERIAL +bool g_useComPort = true; + static HANDLE g_hCommPort = INVALID_HANDLE_VALUE; -#endif +static SOCKET s = 0; //Socket handle +static HANDLE hLogFile = INVALID_HANDLE_VALUE; + /* Private storage for the current mask */ static int log_consolemask = 0xFFFF; @@ -57,8 +63,8 @@ unsigned long os_getCurrentTimeSec() return (unsigned long) time(NULL); } -#ifdef USE_SERIAL -int os_serialPortRead(void* readBuff, int bytesToRead) + +int com_pmComPortRead(void* readBuff, int bytesToRead) { if(g_hCommPort == INVALID_HANDLE_VALUE) { @@ -83,7 +89,7 @@ int os_serialPortRead(void* readBuff, int bytesToRead) return dwTotalRead; } -int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) +int com_pmComPortWrite(const void* dataToWrite, int bytesToWrite) { if(g_hCommPort == INVALID_HANDLE_VALUE) { @@ -108,7 +114,7 @@ int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) return dwTotalWritten; } -bool os_serialPortClose() +bool com_pmComPortClose() { if(g_hCommPort == INVALID_HANDLE_VALUE) { @@ -122,7 +128,7 @@ bool os_serialPortClose() return true; } -bool os_serialPortInit(const char* portName) { +bool com_pmComPortInit(const char* portName) { if(g_hCommPort != INVALID_HANDLE_VALUE) { DEBUG(LOG_ERR, "Com port is already opened."); @@ -182,14 +188,6 @@ bool os_serialPortInit(const char* portName) { DEBUG(LOG_INFO, "Current Settings\n Baud Rate %d\n Parity %d\n Byte Size %d\n Stop Bits %d", config.BaudRate, config.Parity, config.ByteSize, config.StopBits); return true; } -#else -//CONNECT TO REMOTE HOST (CLIENT APPLICATION) -//Include the needed header files. -//Don't forget to link libws2_32.a to your program as well -#include -SOCKET s = 0; //Socket handle - -HANDLE hLogFile = INVALID_HANDLE_VALUE; bool readUntilNewLine(char* buffer, int buffSize) { @@ -340,51 +338,9 @@ void LogBuffer(const char* prefix, const unsigned char* buffer, int bufferLen) } } -/*static unsigned char g_readCache[MAX_BUFFER_SIZE]; -static int g_cacheSize = 0; -static int readFromCache(void* readBuff, int bytesToRead) -{ - int bytesToCopy = bytesToRead; - if(bytesToCopy >= g_cacheSize) - { - bytesToCopy = g_cacheSize; - } - - if(bytesToCopy == 0) - { - return 0; - } - memcpy(readBuff, g_readCache, bytesToCopy); - memmove(g_readCache, g_readCache+bytesToCopy, g_cacheSize-bytesToCopy); - g_cacheSize -= bytesToCopy; - - return bytesToCopy; -} - -int os_serialPortRead(void* readBuff, int bytesToRead) -{ - if(bytesToRead == 0) - { - return 0; - } - - int totalRead = readFromCache(readBuff, bytesToRead); - if(totalRead > 0) - { - return totalRead; - } - - g_cacheSize = recv(s, (char*)g_readCache, sizeof(g_readCache), 0); - if(g_cacheSize < 0) - { - g_cacheSize = 0; - } - return readFromCache(readBuff, bytesToRead); -}*/ - -int os_serialPortRead(void* readBuff, int bytesToRead) +int tcp_pmComPortRead(void* readBuff, int bytesToRead) { int totalRead = 0; while( bytesToRead > 0 ){ @@ -404,7 +360,7 @@ int os_serialPortRead(void* readBuff, int bytesToRead) return totalRead; } -int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) +int tcp_pmComPortWrite(const void* dataToWrite, int bytesToWrite) { LogBuffer("PC", (const unsigned char*)dataToWrite, bytesToWrite); @@ -426,7 +382,7 @@ int os_serialPortWrite(const void* dataToWrite, int bytesToWrite) return totalSent; } -bool os_serialPortClose() +bool tcp_pmComPortClose() { //Close the socket if it exists if (s) @@ -436,14 +392,13 @@ bool os_serialPortClose() return true; } -bool os_serialPortInit(const char* portName) { +bool tcp_pmComPortInit(const char* portName) { hLogFile = CreateFile(L"comms.log", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); SetFilePointer(hLogFile, 0, 0, FILE_END); - //IZIZTODO: int PortNo = 23; - const char* IPAddress = "192.168.0.119"; + const char* IPAddress = portName; WSADATA wsadata; int error = WSAStartup(MAKEWORD(2,2), &wsadata); @@ -515,15 +470,60 @@ bool os_serialPortInit(const char* portName) { } -#endif - - void os_strncat_s(char* dst, int dst_size, const char* src) { strncat_s(dst, dst_size, src, _TRUNCATE); } //////////////////////// +int os_pmComPortRead(void* readBuff, int bytesToRead) +{ + if(g_useComPort) + { + return com_pmComPortRead(readBuff, bytesToRead); + } + else + { + return tcp_pmComPortRead(readBuff, bytesToRead); + } +} + +int os_pmComPortWrite(const void* dataToWrite, int bytesToWrite) +{ + if(g_useComPort) + { + return com_pmComPortWrite(dataToWrite, bytesToWrite); + } + else + { + return tcp_pmComPortWrite(dataToWrite, bytesToWrite); + } +} + +bool os_pmComPortClose() +{ + if(g_useComPort) + { + return com_pmComPortClose(); + } + else + { + return tcp_pmComPortClose(); + } +} + +bool os_pmComPortInit(const char* portName) +{ + if(g_useComPort) + { + return com_pmComPortInit(portName); + } + else + { + return tcp_pmComPortInit(portName); + } +} + int os_cfg_getPacketTimeout() {