Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "vxWorks.h"
- #include "sockLib.h"
- #include "inetLib.h"
- #include "taskLib.h"
- #include "stdioLib.h"
- #include "strLib.h"
- #include "ioLib.h"
- #include "fioLib.h"
- #include "tcpExample.h"
- #include "msgQLib.h"
- #include <taskLib.h>
- #include <logLib.h>
- #include <ioLib.h>
- #include <msgQLib.h>
- #include <stdio.h>
- #include <time.h>
- unsigned char SerBuf[3],x;
- int ser_fd;
- int dir;
- int velocity= 0xed;
- int speed = 1;
- void SendCommand();
- void BlowTrainHorn();
- void Start();
- void Accelerate();
- void Decelerate();
- void Stop();
- void LightsOn();
- void LightsOff();
- #define ENGINE_ADDRESS 1
- int trainStopped=1;
- int trainStarted=0;
- time_t t1, t2;
- void tcpServerWorkTask (int sFd, char * address, u_short port);
- STATUS tcpServer (void) {
- struct sockaddr_in serverAddr; /* server's socket address */
- struct sockaddr_in clientAddr; /* client's socket address */
- int sockAddrSize; /* size of socket address structure */
- int sFd; /* socket file descriptor */
- int newFd; /* socket descriptor from accept */
- int ix = 0; /* counter for work task names */
- char workName[16]; /* name of work task */
- char sensorData;
- printf("TCP PCPCPCPCP PCAT CPTP");
- /* set up the local address */
- sockAddrSize = sizeof (struct sockaddr_in);
- bzero ((char *) &serverAddr, sockAddrSize);
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_len = (u_char) sockAddrSize;
- serverAddr.sin_port = htons (SERVER_PORT_NUM);
- serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);
- /* create a TCP-based socket */
- if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR){
- perror ("socket");
- return (ERROR);
- }
- /* bind socket to local address */
- if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR){
- perror ("bind");
- close (sFd);
- return (ERROR);
- }
- /* create queue for client connection requests */
- if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR){
- perror ("listen");
- close (sFd);
- return (ERROR);
- }
- /* accept new connect requests and spawn tasks to process them */
- FOREVER{
- if ((newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR) {
- perror ("accept");
- close (sFd);
- return (ERROR);
- }
- sprintf (workName, "tTcpWork%d", ix++);
- printf("set up connection with tornado server\n");
- while(1){
- read(newFd,&sensorData, sizeof(int));
- if(sensorData <30 && trainStopped==0){
- printf("\nArrival mode activated\n");
- LightsOn();
- sleep(1);
- Decelerate();
- sleep(1);
- Stop();
- sleep(1);
- printf("Waiting for 60 secs\n");
- sleep(60);
- }
- if(trainStopped==1 && trainStarted==0){
- printf("\nDeparture mode activated\n");
- blowTrainHorn();
- sleep(1);
- Start();
- sleep(1);
- Accelerate();
- sleep(1);
- LightsOff();
- trainStarted=1;
- }
- if(sensorData>60){
- trainStopped=0;
- }
- }
- }
- }
- void init(void){
- /* Open Serial Port */
- ser_fd = open("/tyCo/1",O_WRONLY, 0);
- printf("\n train id %d",ENGINE_ADDRESS);
- if( ser_fd == -1)
- printf("\nSerial Port Not Open, error\n",ser_fd,0,0,0,0,0);
- else
- printf("\nSerial Port Open, fd = %d \n",ser_fd,0,0,0,0,0);
- }
- int main(void){}
- void SendCommand(){
- int numByteWritten = 0;
- SerBuf[0] = (unsigned char)254;
- SerBuf[1] = (unsigned char)0;
- numByteWritten = write(ser_fd, SerBuf, 3);
- }
- int BlowTrainHorn(){
- SerBuf[2] = 0x9c;
- SendCommand();
- }
- /*need a start command, lightson, lightsoff
- int Start(){
- SerBuf[2] = ;
- SendCommand();
- }
- int LightsOn(){
- SerBuf[2] = ;
- SendCommand();
- }
- int LightsOff(){
- SerBuf[2] = ;
- SendCommand();
- }
- */
- int Stop(){
- SerBuf[2] = 0xE0;
- SendCommand();
- }
- int Decelerate(){
- SerBuf[2] = 0xC3;
- SendCommand();
- }
- int Accelerate(){
- SerBuf[2] = 0xCF;
- SendCommand();
- }
Add Comment
Please, Sign In to add comment