Advertisement
Guest User

mainProgramme

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO.Ports;
  7. using Windows.Storage.Streams;
  8. using Windows.Devices.SerialCommunication;
  9.  
  10. namespace testingAgainV2
  11. {
  12.     using testingAgainV2.serialPortInfo;
  13.     using static testingAgainV2.Helpers.Enums;
  14.     public static class Globals
  15.     {
  16.         public static SerialPort _serialPort = new SerialPort(SerialPort.GetPortNames()[0], 19200, Parity.None, 8, StopBits.One)
  17.         {
  18.             Handshake = Handshake.None,
  19.             ReadTimeout = 2000,
  20.             WriteTimeout = 2000
  21.         };
  22.     }
  23.     class Programme
  24.     {
  25.         public static void Main()
  26.         {
  27.             SerialPort port = new SerialPort(portName: "COM6", 19200, Parity.None, 8, StopBits.One);
  28.  
  29.             port.Open();
  30.             Outgoing outgoing = new Outgoing();
  31.             outgoing.LED_GREEN = false;
  32.             outgoing.LED_RED = false;
  33.             Write(port, outgoing);
  34.             while (true)
  35.             {
  36.                 Incoming inbase = new Incoming();
  37.                 Incoming incoming = new Incoming();
  38.                 inbase = Read(port, incoming);
  39.                 Outgoing outbase = new Outgoing();
  40.                 outbase.LED_RED = false;
  41.                 outbase.LED_GREEN = true;
  42.                 outbase.LED1 = true;
  43.                 outbase.throttle1 = inbase.throttle1;
  44.  
  45.                 Write(port, outbase);
  46.             }
  47.         }
  48.  
  49.         public static Incoming Read(SerialPort port, Incoming inbase)
  50.         {
  51.             byte[] buffer = new byte[15];
  52.             byte[] data = new byte[15];
  53.             int i = 0;
  54.             while (i < 15)
  55.             {
  56.                 data[i] = (byte)port.ReadByte();
  57.                 i++;
  58.             }
  59.             byte status = (byte)data[0];
  60.             byte handset1 = (byte)data[1];
  61.             //Console.WriteLine("Status byte: " + Convert.ToString(handset1, 2).PadLeft(8, '0'));
  62.             byte handset2 = (byte)data[2];
  63.             byte handset3 = (byte)data[3];
  64.             byte handset4 = (byte)data[4];
  65.             byte handset5 = (byte)data[5];
  66.             byte handset6 = (byte)data[6];
  67.             byte amps = (byte)data[7];
  68.             byte sf = (byte)data[8];
  69.             int time = data[9];
  70.             byte checksum = (byte)data[13];
  71.  
  72.             inbase.connected1 = Convert.ToBoolean(status & 0x02);
  73.             inbase.connected2 = Convert.ToBoolean(status & 0x04);
  74.             inbase.connected3 = Convert.ToBoolean(status & 0x08);
  75.             inbase.connected4 = Convert.ToBoolean(status & 0x10);
  76.             inbase.connected5 = Convert.ToBoolean(status & 0x20);
  77.             inbase.connected6 = Convert.ToBoolean(status & 0x40);
  78.  
  79.             inbase.trackPowered = Convert.ToBoolean(status & 0x01);
  80.  
  81.             inbase.brake1 = Convert.ToBoolean(~handset1 & 0x80);
  82.             inbase.brake2 = Convert.ToBoolean(~handset2 & 0x80);
  83.             inbase.brake3 = Convert.ToBoolean(~handset3 & 0x80);
  84.             inbase.brake4 = Convert.ToBoolean(~handset4 & 0x80);
  85.             inbase.brake5 = Convert.ToBoolean(~handset5 & 0x80);
  86.             inbase.brake6 = Convert.ToBoolean(~handset6 & 0x80);
  87.  
  88.             inbase.laneChange1 = Convert.ToBoolean(~handset1 & 0x40);
  89.             inbase.laneChange2 = Convert.ToBoolean(~handset2 & 0x40);
  90.             inbase.laneChange3 = Convert.ToBoolean(~handset3 & 0x40);
  91.             inbase.laneChange4 = Convert.ToBoolean(~handset4 & 0x40);
  92.             inbase.laneChange5 = Convert.ToBoolean(~handset5 & 0x40);
  93.             inbase.laneChange6 = Convert.ToBoolean(~handset6 & 0x40);
  94.  
  95.             inbase.throttle1 = ~handset1 & 0x3f;
  96.             inbase.throttle2 = ~handset2 & 0x3f;
  97.             inbase.throttle3 = ~handset3 & 0x3f;
  98.             inbase.throttle4 = ~handset4 & 0x3f;
  99.             inbase.throttle5 = ~handset5 & 0x3f;
  100.             inbase.throttle6 = ~handset6 & 0x3f;
  101.  
  102.             //Console.WriteLine("Power: " + inbase.throttle1.ToString() + "; Brake: " + inbase.brake1 + "; Lane Change: " + inbase.laneChange1);
  103.  
  104.             inbase.carID = sf & 0x03;
  105.             inbase.lapTime = time * 0.0000064;
  106.  
  107.             return inbase;
  108.         }
  109.  
  110.         public static void Write(SerialPort port, Outgoing outbase)
  111.         {
  112.            
  113.  
  114.             outbase.operationMode = (byte)0x3F;
  115.             outbase.driverStatus1 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange1 ? 0x40 : 0x00) |
  116.                 (Convert.ToByte(outbase.throttle1) & 0x3F));
  117.             outbase.driverStatus2 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange2 ? 0x40 : 0x00) |
  118.                 (Convert.ToByte(outbase.throttle2) & 0x3F));
  119.             outbase.driverStatus3 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange3 ? 0x40 : 0x00) |
  120.                 (Convert.ToByte(outbase.throttle3) & 0x3F));
  121.             outbase.driverStatus4 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange4 ? 0x40 : 0x00) |
  122.                 (Convert.ToByte(outbase.throttle4) & 0x3F));
  123.             outbase.driverStatus5 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange5 ? 0x40 : 0x00) |
  124.                 (Convert.ToByte(outbase.throttle5) & 0x3F));
  125.             outbase.driverStatus6 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange6 ? 0x40 : 0x00) |
  126.                 (Convert.ToByte(outbase.throttle6) & 0x3F));
  127.             outbase.LED_STATUS = (byte)((outbase.LED_GREEN ? 0x80 : 0x00) | (outbase.LED_RED ? 0x40 : 0x00) |
  128.                 (outbase.LED6 ? 0x20 : 0x00) | (outbase.LED5 ? 0x10 : 0x00) | (outbase.LED4 ? 0x08 : 0x00) |
  129.                 (outbase.LED3 ? 0x04 : 0x00) | (outbase.LED2 ? 0x02 : 0x00) | (outbase.LED1 ? 0x01 : 0x00));
  130.             //Console.WriteLine("LED STATUS: " + Convert.ToString(outbase.LED_STATUS,2).PadLeft(8, '0'));
  131.             byte crc8Rx = 0;
  132.             byte[] outgoingArray = {outbase.operationMode, outbase.driverStatus1, outbase.driverStatus2,
  133.                 outbase.driverStatus3, outbase.driverStatus4, outbase.driverStatus5, outbase.driverStatus6,
  134.                 outbase.LED_STATUS, crc8Rx};
  135.  
  136.             crc8Rx = ByteTable.CRC8_LOOK_UP_TABLE[outgoingArray[0]]; //first byte
  137.             for  (int i = 1; i <= 7; i++) //loop for 14 times for incoming packet
  138.             {
  139.                 crc8Rx = ByteTable.CRC8_LOOK_UP_TABLE[crc8Rx ^ outgoingArray[i]];
  140.             }
  141.  
  142.             outgoingArray[8] = (byte)crc8Rx;
  143.  
  144.             //foreach (byte x in outgoingArray) { Console.WriteLine(Convert.ToString(x, 2).PadLeft(8, '0')); }
  145.             //Console.ReadLine();
  146.  
  147.             //Console.WriteLine(BitConverter.ToString(outgoingArray));
  148.             port.Write(outgoingArray, 0, 9);
  149.             //port.WriteLine(outgoingArray.ToString());
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement