public bool closeComport() { Win32Com.CancelIo(hPort); if (!Win32Com.CloseHandle(hPort)) { //Console.WriteLine("Warning: Can't close COM port!"); return(false); } return(true); }
public unsafe void writeComport(string sBuffer) { byte[] byteArray = new byte[MAX]; byteArray = encoding.GetBytes(sBuffer + "\r\n"); fixed(byte *data = &byteArray[0]) { int write = 0; if (!Win32Com.WriteFile(hPort, data, byteArray.Length, &write, ptrUWO)) { Console.WriteLine("Error: Can't write file into COM port!"); } } }
public void timeOut() { if (!Win32Com.GetCommTimeouts(hPort, out CommTimeouts)) { //Console.WriteLine("TIMEOUT FEJL"); CommTimeouts.ReadIntervalTimeout = 0; } CommTimeouts.ReadTotalTimeoutMultiplier = 1; CommTimeouts.ReadTotalTimeoutConstant = 0; CommTimeouts.WriteTotalTimeoutMultiplier = 0; CommTimeouts.WriteTotalTimeoutConstant = 0; if (!Win32Com.SetCommTimeouts(hPort, ref CommTimeouts)) { Console.WriteLine("TIMEOUT FEJL"); } }
public unsafe string readComport() { int index = 0; string sBuffer; byte[] buffer = new byte[MAX]; int n = 1; while (n != 0) { n = 0; fixed(byte *data = &buffer[0]) { if (!Win32Com.ReadFile(hPort, data + index, MAX, &n, ptrUWO)) { Console.WriteLine("Read Modem Failed"); } } } sBuffer = encoding.GetString(buffer); return(sBuffer); }
public bool openComport(string port) { //hPort = Win32Com.CreateFile(port, (uint) Win32Com.GENERIC_READ | Win32Com.GENERIC_WRITE, 0, 0, 3, 0x40000000, 0); hPort = Win32Com.CreateFile(port, Win32Com.GENERIC_READ | Win32Com.GENERIC_WRITE, 0, IntPtr.Zero, Win32Com.OPEN_EXISTING, 0, IntPtr.Zero); if (hPort == (IntPtr)Win32Com.INVALID_HANDLE_VALUE) { //Console.WriteLine("Error: COM port could not be opened!"); return(false); } else { Win32Com.GetCommState(hPort, ref PortDCB); PortDCB.BaudRate = 115200; // PortDCB.ByteSize = 8; // 8 databit PortDCB.Parity = 0; //no parity PortDCB.StopBits = 0; //1 stopbit /*PortDCB.XonLim = 2048; * PortDCB.XoffLim = 512; * PortDCB.XonChar = 0x11; // Ctrl-Q * PortDCB.XoffChar = 0x13; // Ctrl-S*/ if (!Win32Com.SetCommState(hPort, ref PortDCB)) { //Console.WriteLine("Error: COM port could not be opened!"); return(false); } timeOut(); return(true); } }