views:

995

answers:

5

Hi,

I'm trying to write to an LCD Display using System.IO.Ports in C#, with the SerialPort class.

Firstly, it works! My program does write to it successfully and it displays fine... but only after I've run a prior commerical program (LCDC).

If I run by program before running LCDC (a full LCD-display GUI program) - the LCD display sits there blankly, not even a flicker.

If I 'Start Display' on LCDC (i.e. make it display even a single screen from LCDC's catalogue) and then 'Stop Display' and shut down LCDC completely; and THEN run my program it works absolutely fine.

So it seems LCDC is activating or initialising the COM/serial port or something, that the port then remembers after the program has shut down. Does anyone have any idea?

I have tried countless code samples using SerialPort (from various online resources) and they all have the same problem - nothing before LCDC, but full functionality afterwards.

I can post code if you need but it's the standard: declare SerialPort port

port.Open( x, y z); port.Write("string");

etc. and it works fine after LCDC is run!

Thanks for reading,

Duncan.

+1  A: 

I would think first that is not something that is getting configured on the serial port on your computer, but rather some initialization that the LCD requires before you start sending data.

I would recommend you run a tracer on the serial port to see what the other application is sending to the LCD before it sends the first line of text to display.

You could try Portmon from Sysinternals (now part of Microsoft).

jvanderh
A: 

I've had similar issues with connecting to a motor, where running another application first caused my application to work.

The issue ended up being noise on the serial port, both when writing and reading. Sending test/junk messages, and clearing the Input and Output buffers eventually cleared the issue up. In my case, the other application was just clearing the junk out of the buffers.

Will Eddins
+1  A: 

Make sure your handshake matches what the other end is expecting. Try setting DTR and RTS to true.

sPort.Dtr = true;
sPort.Rts = true;
Rob Elliott
A: 

I went through an almost identical issue recently with serial port communications. I would try connecting to my device, and no communications would work properly. Then I would open up a serial port monitoring program, to see if I was indeed getting any data transfer, and everything would look okay. Finally, I would try connecting again and I would be able to communicate properly.

What was happening in my case was that I was not setting up the baud rate, read/write timeouts, and data characteristics (data bits, stop bits, parity bits) in my program. The debug program did that automatically, and those changes seemed to "stick with" the serial port, which was why my program would work after running the debug program once.

Make sure you've set all of those settings properly in your program.

Mark Rushakoff
A: 

I'm still stuck! I used PortMon to analyse the port, firstly for my program running alone:

0   0.04459980 MatOrbOfficialT IRP_MJ_CREATE VCP0 SUCCESS Options: Open  
1   0.00000531 MatOrbOfficialT IOCTL_SERIAL_GET_PROPERTIES VCP0 SUCCESS  
2   0.00000335 MatOrbOfficialT IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS  
3   0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS  
4   0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS  
5   0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS  
6   0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS  
7   0.00000307 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS  
8   0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS  
9   0.00000196 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS  
10  0.00000196 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS  
11  0.00216620 MatOrbOfficialT IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200 
12  0.00295484 MatOrbOfficialT IOCTL_SERIAL_SET_RTS VCP0 SUCCESS  
13  0.00297244 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS  
14  0.00297496 MatOrbOfficialT IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8 
15  0.00000251 MatOrbOfficialT IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13 
16  0.00501293 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:40 XonLimit:1024 XoffLimit:1024 
17  0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS  
18  0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS  
19  0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS  
20  0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS  
21  0.00282829 MatOrbOfficialT IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200 
22  0.00293892 MatOrbOfficialT IOCTL_SERIAL_SET_RTS VCP0 SUCCESS  
23  0.00296909 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS  
24  0.00297719 MatOrbOfficialT IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8 
25  0.00000279 MatOrbOfficialT IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13 
26  0.00294954 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:40 XonLimit:1024 XoffLimit:1024 
27  0.00288919 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS  
28  0.00000447 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS  
29  0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS  
30  0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS  
31  0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS  
32  0.00285763 MatOrbOfficialT IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200 
33  0.00296406 MatOrbOfficialT IOCTL_SERIAL_CLR_RTS VCP0 SUCCESS  
34  0.00298753 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS  
35  0.00292132 MatOrbOfficialT IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8 
36  0.00000559 MatOrbOfficialT IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13 
37  0.00491962 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:1024 XoffLimit:1024 
38  0.00292942 MatOrbOfficialT IOCTL_SERIAL_CLR_RTS VCP0 SUCCESS  
39  0.00000475 MatOrbOfficialT IOCTL_SERIAL_SET_TIMEOUTS VCP0 SUCCESS RI:-1 RM:-1 RC:-2 WM:0 WC:0 
40  0.00245674 MatOrbOfficialT IOCTL_SERIAL_SET_WAIT_MASK VCP0 SUCCESS Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING  
41  0.00000670 MatOrbOfficialT IOCTL_SERIAL_SET_QUEUE_SIZE VCP0 SUCCESS InSize: 4096 OutSize: 2048 
42  0.00000000 MatOrbOfficialT IOCTL_SERIAL_WAIT_ON_MASK VCP0   
43  0.00049811 MatOrbOfficialT IRP_MJ_WRITE VCP0 SUCCESS Length 2: .X

Then for LCDC:

0   0.05153979 LCDC.exe IRP_MJ_CREATE VCP0 SUCCESS Options: Open  
1   0.00000503 LCDC.exe IOCTL_SERIAL_SET_QUEUE_SIZE VCP0 SUCCESS InSize: 1024 OutSize: 1024 
2   0.00000251 LCDC.exe IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS  
3   0.00000279 LCDC.exe IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS  
4   0.00000223 LCDC.exe IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS  
5   0.00000251 LCDC.exe IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS  
6   0.00274029 LCDC.exe IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200 
7   0.00299563 LCDC.exe IOCTL_SERIAL_CLR_RTS VCP0 SUCCESS  
8   0.00295205 LCDC.exe IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS  
9   0.00289757 LCDC.exe IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8 
10  0.00000531 LCDC.exe IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:b0 ERR:1 BRK:1 EVT:0 XON:79 XOFF:13 
11  0.00296155 LCDC.exe IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:256 XoffLimit:256 
12  0.00000307 LCDC.exe IOCTL_SERIAL_SET_TIMEOUTS VCP0 SUCCESS RI:-1 RM:0 RC:0 WM:100 WC:1000 
13  0.00251401 LCDC.exe IOCTL_SERIAL_SET_WAIT_MASK VCP0 SUCCESS Mask: RXCHAR ERR  
14  0.00000335 LCDC.exe IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS  
15  0.00000251 LCDC.exe IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS  
16  0.00000251 LCDC.exe IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS  
17  0.00000391 LCDC.exe IOCTL_SERIAL_PURGE VCP0 SUCCESS Purge: TXCLEAR RXCLEAR 
18  0.00066545 LCDC.exe IRP_MJ_WRITE VCP0 SUCCESS Length 3: .P. 
19  3.94750981 LCDC.exe IOCTL_SERIAL_WAIT_ON_MASK VCP0 SUCCESS  
20  0.00091632 LCDC.exe IRP_MJ_WRITE VCP0 SUCCESS Length 3: ...

Any ideas?

I noticed the LCDC did an IOCTL_SERIAL_PURGE right before writing, but my program does this on disconnection - and then it still doesn't work after reconnecting (so I'm assuming that it's not to do with this purge).

If I run my program again after LCDC, it reads exactly the same except for:

26 0.00294954 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:1024 XoffLimit:1024

Where is says Replace:0 used to read Replace:40, is this significant?

As far as I can tell... all other values set by LCDC are reset by my program after I run it, so surely it should make no difference that some values are different? Given that my program does write successfully... Only after LCDC has done it's business!

Thanks again,

Duncan.

As jvanderh suggested, try using Portmon to look at the actual data going from LCDC to the device during startup. Most devices need some initialization sequence sent from the host.
mtrw