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); }
private void ReceiveSendThreadSerial() { var buffer = new byte[64]; var restart = false; var sendMessageBytes = new byte[0]; int sentBytes = 0; var sendTimeout = new Stopwatch(); var portHandle = IntPtr.Zero; Thread.Sleep(1000); _receivedBytes.Clear(); var resetEvent = new AutoResetEvent(false); var overlapped = new OVERLAPPED(); var ptrOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(overlapped)); overlapped.Offset = 0; overlapped.OffsetHigh = 0; overlapped.hEvent = resetEvent.SafeWaitHandle.DangerousGetHandle(); Marshal.StructureToPtr(overlapped, ptrOverlapped, true); bool waitingRead = false; var writeResetEvent = new AutoResetEvent(false); var writeOverlapped = new OVERLAPPED(); var ptrWriteOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(writeOverlapped)); writeOverlapped.Offset = 0; writeOverlapped.OffsetHigh = 0; writeOverlapped.hEvent = writeResetEvent.SafeWaitHandle.DangerousGetHandle();; Marshal.StructureToPtr(writeOverlapped, ptrWriteOverlapped, true); bool waitingWrite = false; while (!_receiveSendThreadStopped) { try { if (portHandle == IntPtr.Zero) { int portnumber = Int32.Parse(_comPort.Replace("COM", String.Empty)); string comPort = _comPort; if (portnumber > 9) { comPort = String.Format("\\\\.\\{0}", _comPort); } portHandle = Win32Com.CreateFile(comPort, Win32Com.GENERIC_READ | Win32Com.GENERIC_WRITE, 0, IntPtr.Zero, Win32Com.OPEN_EXISTING, Win32Com.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (portHandle == (IntPtr)Win32Com.INVALID_HANDLE_VALUE) { if (Marshal.GetLastWin32Error() == Win32Com.ERROR_ACCESS_DENIED) { throw new Exception(String.Format("Access denied for port {0}", _comPort)); } else { throw new Exception(String.Format("Failed to open port {0}", _comPort)); } } COMMTIMEOUTS commTimeouts = new COMMTIMEOUTS { ReadIntervalTimeout = 5, ReadTotalTimeoutConstant = 0, ReadTotalTimeoutMultiplier = 0, WriteTotalTimeoutConstant = 0, WriteTotalTimeoutMultiplier = 0 }; DCB dcb = new DCB(); dcb.Init(false, false, false, 0, false, false, false, false, 0); dcb.BaudRate = _baudRate; dcb.ByteSize = 8; dcb.Parity = 0; dcb.StopBits = 0; if (!Win32Com.SetupComm(portHandle, 8192, 4096)) { throw new Exception(String.Format("Failed to set queue settings for port {0}", _comPort)); } if (!Win32Com.SetCommState(portHandle, ref dcb)) { throw new Exception(String.Format("Failed to set comm settings for port {0}", _comPort)); } if (!Win32Com.SetCommTimeouts(portHandle, ref commTimeouts)) { throw new Exception(String.Format("Failed to set comm timeouts for port {0}", _comPort)); } if (_rtsCts) { if (!Win32Com.EscapeCommFunction(portHandle, Win32Com.CLRRTS)) { throw new Exception(String.Format("Failed to reset RTS pin{0}", _comPort)); } } Thread.Sleep(1000); } if (!Win32Com.GetHandleInformation(portHandle, out uint lpdwFlags)) { throw new Exception(String.Format("Port {0} went offline", _comPort)); } if (!waitingRead) { waitingRead = true; if (!Win32Com.ReadFile(portHandle, buffer, (uint)buffer.Length, out uint readBytes, ptrOverlapped)) { if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) { throw new Exception(String.Format("Failed to read port {0}", _comPort)); } } } else { if (resetEvent.WaitOne(10)) { waitingRead = false; if (!Win32Com.GetOverlappedResult(portHandle, ptrOverlapped, out uint readBytes, false)) { throw new Exception(String.Format("Failed to read port {0}", _comPort)); } for (int i = 0; i < readBytes; i++) { _receivedBytes.Add(buffer[i]); } } } if (_sendMessageQueue.Count > 0 && sendMessageBytes.Length == 0) { lock (_syncobject) sendMessageBytes = _sendMessageQueue.Dequeue(); sentBytes = 0; } if (sendMessageBytes.Length > 0) { if (_rtsCts) { if (!Win32Com.EscapeCommFunction(portHandle, Win32Com.SETRTS)) { lock (_syncobject) _sendExceptionQueue.Enqueue(new SBPSendExceptionEventArgs(new Exception(String.Format("Failed to set RTS pin{0}", _comPort)))); } } uint lpmodemstat = 0; if (_rtsCts) { if (!Win32Com.GetCommModemStatus(portHandle, out lpmodemstat)) { _sendExceptionQueue.Enqueue(new SBPSendExceptionEventArgs(new Exception(String.Format("Failed to get RTS pin{0}", _comPort)))); } } if ((lpmodemstat & Win32Com.MS_CTS_ON) > 0 || !_rtsCts) { if (!waitingWrite) { waitingWrite = true; sendTimeout.Restart(); if (!Win32Com.WriteFile(portHandle, sendMessageBytes, (uint)sendMessageBytes.Length, out uint sent, ptrWriteOverlapped)) { if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) { _sendExceptionQueue.Enqueue(new SBPSendExceptionEventArgs(new Exception(String.Format("Failed to write to port {0}", _comPort)))); } } } else { if (writeResetEvent.WaitOne(0)) { if (!Win32Com.GetOverlappedResult(portHandle, ptrWriteOverlapped, out uint sent, false)) { _sendExceptionQueue.Enqueue(new SBPSendExceptionEventArgs(new Exception(String.Format("Failed to write to port {0}", _comPort)))); } sentBytes += (int)sent; } } if (sentBytes == sendMessageBytes.Length || sendTimeout.ElapsedMilliseconds > 200) { if (sendTimeout.ElapsedMilliseconds > 100) { lock (_syncobject) _sendExceptionQueue.Enqueue(new SBPSendExceptionEventArgs(new Exception(String.Format("Failed to write all bytes to port {0}", _comPort)))); } if (_rtsCts) { if (!Win32Com.EscapeCommFunction(portHandle, Win32Com.CLRRTS)) { lock (_syncobject) _sendExceptionQueue.Enqueue(new SBPSendExceptionEventArgs(new Exception(String.Format("Failed to reset RTS pin{0}", _comPort)))); } } sendMessageBytes = new byte[0]; waitingWrite = false; } } } ProcessReading(restart); restart = false; } catch (Exception e) { _receivedBytes.Clear(); Win32Com.CancelIo(portHandle); Win32Com.CloseHandle(portHandle); portHandle = IntPtr.Zero; restart = true; lock (_syncobject) _readExceptionQueue.Enqueue(new SBPReadExceptionEventArgs(e)); Thread.Sleep(5000); } } Win32Com.CancelIo(portHandle); Win32Com.CloseHandle(portHandle); Marshal.FreeHGlobal(ptrOverlapped); Marshal.FreeHGlobal(ptrWriteOverlapped); }