internal static bool send_init_to_master() { uint retval = 0; COMSTAT cs; uint er; writePacket(JVS_ADDR_BROADCAST, new[] { JVS_OP_RESET, JVS_OP_RESET_ARG }); if (!Win32Com.ClearCommError(HPort, out er, out cs)) { return(false); } if (!Win32Com.EscapeCommFunction(HPort, Win32Com.SETRTS)) { return(false); } Thread.Sleep(1000); JVS_DEVICE_COUNT = 0; while (JVS_DEVICE_COUNT != 1) { JVS_DEVICE_COUNT++; //info(true, "Sending reset to JVS %08d", JVS_DEVICE_COUNT); send_init_to_JVS_board(JVS_DEVICE_COUNT); } return(true); }
internal static List <byte> GetJvsReply(byte node, byte[] package) { List <byte> result = new List <byte>(); writePacket(node, package); //if (!ClearCommError(hPort, 0, 0)) //return false; if (!Win32Com.EscapeCommFunction(HPort, Win32Com.SETRTS)) { return(result); } uint count = 0; uint Err; COMSTAT CST; while (true) { Win32Com.ClearCommError(HPort, out Err, out CST); if ((CST.cbInQue > 0) || (count > 1000000)) { break; } count++; } result.AddRange(readPacket()); if (result[0] != JVS_STATUS_OK) { Console.WriteLine("JVS Status not ok!"); return(null); } if (result[1] != JVS_REPORT_OK) { Console.WriteLine("JVS Report not ok!"); return(null); } //Console.WriteLine("Reply ok!"); //info(true, "first eight bytes from reply buffer: %02x %02x %02x %02x %02x %02x %02x %02x", readData[0], readData[1], readData[2], readData[3], readData[4], readData[5], readData[6], readData[7]); // We remove status and report. result.RemoveAt(0); result.RemoveAt(0); return(result); }
private static bool ReadTheComData() { COMSTAT cs; uint er; if (!Win32Com.ClearCommError(HPort, out er, out cs)) { return(false); } if (!Win32Com.EscapeCommFunction(HPort, Win32Com.CLRRTS)) { return(false); } Thread.Sleep(1000); uint count = 0; uint Err; COMSTAT CST; while (true) { Win32Com.ClearCommError(HPort, out Err, out CST); if ((CST.cbInQue > 0) || (count > 1000000)) { break; } count++; } if (CST.cbInQue == 0) { return(false); } var result = readPacket(); if (result[0] != JVS_STATUS_OK) { return(false); } if (result[1] != JVS_REPORT_OK) { return(false); } return(true); }
/// <summary> /// Get the status of the queues /// </summary> /// <returns>Queue status object</returns> protected QueueStatus GetQueueStatus() { COMSTAT cs; COMMPROP cp; uint er; CheckOnline(); if (!Win32Com.ClearCommError(_hPort, out er, out cs)) { ThrowException("Unexpected failure"); } if (!Win32Com.GetCommProperties(_hPort, out cp)) { ThrowException("Unexpected failure"); } return(new QueueStatus(cs.Flags, cs.cbInQue, cs.cbOutQue, cp.dwCurrentRxQueue, cp.dwCurrentTxQueue)); }
private void ReceiveThread() { var buf = new Byte[1]; var sg = new AutoResetEvent(false); var ov = new OVERLAPPED(); var unmanagedOv = Marshal.AllocHGlobal(Marshal.SizeOf(ov)); ov.Offset = 0; ov.OffsetHigh = 0; ov.hEvent = sg.Handle; Marshal.StructureToPtr(ov, unmanagedOv, true); uint eventMask = 0; var uMask = Marshal.AllocHGlobal(Marshal.SizeOf(eventMask)); try { while (true) { if (!Win32Com.SetCommMask(_hPort, Win32Com.EV_RXCHAR | Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR | Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING | Win32Com.EV_ERR)) { throw new CommPortException("IO Error [001]"); } Marshal.WriteInt32(uMask, 0); if (!Win32Com.WaitCommEvent(_hPort, uMask, unmanagedOv)) { if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) { sg.WaitOne(); } else { throw new CommPortException("IO Error [002]"); } } eventMask = (uint)Marshal.ReadInt32(uMask); if ((eventMask & Win32Com.EV_ERR) != 0) { UInt32 errs; if (Win32Com.ClearCommError(_hPort, out errs, IntPtr.Zero)) { var s = new StringBuilder("UART Error: ", 40); if ((errs & Win32Com.CE_FRAME) != 0) { s = s.Append("Framing,"); } if ((errs & Win32Com.CE_IOE) != 0) { s = s.Append("IO,"); } if ((errs & Win32Com.CE_OVERRUN) != 0) { s = s.Append("Overrun,"); } if ((errs & Win32Com.CE_RXOVER) != 0) { s = s.Append("Receive Cverflow,"); } if ((errs & Win32Com.CE_RXPARITY) != 0) { s = s.Append("Parity,"); } if ((errs & Win32Com.CE_TXFULL) != 0) { s = s.Append("Transmit Overflow,"); } s.Length = s.Length - 1; throw new CommPortException(s.ToString()); } throw new CommPortException("IO Error [003]"); } if ((eventMask & Win32Com.EV_RXCHAR) != 0) { uint gotbytes; do { if (!Win32Com.ReadFile(_hPort, buf, 1, out gotbytes, unmanagedOv)) { if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) { Win32Com.CancelIo(_hPort); gotbytes = 0; } else { throw new CommPortException("IO Error [004]"); } } if (gotbytes == 1) { OnRxChar(buf[0]); } } while (gotbytes > 0); } if ((eventMask & Win32Com.EV_TXEMPTY) != 0) { OnTxDone(); } if ((eventMask & Win32Com.EV_BREAK) != 0) { OnBreak(); } uint i = 0; if ((eventMask & Win32Com.EV_CTS) != 0) { i |= Win32Com.MS_CTS_ON; } if ((eventMask & Win32Com.EV_DSR) != 0) { i |= Win32Com.MS_DSR_ON; } if ((eventMask & Win32Com.EV_RLSD) != 0) { i |= Win32Com.MS_RLSD_ON; } if ((eventMask & Win32Com.EV_RING) != 0) { i |= Win32Com.MS_RING_ON; } if (i != 0) { uint f; if (!Win32Com.GetCommModemStatus(_hPort, out f)) { throw new CommPortException("IO Error [005]"); } OnStatusChange(new ModemStatus(i), new ModemStatus(f)); } } } catch (Exception e) { if (uMask != IntPtr.Zero) { Marshal.FreeHGlobal(uMask); } if (unmanagedOv != IntPtr.Zero) { Marshal.FreeHGlobal(unmanagedOv); } if (!(e is ThreadAbortException)) { _rxException = e; OnRxException(e); } } }