Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert serial port application from C# to C in visual studio (ERROR_INVALID_PARAMETER) 87
    primarykey
    data
    text
    <p>I am trying to write an app in C to communication with a device through a COM port.</p> <p>I am able to send data using Docklight (hyper terminal like program) with the following settings and I can verify that I am receiving it through Itronix LogicPort Logic Analyzer. Baud rate:9600 Parity:None Byte Size:8 Stop bits:1</p> <p>I am having 2 problems with my C program: 1) sometimes CreateFile returns INVALID_HANDLE_VALUE and the other problem I can't seem to get by is that GetCommState() SetCommState() SetCommTimeouts() always return ERROR_INVALID_PARAMETER 87 (0x57)</p> <p>Since I couldn't get past this problem, I tried a program in C# and that works just fine, but my program must be in C to communicate with the rest of my code. This program also communicates with other programs in C through sockets.</p> <p>in Device Manager the device I want to talk to is listed as COM6 under Ports(COM &amp; LPT)</p> <p>any help would be appreciated!</p> <p>Below is the code for both programs:</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; namespace WindowsFormsApplication1 { public partial class Form1 : Form { byte[] TxBuffer = new byte[20]; SerialPort CommPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CommPort.Open(); TxBuffer[0] = (byte)'|'; TxBuffer[1] = 0; TxBuffer[2] = 0; TxBuffer[3] = 0; TxBuffer[4] = 1; TxBuffer[5] = 100; TxBuffer[6] = 1; TxBuffer[7] = 6; TxBuffer[8] = (byte)'|'; TxBuffer[9] = 1; } private void button1_Click(object sender, EventArgs e) { CommPort.Write(TxBuffer, 0, 10); } } } </code></pre> <p>and the C code is:</p> <p>somewhere </p> <pre><code> struct serial_dev { HANDLE hSerial; }; struct serial_dev sdev; //global //somewhere in main if (init_serial(&amp;sdev, com_file) == -1) return -1; write_ser(&amp;sdev, buffer, buf_size); close_serial(&amp;sdev); // in serial.c int init_serial(struct serial_dev *sdev, char *port_name) { DCB dcbSerialParams; COMMTIMEOUTS timeouts; int error = 0; memset(&amp;dcbSerialParams, 0, sizeof(dcbSerialParams)); memset(&amp;timeouts, 0, sizeof(timeouts)); sdev-&gt;hSerial = CreateFile("COM6", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (sdev-&gt;hSerial == INVALID_HANDLE_VALUE) { error = GetLastError(); close_serial(sdev); fprintf(stderr, "error: could not open serial port (error:%d)\n", error); return -1; } memset(&amp;dcbSerialParams, 0, sizeof(dcbSerialParams)); dcbSerialParams.DCBlength = sizeof(dcbSerialParams); if (!GetCommState(sdev-&gt;hSerial, &amp;dcbSerialParams)) { close_serial(sdev); error = GetLastError(); fprintf(stderr, "error: could not get serial port state (%d)\n", error); return -1; } dcbSerialParams.BaudRate = CBR_9600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY; if (!SetCommState(sdev-&gt;hSerial, &amp;dcbSerialParams)) { close_serial(sdev); error = GetLastError(); fprintf(stderr, "error: could not set serial port state (%d)\n", error); return -1; } timeouts.ReadIntervalTimeout = 50; timeouts.ReadTotalTimeoutConstant = 200; timeouts.ReadTotalTimeoutMultiplier = 10; timeouts.WriteTotalTimeoutConstant = 50; timeouts.WriteTotalTimeoutMultiplier = 10; if (!SetCommTimeouts(sdev-&gt;hSerial, &amp;timeouts)) { close_serial(sdev); error = GetLastError(); fprintf(stderr, "error: could not set serial port timeouts (%d)\n", error); return -1; } return 0; } void close_serial(struct serial_dev *sdev) { CloseHandle(sdev-&gt;hSerial); } int write_ser(struct serial_dev *sdev, void *buf, size_t len) { DWORD written; BOOL success; success = WriteFile(sdev-&gt;hSerial, buf, len, &amp;written, NULL); if(!success || written == 0) { printf("error writing to serial\n"); return -1; } printf("writing to serial OK!\n"); return written; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload