| | |
| | | |
| | | namespace WIDESEAWCS_Tasks |
| | | { |
| | | [DisallowConcurrentExecution] |
| | | public class SerialPortJob : JobBase, IJob |
| | | { |
| | | public enum CommandType |
| | |
| | | Get = 1, |
| | | Set = 2, |
| | | } |
| | | private SerialPort serialPort; |
| | | private CommandType commandType = CommandType.None; |
| | | private string commandGet = "";//è¯»å¼ |
| | | private string commandSet = "";//è®¾å¼ |
| | | private string setOK = ""; |
| | | private string getOK = ""; |
| | | private static SerialPort serialPort; |
| | | private static CommandType commandType = CommandType.None; |
| | | private string commandGet = "824070241JNT\r";//è¯»å¼ |
| | | private string commandSet = "824070241JNT,0005000,+0001000[+TOL],-0001000[-TOL]\r";//è®¾å¼ |
| | | private string setOK = "ParseOK"; |
| | | private string getOK = "82407024103"; |
| | | private string getError = "82407024104"; |
| | | public Task Execute(IJobExecutionContext context) |
| | | { |
| | | try |
| | | { |
| | | if (serialPort == null) { serialPort = new SerialPort("COM1"); serialPort.DataReceived += SerialPort_DataReceived;} |
| | | if (serialPort == null) { serialPort = new SerialPort("COM3"); serialPort.DataReceived += SerialPort_DataReceived; } |
| | | if (!serialPort.IsOpen) |
| | | { |
| | | serialPort.Open(); |
| | | |
| | | |
| | | |
| | | serialPort.Write(commandSet);//æå¼ä¸²å£æ¶å
è®¾å¼ |
| | | commandType = CommandType.Set; |
| | |
| | | break; |
| | | } |
| | | } |
| | | catch (Exception ex) { } return Task.CompletedTask; |
| | | catch (Exception ex) |
| | | { |
| | | throw new Exception(ex.Message); |
| | | } |
| | | return Task.CompletedTask; |
| | | } |
| | | private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) |
| | | { |
| | |
| | | else if (data.Contains(getOK) && commandType == CommandType.Get) |
| | | { |
| | | commandType = CommandType.Set; |
| | | |
| | | |
| | | } |
| | | else if (data.Contains(getError) && commandType == CommandType.Get) |
| | | { |
| | | commandType = CommandType.Set; |
| | | |
| | | |
| | | } |
| | | } |
| | | } |