| 1 | // mbxfel.cpp: implementation de la classe CMb
|
|---|
| 2 | //
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | #include "mbxfel.h"
|
|---|
| 7 | /*
|
|---|
| 8 | CMb::CMb()//constructeur
|
|---|
| 9 | :CRemoteModbus() //nom de l'instance mais c'est un appel implicite
|
|---|
| 10 | {
|
|---|
| 11 |
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | CMb::CMb(CString sHost,WORD nPort,DWORD nrTimeout)//constructeur
|
|---|
| 15 | :CRemoteModbus(sHost,nPort,nrTimeout)//appel CRemoteModbus::CRemoteModbus(sHost,nPort,nrTimeout)
|
|---|
| 16 | {
|
|---|
| 17 |
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | CMb::~CMb()//destructeur
|
|---|
| 21 | {
|
|---|
| 22 | // closeConnection();
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | CString CMb::errorMessage(WORD errorCode)
|
|---|
| 26 | {
|
|---|
| 27 | CString sRetValue("");
|
|---|
| 28 |
|
|---|
| 29 | return sRetValue;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | BOOL CMb::openConnection(CString sHost, WORD nPort, DWORD nrTimeOut)
|
|---|
| 33 | {
|
|---|
| 34 |
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | BOOL CMb::closeConnection()
|
|---|
| 38 | {
|
|---|
| 39 |
|
|---|
| 40 | }
|
|---|
| 41 | */
|
|---|
| 42 |
|
|---|
| 43 | CMb::CMb()//constructeur
|
|---|
| 44 | {
|
|---|
| 45 | serverHost = "127.0.0.1";
|
|---|
| 46 | numberPort = 502;
|
|---|
| 47 | noResponseTimeout = 3000;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | CMb::CMb(String* sHost,int nPort,unsigned int nrTimeout)//constructeur
|
|---|
| 51 | {
|
|---|
| 52 | serverHost = sHost;
|
|---|
| 53 | numberPort = nPort;
|
|---|
| 54 | noResponseTimeout = nrTimeout;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | CMb::~CMb()//destructeur
|
|---|
| 58 | {
|
|---|
| 59 | closeConnection();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | String* CMb::errorMessage(int errorCode)
|
|---|
| 63 | {
|
|---|
| 64 | String* sRetValue("");
|
|---|
| 65 |
|
|---|
| 66 | return sRetValue;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | bool CMb::openConnection(String* sHost, int nPort, String* message)
|
|---|
| 70 | {
|
|---|
| 71 | /*
|
|---|
| 72 | try {
|
|---|
| 73 | // creation TcpClient.
|
|---|
| 74 | // le serveur doit lancé.
|
|---|
| 75 | client = new TcpClient(sHost, nPort);
|
|---|
| 76 |
|
|---|
| 77 | // convertit le message en ascii et on le met dans un tableau
|
|---|
| 78 | Byte data[] = Text::Encoding::ASCII->GetBytes(message);
|
|---|
| 79 |
|
|---|
| 80 | // Retourne le NetworkStream utilisé pour l'envoi et
|
|---|
| 81 | // la réception de données
|
|---|
| 82 | NetworkStream* stream = client->GetStream();
|
|---|
| 83 |
|
|---|
| 84 | // envoie le message au TcpServer.
|
|---|
| 85 | stream->Write(data, 0, data->Length);
|
|---|
| 86 |
|
|---|
| 87 | Console::WriteLine(S"Sent: {0}", message);
|
|---|
| 88 |
|
|---|
| 89 | // Reponse du TcpServer.
|
|---|
| 90 |
|
|---|
| 91 | // Buffer pour la réponse.
|
|---|
| 92 | data = new Byte[256];
|
|---|
| 93 | // pour memoriser la reponse en ascii
|
|---|
| 94 | String* responseData = String::Empty;
|
|---|
| 95 |
|
|---|
| 96 | // lecture des octets venant du TcpServer.
|
|---|
| 97 | Int32 bytes = stream->Read(data, 0, data->Length);
|
|---|
| 98 | responseData = Text::Encoding::ASCII->GetString(data, 0, bytes);
|
|---|
| 99 | Console::WriteLine(S"Received: {0}", responseData);
|
|---|
| 100 |
|
|---|
| 101 | } catch (ArgumentNullException* e) {
|
|---|
| 102 | Console::WriteLine(S"ArgumentNullException: {0}", e);
|
|---|
| 103 | } catch (SocketException* e) {
|
|---|
| 104 | Console::WriteLine(S"SocketException: {0}", e);
|
|---|
| 105 | }
|
|---|
| 106 | */
|
|---|
| 107 |
|
|---|
| 108 | // ctx = modbus_new_tcp(sHost, nPort);
|
|---|
| 109 | Console::WriteLine(S"CMb::openConnection : modbus_new_tcp...");
|
|---|
| 110 | // ctx = modbus_new_tcp("134.158.91.254", 502);//JCM
|
|---|
| 111 |
|
|---|
| 112 | // modbus_set_debug(ctx, 1);
|
|---|
| 113 |
|
|---|
| 114 | Console::WriteLine(S"Press Enter to continue...");
|
|---|
| 115 | // Console::Read();
|
|---|
| 116 |
|
|---|
| 117 | return true;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | bool CMb::closeConnection()
|
|---|
| 121 | {
|
|---|
| 122 | // Close everything
|
|---|
| 123 | // client->Close();
|
|---|
| 124 |
|
|---|
| 125 | // modbus_free(ctx);
|
|---|
| 126 |
|
|---|
| 127 | return true;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void CMb::setServerHost(String* sHost)
|
|---|
| 131 | {
|
|---|
| 132 | serverHost = sHost;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | void CMb::setNumberPort(int nPort)
|
|---|
| 136 | {
|
|---|
| 137 | numberPort = nPort;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | void CMb::setNrTimeout(unsigned nrTimeout)
|
|---|
| 141 | {
|
|---|
| 142 | noResponseTimeout = nrTimeout;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|