C++ OK204-25-USB writing text issue

LK/ELK/VK/PK/OK/MX/GLK/EGLK/GVK/GLT Series

Moderators: Henry, Mods

Post Reply
mascenzi
LCD?
Posts: 9
Joined: Thu Apr 25, 2019 9:18 am

C++ OK204-25-USB writing text issue

Post by mascenzi »

Hello,

I just picked up my first character display and I'm having a few issues displaying characters on it. So first off, using the hello, world sample code it is working mostly fine. I get a weird character at the very end of the string that I didn't enter. attached photo helloworldchar.jpg

The bigger issue I'm having is the C++ code I'm writing and having issues sending text to the display.

So I have three files. A header and source file that contains a bunch of functions that call the different commands listed in the Manual. For instance.

Code: Select all

 void setAutoScroll (int state = 0); // state 0 = off, 1 = on
	void ClearScreen ();
	void changeStartupScreen (char data);
	void setAutoLineWrap (int state = 0); // state 0 = off, 1 = on
	void setCursorPosition (int col = 0, int row = 0);
	void goHome ();
	void moveCursor (int state, int positions = 0); // state 0 = off, 1 = on, positions = number of positions to move
	void underlineCursor (int state); // state 0 = off, 1 = on
	void blinkingCursor (int state); // state 0 = off, 1 = on
And the third file is just a main method test class to test the Character_Display class I created. I want to make the calls portable to different projects I'm working on.

so when I create an object of the Character_Display class in the constructor is where I create my connection to the USB port and set the attributes.

I have a function that is simply writeToDisplay() that takes in a unsigned char array, and writes it to the display.

Code: Select all

void Character_Display::writeToDisplay (unsigned char mesg[])
{

	if (!WriteFile (my_port, &mesg, sizeof (mesg), &bytes_written, NULL))	//Write message
	{
		std::cout << "Error writing message to display!" << std::endl;	//Check for errors in writing, call GetLastError with the int response for more info
	}
}
however, the text on the display doesn't make sense. check image texterror.jpg

what I'm doing is as follows.

main method

Code: Select all

Character_display cd;  // create object
unsigned char message[] = "Test 1";    
cd.ClearScreen();
cd.writeToScreen(message);
This is obviously an issue with my c++ abilities, as if I create an new message from within the writeToDisplay() function it works just fine. Its something to do with how I'm passing the value to the function. I just can't seem to figure it out.
Attachments
write to display garble.
write to display garble.
texterror.jpg (33.76 KiB) Viewed 8645 times
Hello Wold Weird Character
Hello Wold Weird Character
helloworldChar.jpg (3.18 MiB) Viewed 8645 times

Daniel Divino
Matrix Orbital
Matrix Orbital
Posts: 247
Joined: Thu Sep 24, 2015 9:38 am

Re: C++ OK204-25-USB writing text issue

Post by Daniel Divino »

Hi mascenci,

Welcome to the forums!

I've taken a look at your first photo (helloworldchar.jpg) and it looks like your string's NULL terminator (0x00) is also being sent and displayed on screen. Sending the Hello World string without the following NULL terminator should get rid of that extraneous character on your screen.

It looks like the issue highlighted in your second image is being caused by a mismatched baud rate. What baud rate is your port set to, and does it match your OK204-25's baud rate?

Cheers,
Daniel
Daniel Divino
Technical Support
Matrix Orbital

mascenzi
LCD?
Posts: 9
Joined: Thu Apr 25, 2019 9:18 am

Re: C++ OK204-25-USB writing text issue

Post by mascenzi »

Hi Daniel,

Hey thanks for the welcome! I appreciate your time. So I'm not sure how to remove the NULL terminator. I've worked with serial communications awhile back and remember that there might be a way to ignore that by making changes to the port_attributes. I'll have to dig into that a bit more. For now I just de-incremented the sizeof value by 1. This eliminated the null terminator.

So the garbled message. I'm using the same baudrate for each of my program, 19200. Bytesize = 8, no parity, and one stop bit. I literally just copied and pasted these settings. All other commands, change cursor position, blinker on off, underline on off, and etc is all just fine. Here's a sample that works and doesn't work at in the same program.

Code: Select all

Character_Display cd;
unsigned char message[] = "Testing 1";
cd.ClearScreen ();
cd.writeToDisplay (message);

void Character_Display::writeToDisplay (unsigned char mesg1[])
{
	unsigned char mesg2[] = "Test 2";

	WriteFile (my_port, &mesg1, sizeof (mesg1)-1, &bytes_written, NULL);  // this line won't work, see picture below. 
	
	setCursorPosition (1, 2);

	WriteFile (my_port, &mesg2, sizeof (mesg2)-1, &bytes_written, NULL);  // this line will work just fine. 
}
line 1 garble, line 2 clean
line 1 garble, line 2 clean
both.jpg (38.56 KiB) Viewed 8632 times

mascenzi
LCD?
Posts: 9
Joined: Thu Apr 25, 2019 9:18 am

Re: C++ OK204-25-USB writing text issue

Post by mascenzi »

So I've resolved the issue and I was correct it had to do with my inexperience with c++. I changed the incoming parameters associated with my function. I've included a pointer to the first character in the array and the the size of the array. This allowed me to use the pointer and the size to pass the text to the display and worked just the way I needed it to.

Code: Select all

Character_Display cd = Character_Display;
unsigned char message[] = "Testing 1";
unsigned char * ptr = &message[0];
cd.ClearScreen ();
cd.writeToDisplay (&message[0], sizeof(message));

void Character_Display::writeToDisplay (unsigned char* ptr, int size)
{	
	unsigned char mesg2[] = "Test 2";

	WriteFile (my_port, ptr, size-1, &bytes_written, NULL);  // this line won't work, see picture below. 
	
	setCursorPosition (1, 2);

	WriteFile (my_port, &mesg2, sizeof (mesg2)-1, &bytes_written, NULL);  // this line will work just fine. 
}
success.jpg
success.jpg (33.19 KiB) Viewed 8623 times

Daniel Divino
Matrix Orbital
Matrix Orbital
Posts: 247
Joined: Thu Sep 24, 2015 9:38 am

Re: C++ OK204-25-USB writing text issue

Post by Daniel Divino »

Hi mascenzi,

Sorry for the delay in getting back to you! I'm glad to hear that you were able to resolve the issue.

Now that your communication interface has been sorted, you should have everything you need to fully control your new display.

Let us know if you have any other questions or concerns!

Cheers,
Daniel
Daniel Divino
Technical Support
Matrix Orbital

Post Reply