/* * Lumencor Light Engine * UDP test program * Sends sequence of commands to Light Engine via UDP socket * to turn lights on and off, with varying intensities. * Displays command timing. * * Lumencor Inc, 2015 */ #include <stdio.h> #include <winsock2.h> #include <string> #include <chrono> #include <iostream> using namespace std; #pragma comment(lib,"ws2_32.lib") //Winsock Library // Send string command to UDP socket // // socket - socket handle // saddr - sockaddr_in data structure (socket info) // cmd - command // response - string returned from the server in response to the command int executeCommand(int socket, sockaddr_in* saddr, string& cmd, string& response) { const int maxBuf = 512; char buf[maxBuf]; memset(buf, '\0', maxBuf); cout << "Command " << cmd << endl; int slen = sizeof(sockaddr_in); auto begin = std::chrono::high_resolution_clock::now(); // write command to UDP socket if (sendto(socket, cmd.c_str(), cmd.size(), 0, (struct sockaddr *) saddr, slen) == SOCKET_ERROR) { int err = WSAGetLastError(); printf("Socket write failed with code %d\n", err); return err; } // receive response from the server // recvrfom blocks until answer is received if (recvfrom(socket, buf, maxBuf, 0, (struct sockaddr *) saddr, &slen) == SOCKET_ERROR) { int err = WSAGetLastError(); printf("Socket read failed with code %d\n", err); return err; } auto end = std::chrono::high_resolution_clock::now(); response = buf; cout << "Response: " << response << ", in " << chrono::duration_cast<chrono::microseconds>(end - begin).count() << " us" << endl; return 0; } // error handling int exitWithError(int socket) { closesocket(socket); WSACleanup(); exit(EXIT_FAILURE); } // main routine int main(int argc, char* argv[]) { char server[1024] = "192.168.1.162"; //ip address of udp server unsigned short port = 8095; // port struct sockaddr_in si_other; int s, slen = sizeof(si_other); WSADATA wsa; if (argc < 3) { printf("Invalid number of parameters\nUsage: udptest <IP address> <port>\n"); exit(EXIT_FAILURE); } strcpy(server, argv[1]); port = (unsigned short) atoi(argv[2]); //Initialise winsock printf("\nInitialising socket %s : %s...", argv[1], argv[2]); if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { printf("Failed. Error Code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } printf("\nOK.\n"); //create socket if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) { printf("socket() failed with error code : %d", WSAGetLastError()); exit(EXIT_FAILURE); } // timeout DWORD timeoutMs = 500; if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeoutMs, sizeof(timeoutMs)) < 0) { printf("setsockopt() failed with error code : %d", WSAGetLastError()); closesocket(s); WSACleanup(); exit(EXIT_FAILURE); } //setup address structure memset((char *)&si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(port); si_other.sin_addr.S_un.S_addr = inet_addr(server); // start sequence // Obtain general info string response; int err = executeCommand(s, &si_other, string("GET IP"), response); if (err != 0) exitWithError(s); err = executeCommand(s, &si_other, string("GET MODEL"), response); if (err != 0) exitWithError(s); err = executeCommand(s, &si_other, string("GET SN"), response); if (err != 0) exitWithError(s); err = executeCommand(s, &si_other, string("GET CHMAP"), response); if (err != 0) exitWithError(s); // Set intensities for the first three lights, others at 0 err = executeCommand(s, &si_other, string("SET MULCHINT 450 500 550 0 0"), response); if (err != 0) exitWithError(s); // turn each light ON for one second err = executeCommand(s, &si_other, string("SET MULCH 1 0 0 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); err = executeCommand(s, &si_other, string("SET MULCH 0 1 0 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); err = executeCommand(s, &si_other, string("SET MULCH 0 0 1 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); // turn on all lights and modulate intensities err = executeCommand(s, &si_other, string("SET MULCH 1 1 1 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); err = executeCommand(s, &si_other, string("SET MULCHINT 300 2000 1000 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); err = executeCommand(s, &si_other, string("SET MULCHINT 3000 300 2000 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); err = executeCommand(s, &si_other, string("SET MULCHINT 1000 300 4000 0 0"), response); if (err != 0) exitWithError(s); Sleep(1000); // Turn everything off err = executeCommand(s, &si_other, string("SET MULCH 0 0 0 0 0"), response); if (err != 0) exitWithError(s); closesocket(s); WSACleanup(); return 0; } |
Light Engine Server >