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
  | ||||||||||||||
[solved] SerialPort problems
Started by firestar, Dec 29 2012 07:46 PM
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
4 replies to this topic
#1Posted 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 #2Posted 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 #3Posted 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 } #4Posted 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.
#5Posted 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 topic0 members, 0 guests, 0 anonymous users | ||||||||||||||
|
||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | ||||||||||||||