more G-Labs products

Author Topic: SerialPort can't connect  (Read 1075 times)

February 28, 2016, 10:46:26 PM
Read 1075 times

nobody

  • *
  • Information
  • Newbie
  • Posts: 5
Hi!
So I would like to use in my project the RPi's serial port. I don't have hardware connection on the pins, for now I just would like to test the future script. For that I created virtual serial ports with socat, and a script is running and sending contiunosly the "test" message to the port. With CuteCom I tested it and the messages arrived.

**EDIT**
So I've tried to jumper Rx and Tx on ttyAMA0, and it is working too, HG didn't get the anything...
OK now the third script is running well! But the python script still nothing.
Second version would be working, but the messages need to convert to string I think..

So problem SOLVED.
*****

First thing I tried:
A python script, basic reading. I couldn't import serial. I used sys.path.append and it seemed to be OK, but the functions wasn't enabled so I Couldn't configure the port.

Second:
I used the SerialPortHelper, with the following code:
Code: [Select]
def ser_rec (mymessage):
    # this will be called every time a message is received from serial port
    hg.Program.Notify("SerialPort String", mymessage);
    print mymessage
    return None

#setup program stuff
def setup():
  portname = "/dev/ttyUSB0"  #this changes depending on the PRI USB or GPIO connection
  #ser = hg.SerialPort.WithName( portname ).OnMessageReceived( ser_rec ).Connect(9600)   # 9600
  ser = hg.SerialPort.WithName( portname ).OnMessageReceived( ser_rec ).Connect()   # defaults to 115200
  hg.Program.Notify("Python Test", "Hello Python "+portname)
  return None
hg.Program.Setup(setup)
But nothing happened.

Third try:
I used the C# script which I found on the forum:
Code: [Select]
var portname = "/dev/ttyAMA0";

Action<string>
HandleStringReceived = (string message) => {
 
  // this will be called every time a message is received from serial port
Program.Notify("SerialPort String", message);
 
};

Action<byte[]>
HandleMessageReceived = (byte[] message) => {
 
  // this will be called every time a message is received from serial port
Program.Notify("SerialPort Bytes", BitConverter.ToString(message));
 
};

Action<bool>
HandleStatusChanged = (bool connected) => {
 
  // this will be called every time the connection status changes
  Program.Notify("SerialPort Status", connected ? "CONNECTED!" : "DISCONNECTED!");
 
};

// open the serial port channel and register handlers
SerialPort
  .WithName( portname )
  .OnStatusChanged( HandleStatusChanged )
  .OnMessageReceived( HandleMessageReceived )
  .OnStringReceived( HandleStringReceived )
  .Connect( 9600 ); // change baud rate if needed

while (Program.IsEnabled)
{
  // send a test message through serial port every 10 seconds
  //
  byte[] message = { 0x00, 0x00, 0x00, 0x00, 0x00 };
  SerialPort.SendMessage(message);
  //
  Pause(5);
  SerialPort.SendMessage("Hello Things!");
  //
  Pause(10);
}

But still nothing happens... I couldn't get anything.

Could You help me with that? What am I doing wrong?
Thanks in advance!
nobody
« Last Edit: February 29, 2016, 10:03:04 AM by nobody »


There are no comments for this topic. Do you want to be the first?