You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					65 lines
				
				1.1 KiB
			
		
		
			
		
	
	
					65 lines
				
				1.1 KiB
			| 
								 
											4 years ago
										 
									 | 
							
								using EC.Utils.SerialPortHelper;
							 | 
						||
| 
								 | 
							
								using System;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace EC.AutoWeightServer.IfManager.Light
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									public class LightRelay : ILightRelay
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										private ECSerialPort _ecSerialPort;
							 | 
						||
| 
								 | 
							
										private readonly string _comName;
							 | 
						||
| 
								 | 
							
										private readonly int _baudRate;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										public LightRelay(string comName, int baudRate)
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											_comName = comName;
							 | 
						||
| 
								 | 
							
											_baudRate = baudRate;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										public bool StartServer()
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											_ecSerialPort = SerialPortManager.CreateSerialPort(_comName, _baudRate);
							 | 
						||
| 
								 | 
							
											try
							 | 
						||
| 
								 | 
							
											{
							 | 
						||
| 
								 | 
							
												_ecSerialPort.OpenCom();
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
											catch (Exception ex)
							 | 
						||
| 
								 | 
							
											{
							 | 
						||
| 
								 | 
							
												LogUnit.Error(ex.Message);
							 | 
						||
| 
								 | 
							
												return false;
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										public bool StopServer()
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											_ecSerialPort?.CloseCom();
							 | 
						||
| 
								 | 
							
											_ecSerialPort = null;
							 | 
						||
| 
								 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										public bool IsOpen()
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											if (_ecSerialPort == null)
							 | 
						||
| 
								 | 
							
												return false;
							 | 
						||
| 
								 | 
							
											if (!_ecSerialPort.IsOpen())
							 | 
						||
| 
								 | 
							
												return false;
							 | 
						||
| 
								 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										public bool SendAscii(string text)
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											if (!IsOpen())
							 | 
						||
| 
								 | 
							
												return false;
							 | 
						||
| 
								 | 
							
											_ecSerialPort.Send(false, text);
							 | 
						||
| 
								 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										public bool SendHex(string text)
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											if (!IsOpen())
							 | 
						||
| 
								 | 
							
												return false;
							 | 
						||
| 
								 | 
							
											_ecSerialPort.Send(true, text);
							 | 
						||
| 
								 | 
							
											return true;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |