Exemple #1
0
        static void Main(string[] args)
        {
            var settings = ConfigurationManager.AppSettings;
            var port = settings["COM_PORT"];
            var speed = Convert.ToInt32(settings["COM_SPEED"]);
            var tcpPort = Convert.ToInt32(settings["TCP_PORT"]);
            serial = new SerialManager(port, speed);
            Console.WriteLine("UART: " + port + " - " + speed);
            server = new HardwareTcpServer(tcpPort);
            server.OnServerState += server_OnServerState;
            server.OnClientState += server_OnClientState;
            server.OnData += server_OnData;
            serial.OnReceive += serial_OnReceive;
            serial.OnStateChange += serial_OnStateChange;
            serial.Connect();

           // bridge = new HttpToUartBridge(6200);

            TcpListener listener = new TcpListener(IPAddress.Parse("188.127.233.35"), tcpPort);
            listener.Start();
            Console.WriteLine("TCP: " + tcpPort);
            while (Thread.CurrentThread.ThreadState == ThreadState.Running)
            {
                Console.WriteLine("Listening " + tcpPort);
                while (!listener.Pending())
                {
                    Thread.Sleep(300);
                }
                server.AcceptTcpClient(listener.AcceptSocket());
            }
            Console.WriteLine("Stopped");
            listener.Stop();
            server.Close();
            serial.Close();
        }
 public static void ProcessMessage(byte[] data, SerialManager manager)
 {
     MotorState obj = MotorState.Deserialize(data);
     WriteLog(obj);
     if (obj != null)
     {
         /*if (!initialized && LastState != null)
         {
             if (LastState.x != obj.x || LastState.y != obj.y || LastState.z != obj.z)
             {
                 Uart.SendCommand(MotorCommand.Rebase(LastState.x, LastState.y, LastState.z));
             }
         }*/
         if (OnMessage != null)
         {
             OnMessage(obj);
         }
         LastState = obj;
         /*StreamWriter writer = new StreamWriter(cfgPath + "\\config.json", false, Encoding.UTF8);
         writer.WriteLine(Uart.PortName);
         writer.WriteLine(JsonConvert.SerializeObject(obj));
         writer.Close();*/
         initialized = true;
         
     }
     else
     {
         if (data.Length == 1 && (data[0] >= 50 || data[0] < 100))
         {
             if (LastState != null)
             {
                 //SendCoordCommand(CoordMotorCommand.Rebase(LastState.x, LastState.y, LastState.z));
             }
         }
     }
 }
Exemple #3
0
 static void serial_OnReceive(byte[] data, SerialManager sm)
 {
     server.Send(data);
 }
Exemple #4
0
 static void serial_OnStateChange(UART.EDeviceState state, SerialManager sm)
 {
     Console.WriteLine("UART -> " + state);
 }
Exemple #5
0
 static void serial_OnStateChange(UART.EDeviceState state, SerialManager sm)
 {
     if (state == EDeviceState.Online || state == EDeviceState.PortOpen)
     {
         Info(sm.PortName + " -> " + state);
         return;
     }
     if (state == EDeviceState.Offline)
     {
         Serials.Remove(sm.PortName);
         Warn(sm.PortName + " -> " + state);
         return;
     }
     if (state == EDeviceState.Error)
     {
         Error(sm.PortName + " -> " + state);
         return;
     }
     Log(sm.PortName + " -> " + state);
 }
Exemple #6
0
 static void serial_OnReceive(byte[] data, SerialManager sm)
 {
     var portName = "";
     if (sm != null)
     {
         portName = sm.PortName;
     }
     if (data.Length > 40)
     {
         Log(portName + " >> " + data.Length);
     }
     else
     {
         var str = SerialPacket.SerializeData(data);
         Log(portName + " >> " + str);
     }
     var segment = getMessage(portName, SerialDirection.FromUART, data);
     if (socketServer != null && socketServer.State == SuperSocket.SocketBase.ServerState.Running)
     {
         var sessions = GetSessions(portName);
         if (sessions != null)
         {
             foreach (var session in sessions)
             {
                 session.Send(segment);
             }
         }
     }
     if (tcpService != null)
     {
         tcpService.Send(segment);
     }
 }
Exemple #7
0
        //--------------------------------------------------------------------------------------------------


        static void sm_OnSend(byte[] data, SerialManager manager)
        {
            if (data.Length > 40)
            {
                Log(manager.PortName + " << " + data.Length);
            }
            else
            {
                var str = SerialPacket.SerializeData(data);
                Log(manager.PortName + " << " + str);
            }
            var sessions = GetSessions(manager.PortName);
            if (sessions == null) return;
            var segment = getMessage(manager.PortName, SerialDirection.ToUART, data);
            foreach (var session in sessions)
            {
                session.Send(segment);
            }
            if (tcpService != null)
            {
                tcpService.Send(segment);
            }
        }
Exemple #8
0
 public fmStats(SerialManager sm)
 {
     InitializeComponent();
     manager = sm;
 }
Exemple #9
0
        protected void OpenDevice(SerialConfig cfg)
        {
            if (cfg == null) return;
            if (device != null)
            {
                device.Close();
            }
            try
            {
                switch (cfg.RxPacketType)
                {
                    case PacketType.Raw:
                        device = new SerialManager(cfg);
                        break;
/*case PacketType.Simple:
                    case PacketType.SimpleCoded:
                    case PacketType.SimpleCRC:
                    case PacketType.Sized:                    
                    case PacketType.SizedOld:
                    case PacketType.SizedCRC:
                    case PacketType.SizedCRC_old:
                    case PacketType.PacketInvariant:
                        device = new SerialPacketManager(cfg);
                        break;*/
                    case PacketType.Addressed:
                    case PacketType.AddressedOld:
                        device = new SerialAddressedManager(cfg);
                        break;
                    case PacketType.XRouting:
                        device = new XRoutingManager(cfg);
                        break;
                    default: 
                        device = new SerialPacketManager(cfg);
                        break;
                }
                if (device.Connect())
                {
                    device.OnStateChange += SelectedPort_Disposed;
                    ShowMessage(cfg.PortName + "  " + "\nConnected!");
                    ShowMessage(JsonConvert.SerializeObject(cfg));
                    EnableItems();
                }
                else
                {
                    DisableItems();
                    device.Close();
                    device = null;
                    ShowError(cfg.PortName + "Connecting error: " + device.LastError);
                }
            }
            catch (Exception err)
            {
                ShowError(cfg.PortName + "Connecting error: ", err);
            }
        }
Exemple #10
0
 protected static SerialConfig AddPort(SerialConfig cfgBase, bool autoConnect)
 {
     if (Serials.ContainsKey(cfgBase.PortName))
     {
         //Serials[cfg.PortName].SetParams((uint)cfg.Speed, cfg.DataBits, cfg.StopBits, cfg.Parity);
         if (autoConnect && !Serials[cfgBase.PortName].IsOpened)
         {
             Serials[cfgBase.PortName].Connect();
         }
         return cfgBase;
     }
     var cfg = ComPortConfigs.FirstOrDefault(s => s.PortName == cfgBase.PortName);
     SerialConfig cfgNew = null;
     SerialManager sm;
     switch (cfgBase.RxPacketType)
     {
         case PacketType.SimpleCoded:
         case PacketType.SimpleCRC: throw new NotImplementedException("PacketType.SimpleCRC have no manager today!");
         case PacketType.Sized:
             sm = new SerialPacketManager(cfgBase);
             break;
         case PacketType.SizedOld:
             sm = new SerialPacketManager(cfgBase);
             break;
         case PacketType.Addressed:
             sm = new SerialAddressedManager(cfgBase);
             (sm as SerialAddressedManager).DeviceAddr = 0xAA;
             break;
         case PacketType.AddressedOld:
             sm = new SerialAddressedManager(cfgBase);
             break;
         case PacketType.XRouting:
             /*if (cfg == null)
             {
                 cfgNew = new XRSerialConfig(cfgBase);
                 sm = new XRoutingManager(cfgNew as XRSerialConfig);
             }
             else
             {
                 sm = new XRoutingManager(cfg as XRSerialConfig);
             }   */
             sm = new XRoutingManager(cfgBase);
             (sm as XRoutingManager).DeviceAddr = 0xAB;
             
             break;
         default:
             sm = new SerialManager(cfgBase);
             break;
     }
     if (cfg == null)
     {
         if (cfgNew != null)
         {
             ComPortConfigs.Add(cfgNew);
             cfg = cfgNew;
         }
         else
         {
             ComPortConfigs.Add(cfgBase);
             cfg = cfgBase;
         }
     }
     Log(cfg);
     sm.OnReceive += serial_OnReceive;
     sm.OnSend += sm_OnSend;
     sm.OnError += serial_OnError;
     sm.OnStateChange += serial_OnStateChange;
     if (autoConnect)
     {
         if (sm.Connect())
         {
             Serials.Add(cfg.PortName, sm);
             return cfg;
         }
         else
         {
             Warn("Can't connect " + cfg.PortName);
             sm.OnReceive -= serial_OnReceive;
             sm.OnSend -= sm_OnSend;
             sm.OnError -= serial_OnError;
             sm.OnStateChange -= serial_OnStateChange;
             return null;
         }
     }
     return cfg;
 }
Exemple #11
0
 void SelectedPort_Disposed(EDeviceState state, SerialManager sm)
 {
     Invoke(DeviceStateChanged, state, sm);
 }
Exemple #12
0
 void OnDeviceStatusChanged(EDeviceState state, SerialManager sm)
 {
     ShowMessage(device.PortName + " -->  " + state);
     if (state == EDeviceState.Offline)
     {
         DisableItems();
         device = null;
     }
 }
 protected void onReceiveByte(byte data, SerialManager sm)
 {
     if (State < EDeviceState.Online) State = EDeviceState.Online;
     switch (packetState)
     {
         case PacketReadingState.free:
             if (data == receiverStart || receiverStart == 0)
             {
                 if (receiverCRCValue.HasValue)
                 {
                     crc = receiverCRCValue.Value;
                 }
                 readingIndex = 0;
                 if (data == PACKET_UNSIZED_START_BYTE)
                 {
                     packetState = PacketReadingState.dataReading;
                     receivingBuf = new byte[MAX_PACKET_DATA_SIZE];
                     return;
                 }
                 if (data == PACKET_SIZED_START_BYTE)
                 {
                     packetState = PacketReadingState.sizeWaiting;
                     return;
                 }
             }
             break;
         case PacketReadingState.sizeWaiting:
             if (data <= 0)
             {
                 packetState = PacketReadingState.free;
             }
             else
             {
                 receivingBuf = new byte[data];
                 if (data > 0){                        
                     packetState = PacketReadingState.dataReading;
                 }
                 else{
                     packetState = PacketReadingState.finishCheck;
                 }
             }
             break;
         case PacketReadingState.dataReading:                    
             if ((receiverStart == PACKET_UNSIZED_START_BYTE) && data == PACKET_END_TEXT){
                 var buf = new byte[readingIndex + 1];
                 Array.Copy(receivingBuf, 0, buf, 0, buf.Length);
                 receivingBuf = buf;
                 packetState = receiverCRCValue.HasValue ? PacketReadingState.crcCheck : PacketReadingState.finishCheck;                        
                 return;
             }
             receivingBuf[readingIndex] = data;
             crc ^= data;
             readingIndex++;   
             if (readingIndex >= receivingBuf.Length)
             {
                 packetState = receiverCRCValue.HasValue ? PacketReadingState.crcCheck : PacketReadingState.finishCheck;                        
             }
             break;
         case PacketReadingState.crcCheck:
                 if (data == crc)
                 {
                     packetState = PacketReadingState.finishCheck;
                 }
                 else
                 {
                     callError(new Exception("Wrong CRC sum!"));
                 }
                 break;
         case PacketReadingState.finishCheck:
                 packetState = PacketReadingState.free;
                 if ((receiverEnd > 0 && data == receiverEnd) || (receiverEnd == 0 && (data == PACKET_END_TRANSMIT || data == PACKET_END_TEXT))){
                     callReceive(receivingBuf);
                 }
                 else
                 {
                     callError(new Exception("Incorrect finishing byte!"));
                 }
             break;
     }
 }
Exemple #14
0
 public fmUART()
 {
     InitializeComponent();
     manager = new SerialManager();
 }
Exemple #15
0
 static void serial_OnError(Exception e, SerialManager sm)
 {
     var portName = "";
     if (sm != null)
     {
         portName = sm.PortName;
     }
     var segment = getMessage(portName, SerialDirection.Error, e.Message);
     if (socketServer != null && socketServer.State == SuperSocket.SocketBase.ServerState.Running)
     {
         Error(portName + " ERROR: " + e.Message);
         var sessions = socketServer.GetAllSessions();
         foreach (var session in sessions)
         {
             session.Send(segment);
         }
     }
     if (tcpService != null)
     {
         tcpService.Send(segment);
     }
 }
Exemple #16
0
 public fmUART(SerialManager device)
 {
     InitializeComponent();
     manager = device;
 }
Exemple #17
0
 void UART_Server_OnStateChange(EDeviceState state, SerialManager sm)
 {
     try
     {
         Invoke(new Action<EDeviceState>(ChangeState), state);
     }
     catch(Exception e)
     {
     }
 }