Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

[solved] SerialPort problems


Best Answer firestar, 30 December 2012 - 05:46 PM

okay it is working now.resoldered my wires. maybe i had a bad connection or so somewhere.i have removed the flush as well as this only seems to deal with sending data. Go to the full post


  • Please log in to reply
4 replies to this topic

#1 firestar

firestar

    Member

  • Members
  • PipPip
  • 15 posts

Posted 29 December 2012 - 07:46 PM

Hi,   I'm trying to read my smart meter, which should send a message every 10 seconds as a TTL signal. The signal is inverted, so i'm using a FET to invert the signal again.   However when im reading in the software, i read zeros all the time, i cant get a single character in my buffer, also the event is pretty much fired continuously, I'm using the following code:  

        void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)        {            if (e.EventType == SerialData.Chars)            {                int bytesToRead = comPort.BytesToRead;                // if we received any bytes in the serial buffer                if (bytesToRead > 0)                {                    byte[] buffer = new byte[bytesToRead];                    comPort.Read(buffer, 0, bytesToRead);                    comPort.Flush();

right now im just trying to read the non inverted signal to see if it works before I hook up the FET.   Did I miss anything in the code?   Best Regards, Martin



#2 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 29 December 2012 - 09:28 PM

Hi Martin. Normally TTL serial devices can be conected directly without an inverter. The inverter (level shifter) is needed for TTL to RS232 conversion. Let us know how you get on. If you have a PC with serial port and RS232-TTL converter, you could try listening or injecting serial data into the system you are building. Good luck - Paul



#3 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 29 December 2012 - 10:39 PM

and: calling flush like this is a bad idea. cos its not sure that all the data will come at once. so you have a pretty high cance you flush a part of the data.

id wait till your expected lenght is in the buffer, than read it.

 

byte[] buffer ..

int index = 0;

while(comPort.BytesToRead < your_expected_data_lenght)

{

buffer[index] = readwhats_there

increment index

}



#4 firestar

firestar

    Member

  • Members
  • PipPip
  • 15 posts

Posted 30 December 2012 - 09:44 AM

Hi,

 

I just modified my code to read per byte. Makes it easier to find starting and ending characters in the telegram as well.

 

The reason for the inverter is that the spec of the meter mentions SPACE > 4V and MARK = 0V. Because they use an opto on their side.

 

I will test some later today and see if that fixes anything.

 

 



#5 firestar

firestar

    Member

  • Members
  • PipPip
  • 15 posts

Posted 30 December 2012 - 05:46 PM   Best Answer

okay it is working now.resoldered my wires. maybe i had a bad connection or so somewhere.i have removed the flush as well as this only seems to deal with sending data.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.