private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int pos = 0; lock (input) { input.Append(serialPort.ReadExisting()); while (input.Length > 0 && (pos = gp()) > 0) { Reply = pos == 0 ? string.Empty : input.ToString(0, pos - 1); input.Remove(0, pos + 1); #if RESPONSELOG if (log != null) { log.WriteLine(Reply); log.Flush(); } #endif if (Reply.Length != 0 && DataReceived != null) { Dispatcher.BeginInvoke(DataReceived, Reply); } state = Reply == "ok" ? Comms.State.ACK : (Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); } } }
private void OnMessage(object sender, MessageEventArgs e) { int pos = 0; lock (input) { if (e.IsText) { input.Append(e.Data); } else { input.Append(Encoding.Default.GetString(e.RawData, 0, e.RawData.Length)); } while (input.Length > 0 && (pos = input.ToString().IndexOf('\n')) > 0) { Reply = input.ToString(0, pos - 1); input.Remove(0, pos + 1); state = Reply == "ok" ? Comms.State.ACK : (Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); if (Reply.Length != 0 && DataReceived != null) { Dispatcher.Invoke(DataReceived, Reply); } } } }
private void SerialPort_DataReceived() { int pos = 0; lock (input) { input.Append(serialPort.ReadStr()); while (input.Length > 0 && (pos = input.ToString().IndexOf('\n')) > 0) { Reply = input.ToString(0, pos - 1); input.Remove(0, pos + 1); #if RESPONSELOG log.WriteLine(Reply); log.Flush(); #endif if (Reply.Length != 0 && DataReceived != null) { Dispatcher.BeginInvoke(DataReceived, Reply); } state = Reply == "ok" ? Comms.State.ACK : (Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); } } }
void ReadComplete(IAsyncResult iar) { int bytesAvailable = 0; byte[] buffer = (byte[])iar.AsyncState; try { bytesAvailable = ipstream.EndRead(iar); } catch { // error handling required here (and many other places)... } int pos = 0; lock (this.input) { this.input.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, bytesAvailable)); while (this.input.Length > 0 && (pos = this.input.ToString().IndexOf('\n')) > 0) { this.Reply = this.input.ToString(0, pos - 1); this.input.Remove(0, pos + 1); this.state = this.Reply == "ok" ? Comms.State.ACK : (this.Reply == "FAILED" ? Comms.State.NAK : Comms.State.DataReceived); if (this.Reply.Length != 0 && this.DataReceived != null) { this.DataReceived(this.Reply); } } } ipstream.BeginRead(this.buffer, 0, buffer.Length, ReadComplete, this.buffer); }
public void WriteCommand(string command) { state = Comms.State.AwaitAck; if (command.Length > 1 || command == GrblConstants.CMD_PROGRAM_DEMARCATION) { command += "\r"; } WriteString(command); }
public void WriteCommand(string command) { this.state = Comms.State.AwaitAck; if (command.Length > 1 || command == "%") { command += "\r"; } WriteString(command); }
public void WriteCommand(string command) { this.state = Comms.State.AwaitAck; if (command.Length == 1 && command != GrblConstants.CMD_PROGRAM_DEMARCATION) { WriteByte((byte)command.ToCharArray()[0]); } else if (command.Length > 0) { command += "\r"; this.SerialPort.WriteStr(command); } }
public void WriteCommand(string command) { state = Comms.State.AwaitAck; if (command.Length == 1 && command != GrblConstants.CMD_PROGRAM_DEMARCATION) { WriteByte((byte)command.ToCharArray()[0]); } else if (command.Length > 0) { command += "\r"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(command); serialPort.BaseStream.Write(bytes, 0, bytes.Length); // serialPort.Write(command); } }
void ReadComplete(IAsyncResult iar) { int bytesAvailable = 0; byte[] buffer = (byte[])iar.AsyncState; try { bytesAvailable = ipstream.EndRead(iar); } catch { // error handling required here (and many other places)... } int pos = 0; lock (input) { input.Append(Encoding.ASCII.GetString(buffer, 0, bytesAvailable)); if (EventMode) { while (input.Length > 0 && (pos = gp()) > 0) { Reply = input.ToString(0, pos - 1); input.Remove(0, pos + 1); state = Reply == "ok" ? Comms.State.ACK : (Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); if (Reply.Length != 0 && DataReceived != null) { Dispatcher.Invoke(DataReceived, Reply); } } } else { ByteReceived?.Invoke(ReadByte()); } if (ipstream != null && ipserver.Connected) { ipstream.BeginRead(buffer, 0, buffer.Length, ReadComplete, buffer); } } }
private void SerialRead() { int pos = 0; lock (this.input) { this.input.Append(this.SerialPort.ReadStr()); while (this.input.Length > 0 && (pos = this.input.ToString().IndexOf('\n')) > 0) { this.Reply = this.input.ToString(0, pos - 1); this.input.Remove(0, pos + 1); this.state = this.Reply == "ok" ? Comms.State.ACK : (this.Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); if (this.Reply.Length != 0 && this.DataReceived != null) { this.DataReceived(this.Reply); } } } }
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int pos = 0; //Action<string> addEdge = (s) => //{ // DataReceived(s); //}; lock (input) { input.Append(serialPort.ReadExisting()); if (EventMode) { while (input.Length > 0 && (pos = gp()) > 0) { Reply = pos == 0 ? string.Empty : input.ToString(0, pos - 1); input.Remove(0, pos + 1); #if RESPONSELOG if (log != null) { log.WriteLine(Reply); log.Flush(); } #endif if (Reply.Length != 0 && DataReceived != null) { Dispatcher.BeginInvoke(DataReceived, Reply); } // Dispatcher.Invoke(addEdge, Reply); state = Reply == "ok" ? Comms.State.ACK : (Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); } } else { ByteReceived?.Invoke(ReadByte()); } // if (EventMode) // { // while (serialPort.BytesToRead > 0) // { // var bytes = Math.Min(serialPort.BytesToRead, buffer.Length); // serialPort.Read(buffer, 0, bytes); // input.Append(Encoding.ASCII.GetString(buffer, 0, bytes)); // if (EventMode) // { // while (input.Length > 0 && (pos = gp()) > 0) // { // Reply = pos == 0 ? string.Empty : input.ToString(0, pos - 1); // input.Remove(0, pos + 1); //#if RESPONSELOG // if (log != null) // { // log.WriteLine(Reply); // log.Flush(); // } //#endif // if (Reply.Length != 0 && DataReceived != null) // Dispatcher.BeginInvoke(DataReceived, Reply); // state = Reply == "ok" ? Comms.State.ACK : (Reply.StartsWith("error") ? Comms.State.NAK : Comms.State.DataReceived); // } // } // else // ByteReceived?.Invoke(ReadByte()); // } // } } }