Need simple uart code to RX/TX from a power meter

I have a TI power meter with a uart interface and I need some code to read and write from the uart interface. Can anybody suggest the code required on the agent and the device?

I’m a beginner and also trying to get UART code working. No success but here’s my code so far if it helps:
`
uart1 <- hardware.uart12;

function inverter1();
{
local a = uart1.read();
server.log(“incoming char from inverter1”)
server.log(a);
}

uart1.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, inverter1);

function writeln();
{
uart1.write(“test - 123 - 123 - 123 - 123 - 123 - 123”);
imp.wakeup(10, writeln);
}

writeln();
`

Can either of you link to a spec for the protocol you’re trying to talk here?

In my case it’s just hyperterm on the PC. I just want to move chars back and forth as a first step.

Are you using a level convertor chip? “Real” PC serial ports on 9 or 25-pin connectors use different (much higher) voltage levels from the imp’s UARTs.

Peter

Hi Peter, yep, max3232 doing the RS232 voltages. I’m getting data Imp -> PC.
Going PC-> Imp I can see data signals reaching pin 2 but nothing turns up except “-1”.

Perhaps I don’t understand uart.configure properly. Does data arriving on the Imp uart trigger the function defined in uart.configure?

Here’s the code as simple as I can make it. As simple as it is, the penny hasn’t dropped for me because I’m only seeing the function “writeln” execute one:

`
uart1 <- hardware.uart12;

function charfromPC();
{ local a = uart1.read();
server.log(“incoming from PC”);
server.log(a); }

uart1.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, charfromPC);

function writeln();
{ uart1.write(“test - 123 - 123 - 123 - 123 - 123 - 123”); }

writeln();
writeln();
writeln();
writeln();
writeln();
`

You need to remove those stray semicolons in the function definitions, and there might be more than one character to read when charfromPC is called, so you should loop there, or use the new, more efficient readstring or readblob.

`uart1 <- hardware.uart12;

function charfromPC()
{
local a = uart1.read();
while (a != -1) {
server.log(“incoming from PC”);
server.log(a);
a = uart1.read();
}
}

uart1.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, charfromPC);

function writeln()
{
uart1.write(“test - 123 - 123 - 123 - 123 - 123 - 123”);
}

writeln();
writeln();
writeln();
writeln();
writeln();`

That was the answer. Thanks, all working.

It’s been decades since I learnt Pascal but those semicolons are still second nature.

Here’s an example of how easy it can be:

`uart1 <- hardware.uart12;

function charsfromPC()
{ local a = uart1.readstring();
server.log(a); }

uart1.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, charsfromPC);

uart1.write(“type now and it’ll appear in the device log”);`