Guest User

Untitled

a guest
Apr 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include "vxWorks.h"
  2. #include "sockLib.h"
  3. #include "inetLib.h"
  4. #include "taskLib.h"
  5. #include "stdioLib.h"
  6. #include "strLib.h"
  7. #include "ioLib.h"
  8. #include "fioLib.h"
  9. #include "tcpExample.h"
  10. #include "msgQLib.h"
  11. #include <taskLib.h>
  12. #include <logLib.h>
  13. #include <ioLib.h>
  14. #include <msgQLib.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17.  
  18. unsigned char SerBuf[3],x;
  19. int ser_fd;
  20. int dir;
  21. int velocity= 0xed;
  22. int speed = 1;
  23.  
  24. void SendCommand();
  25. void BlowTrainHorn();
  26. void Start();
  27. void Accelerate();
  28. void Decelerate();
  29. void Stop();
  30. void LightsOn();
  31. void LightsOff();
  32.  
  33. #define ENGINE_ADDRESS 1
  34. int trainStopped=1;
  35. int trainStarted=0;
  36. time_t t1, t2;
  37.  
  38. void tcpServerWorkTask (int sFd, char * address, u_short port);
  39.  
  40. STATUS tcpServer (void) {
  41.     struct sockaddr_in  serverAddr;    /* server's socket address */
  42.     struct sockaddr_in  clientAddr;    /* client's socket address */
  43.     int                 sockAddrSize;  /* size of socket address structure */
  44.     int                 sFd;           /* socket file descriptor */
  45.     int                 newFd;         /* socket descriptor from accept */
  46.     int                 ix = 0;        /* counter for work task names */
  47.     char                workName[16];  /* name of work task */
  48.      
  49.     char sensorData;
  50.     printf("TCP PCPCPCPCP PCAT CPTP");
  51.   /* set up the local address */
  52.     sockAddrSize = sizeof (struct sockaddr_in);
  53.     bzero ((char *) &serverAddr, sockAddrSize);
  54.     serverAddr.sin_family = AF_INET;
  55.     serverAddr.sin_len = (u_char) sockAddrSize;
  56.     serverAddr.sin_port = htons (SERVER_PORT_NUM);
  57.     serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);
  58.     /* create a TCP-based socket */
  59.     if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR){
  60.         perror ("socket");
  61.         return (ERROR);
  62.         }
  63.     /* bind socket to local address */
  64.     if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR){
  65.         perror ("bind");
  66.         close (sFd);
  67.         return (ERROR);
  68.         }
  69.     /* create queue for client connection requests */
  70.     if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR){
  71.         perror ("listen");
  72.         close (sFd);
  73.         return (ERROR);
  74.         }
  75.     /* accept new connect requests and spawn tasks to process them */
  76.     FOREVER{
  77.     if ((newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR) {
  78.     perror ("accept");
  79.     close (sFd);
  80.     return (ERROR);
  81.     }
  82.  
  83.     sprintf (workName, "tTcpWork%d", ix++);
  84.     printf("set up connection with tornado server\n");
  85.    
  86.         while(1){
  87.             read(newFd,&sensorData, sizeof(int));      
  88.             if(sensorData <30 && trainStopped==0){
  89.                 printf("\nArrival mode activated\n");
  90.                 LightsOn();
  91.                 sleep(1);
  92.                 Decelerate();
  93.                 sleep(1);
  94.                 Stop();
  95.                 sleep(1);
  96.                 printf("Waiting for 60 secs\n");
  97.                 sleep(60);
  98.             }
  99.             if(trainStopped==1 && trainStarted==0){
  100.                 printf("\nDeparture mode activated\n");
  101.                 blowTrainHorn();
  102.                 sleep(1);
  103.                 Start();
  104.                 sleep(1);
  105.                 Accelerate();
  106.                 sleep(1);
  107.                 LightsOff();
  108.                 trainStarted=1;
  109.             }
  110.             if(sensorData>60){
  111.                 trainStopped=0;
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. void init(void){
  118.      /* Open Serial Port */
  119.     ser_fd =  open("/tyCo/1",O_WRONLY, 0);
  120.     printf("\n train id %d",ENGINE_ADDRESS);
  121.     if( ser_fd == -1)
  122.             printf("\nSerial Port Not Open, error\n",ser_fd,0,0,0,0,0);
  123.     else
  124.             printf("\nSerial Port Open, fd = %d \n",ser_fd,0,0,0,0,0);
  125. }
  126.  
  127. int main(void){}
  128.        
  129. void SendCommand(){
  130.     int numByteWritten = 0;
  131.     SerBuf[0] = (unsigned char)254;
  132.     SerBuf[1] = (unsigned char)0;
  133.     numByteWritten = write(ser_fd, SerBuf, 3); 
  134. }
  135.  
  136. int BlowTrainHorn(){
  137.     SerBuf[2] = 0x9c;
  138.     SendCommand();
  139. }
  140.  
  141. /*need a start command, lightson, lightsoff
  142. int Start(){
  143.     SerBuf[2] = ;
  144.     SendCommand();
  145. }
  146.  
  147. int LightsOn(){
  148.     SerBuf[2] = ;
  149.     SendCommand();
  150. }
  151.  
  152. int LightsOff(){
  153.     SerBuf[2] = ;
  154.     SendCommand();
  155. }
  156. */
  157. int Stop(){
  158.     SerBuf[2] = 0xE0;
  159.     SendCommand();
  160. }
  161.  
  162. int Decelerate(){
  163.     SerBuf[2] = 0xC3;
  164.     SendCommand();
  165. }
  166.  
  167. int Accelerate(){
  168.     SerBuf[2] = 0xCF;
  169.     SendCommand();
  170. }
Add Comment
Please, Sign In to add comment