Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO.Ports;
- using Windows.Storage.Streams;
- using Windows.Devices.SerialCommunication;
- namespace testingAgainV2
- {
- using testingAgainV2.serialPortInfo;
- using static testingAgainV2.Helpers.Enums;
- public static class Globals
- {
- public static SerialPort _serialPort = new SerialPort(SerialPort.GetPortNames()[0], 19200, Parity.None, 8, StopBits.One)
- {
- Handshake = Handshake.None,
- ReadTimeout = 2000,
- WriteTimeout = 2000
- };
- }
- class Programme
- {
- public static void Main()
- {
- SerialPort port = new SerialPort(portName: "COM6", 19200, Parity.None, 8, StopBits.One);
- port.Open();
- Outgoing outgoing = new Outgoing();
- outgoing.LED_GREEN = false;
- outgoing.LED_RED = false;
- Write(port, outgoing);
- while (true)
- {
- Incoming inbase = new Incoming();
- Incoming incoming = new Incoming();
- inbase = Read(port, incoming);
- Outgoing outbase = new Outgoing();
- outbase.LED_RED = false;
- outbase.LED_GREEN = true;
- outbase.LED1 = true;
- outbase.throttle1 = inbase.throttle1;
- Write(port, outbase);
- }
- }
- public static Incoming Read(SerialPort port, Incoming inbase)
- {
- byte[] buffer = new byte[15];
- byte[] data = new byte[15];
- int i = 0;
- while (i < 15)
- {
- data[i] = (byte)port.ReadByte();
- i++;
- }
- byte status = (byte)data[0];
- byte handset1 = (byte)data[1];
- //Console.WriteLine("Status byte: " + Convert.ToString(handset1, 2).PadLeft(8, '0'));
- byte handset2 = (byte)data[2];
- byte handset3 = (byte)data[3];
- byte handset4 = (byte)data[4];
- byte handset5 = (byte)data[5];
- byte handset6 = (byte)data[6];
- byte amps = (byte)data[7];
- byte sf = (byte)data[8];
- int time = data[9];
- byte checksum = (byte)data[13];
- inbase.connected1 = Convert.ToBoolean(status & 0x02);
- inbase.connected2 = Convert.ToBoolean(status & 0x04);
- inbase.connected3 = Convert.ToBoolean(status & 0x08);
- inbase.connected4 = Convert.ToBoolean(status & 0x10);
- inbase.connected5 = Convert.ToBoolean(status & 0x20);
- inbase.connected6 = Convert.ToBoolean(status & 0x40);
- inbase.trackPowered = Convert.ToBoolean(status & 0x01);
- inbase.brake1 = Convert.ToBoolean(~handset1 & 0x80);
- inbase.brake2 = Convert.ToBoolean(~handset2 & 0x80);
- inbase.brake3 = Convert.ToBoolean(~handset3 & 0x80);
- inbase.brake4 = Convert.ToBoolean(~handset4 & 0x80);
- inbase.brake5 = Convert.ToBoolean(~handset5 & 0x80);
- inbase.brake6 = Convert.ToBoolean(~handset6 & 0x80);
- inbase.laneChange1 = Convert.ToBoolean(~handset1 & 0x40);
- inbase.laneChange2 = Convert.ToBoolean(~handset2 & 0x40);
- inbase.laneChange3 = Convert.ToBoolean(~handset3 & 0x40);
- inbase.laneChange4 = Convert.ToBoolean(~handset4 & 0x40);
- inbase.laneChange5 = Convert.ToBoolean(~handset5 & 0x40);
- inbase.laneChange6 = Convert.ToBoolean(~handset6 & 0x40);
- inbase.throttle1 = ~handset1 & 0x3f;
- inbase.throttle2 = ~handset2 & 0x3f;
- inbase.throttle3 = ~handset3 & 0x3f;
- inbase.throttle4 = ~handset4 & 0x3f;
- inbase.throttle5 = ~handset5 & 0x3f;
- inbase.throttle6 = ~handset6 & 0x3f;
- //Console.WriteLine("Power: " + inbase.throttle1.ToString() + "; Brake: " + inbase.brake1 + "; Lane Change: " + inbase.laneChange1);
- inbase.carID = sf & 0x03;
- inbase.lapTime = time * 0.0000064;
- return inbase;
- }
- public static void Write(SerialPort port, Outgoing outbase)
- {
- outbase.operationMode = (byte)0x3F;
- outbase.driverStatus1 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange1 ? 0x40 : 0x00) |
- (Convert.ToByte(outbase.throttle1) & 0x3F));
- outbase.driverStatus2 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange2 ? 0x40 : 0x00) |
- (Convert.ToByte(outbase.throttle2) & 0x3F));
- outbase.driverStatus3 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange3 ? 0x40 : 0x00) |
- (Convert.ToByte(outbase.throttle3) & 0x3F));
- outbase.driverStatus4 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange4 ? 0x40 : 0x00) |
- (Convert.ToByte(outbase.throttle4) & 0x3F));
- outbase.driverStatus5 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange5 ? 0x40 : 0x00) |
- (Convert.ToByte(outbase.throttle5) & 0x3F));
- outbase.driverStatus6 = (byte)~((outbase.brake1 ? 0x80 : 0x00) | (outbase.laneChange6 ? 0x40 : 0x00) |
- (Convert.ToByte(outbase.throttle6) & 0x3F));
- outbase.LED_STATUS = (byte)((outbase.LED_GREEN ? 0x80 : 0x00) | (outbase.LED_RED ? 0x40 : 0x00) |
- (outbase.LED6 ? 0x20 : 0x00) | (outbase.LED5 ? 0x10 : 0x00) | (outbase.LED4 ? 0x08 : 0x00) |
- (outbase.LED3 ? 0x04 : 0x00) | (outbase.LED2 ? 0x02 : 0x00) | (outbase.LED1 ? 0x01 : 0x00));
- //Console.WriteLine("LED STATUS: " + Convert.ToString(outbase.LED_STATUS,2).PadLeft(8, '0'));
- byte crc8Rx = 0;
- byte[] outgoingArray = {outbase.operationMode, outbase.driverStatus1, outbase.driverStatus2,
- outbase.driverStatus3, outbase.driverStatus4, outbase.driverStatus5, outbase.driverStatus6,
- outbase.LED_STATUS, crc8Rx};
- crc8Rx = ByteTable.CRC8_LOOK_UP_TABLE[outgoingArray[0]]; //first byte
- for (int i = 1; i <= 7; i++) //loop for 14 times for incoming packet
- {
- crc8Rx = ByteTable.CRC8_LOOK_UP_TABLE[crc8Rx ^ outgoingArray[i]];
- }
- outgoingArray[8] = (byte)crc8Rx;
- //foreach (byte x in outgoingArray) { Console.WriteLine(Convert.ToString(x, 2).PadLeft(8, '0')); }
- //Console.ReadLine();
- //Console.WriteLine(BitConverter.ToString(outgoingArray));
- port.Write(outgoingArray, 0, 9);
- //port.WriteLine(outgoingArray.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement