Welcome! Log In Create A New Profile

Advanced

Reading data on serial port

Posted by Mitchell 
Mitchell
Reading data on serial port
June 06, 2009 11:53AM
Dear Covers,
anyone can advise me please on how to read data on serial port ? I tried the help file "sOpen", sRead , etc.... but none of them is working

nPort = sOpen(1, 4096, 4096, sDefaultTimeout, True)
IF ErrorOccurred THEN
Error()
ELSE


// Configuring I/O
sParameter(nPort, 4800, 0, 8, 1, True, True, True)

// Insert your code here
xNumber is int


Info("press ENTER now")

sMessageRead is string

xNumber = sInEntryQueue(nPort)
sMessageRead = sRead(nPort, xNumber)
info(sMessageRead) // this line is giving empty string .


advise please.

thanks
Alexander Predl
Re: Reading data on serial port
June 06, 2009 02:37PM
Hi!

First of all i use a sysTimer or a sEvent. Your other code should work properly!

In my opinion (tested) do not use sEvent with ThreadExecute/ThreadWaitSignal/ThreadSendSignal in the same window... this is not working (at the moment)

//Code to use with TimerSys
Local
  nPort is int

nPort=sOpen("COM1:", 5000, 5000, 5000)
sParameter(nPort, 9600,0,8,0, True , True , True)
TimerSys("SerialRead", 50, 1)

//Dont't forget to close the com port on closing window/procedure...
sClose(nPort)

Procedure SerialRead(PortNum,Event)
info(sRead(1,sInEntryQueue(1)))

//Code to use with sEvent
Local
  nPort is int

nPort=sOpen("COM1:", 5000, 5000, 5000,true)
sParameter(nPort, 9600,0,8,0, True , True , True)
sEvent(nPort,sEveCharReceived,"SerialRead") 

//Dont't forget to close the com port on closing window/procedure...
sClose(nPort)

Procedure SerialRead(PortNum,Event)
info(sRead(PortNum,sInEntryQueue(PortNum)))
christoph
Re: Reading data on serial port
June 06, 2009 02:43PM
Hello Mitchel,

nPort = sOpen(1,..) dont give you the real PortNumber. You cannot use nPort in sParameter,sread, sinentyqueue etc. You must give the real PortNumber - in your case, you has open COM1 in sOpen. sMessageRead = sRead(1, xNumber) give you what you want.

Christoph
Andres Sanchez
Re: Reading data on serial port
June 07, 2009 04:55AM
Mitchell

// Configuring I/O
sParameter(nPort, 4800, 0, 8, 1, True, True, True)

I think that your problem is in the stop bits you have 1 that is equivalent to 1.5
and this is seldom used, if you need 1 stop bit, you must put 0 in the stop bits.

<NbStopBits>: Integer
Number of stop bits:
0 1 stop bit
1 1.5 stop bits
2 2 stop bits


I know cause I made the same mistake.

Hope it helps

Andres Sanchez
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: