Skip to content

Commit

Permalink
Windows application can now connect both via COM and TCPO
Browse files Browse the repository at this point in the history
  • Loading branch information
irekzielinski committed Jun 14, 2016
1 parent 004e82f commit bd9a125
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Libraries/PMax/pmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions Libraries/PMax/pmax.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions PowerMaxEsp8266/PowerMaxEsp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
44 changes: 41 additions & 3 deletions PowerMaxWin/PowerMax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>

#include <windows.h>
#include <conio.h>
Expand All @@ -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))
{
Expand Down Expand Up @@ -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[])
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -197,7 +235,7 @@ int _tmain(int argc, _TCHAR* argv[])
}*/
}

os_serialPortClose();
os_pmComPortClose();
return 0;
}

8 changes: 4 additions & 4 deletions PowerMaxWin/pmax_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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);
Expand Down
Loading

0 comments on commit bd9a125

Please sign in to comment.